mathexpression.java
来自「一个小型的数据挖掘器应用软件,综合数据挖掘的各种功能」· Java 代码 · 共 946 行 · 第 1/3 页
JAVA
946 行
if (ignoreList.length() != 0) { setIgnoreRange(ignoreList); } setInvertSelection(Utils.getFlag('V', options)); } /** * Gets the current settings of the filter. * * @return an array of strings suitable for passing to setOptions */ public String [] getOptions() { String [] options = new String [5]; int current = 0; options[current++] = "-E"; options[current++] = getExpression(); if (getInvertSelection()) { options[current++] = "-V"; } if (!getIgnoreRange().equals("")) { options[current++] = "-R"; options[current++] = getIgnoreRange(); } while(current < options.length) { options[current++] = ""; } return options; } /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ public Enumeration listOptions() { Vector newVector = new Vector(3); newVector.addElement(new Option( "\tSpecify the expression to apply. Eg. pow(A,6)/(MEAN+MAX)" +"\n\tSupported operators are +, -, *, /, pow, log," +"\n\tabs, cos, exp, sqrt, tan, sin, ceil, floor, rint, (, ), " +"\n\tMEAN, MAX, MIN, SD, COUNT, SUM, SUMSQUARED, ifelse", "E",1,"-E <expression>")); newVector.addElement(new Option( "\tSpecify list of columns to ignore. First and last are valid\n" +"\tindexes. (default none)", "R", 1, "-R <index1,index2-index4,...>")); newVector.addElement(new Option( "\tInvert matching sense (i.e. only modify specified columns)", "V", 0, "-V")); return newVector.elements(); } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String expressionTipText() { return "Specify the expression to apply. The 'A' letter" + "refers to the attribute value. MIN,MAX,MEAN,SD" + "refer respectively to minimum, maximum, mean and" + "standard deviation of the attribute." +"\n\tSupported operators are +, -, *, /, pow, log," +"abs, cos, exp, sqrt, tan, sin, ceil, floor, rint, (, )," +"A,MEAN, MAX, MIN, SD, COUNT, SUM, SUMSQUARED, ifelse" +"\n\tEg. pow(A,6)/(MEAN+MAX)*ifelse(A<0,0,sqrt(A))+ifelse(![A>9 && A<15])"; } /** * Set the expression to apply * @param expr a mathematical expression to apply */ public void setExpression(String expr) { m_expression = expr; } /** * Get the expression * @return the expression */ public String getExpression() { return m_expression; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String invertSelectionTipText() { return "Determines whether action is to select or unselect." + " If set to true, only the specified attributes will be modified;" + " If set to false, specified attributes will not be modified."; } /** * Get whether the supplied columns are to be select or unselect * * @return true if the supplied columns will be kept */ public boolean getInvertSelection() { return !m_SelectCols.getInvert(); } /** * Set whether selected columns should be select or unselect. If true the * selected columns are modified. If false the selected columns are not * modified. * * @param invert the new invert setting */ public void setInvertSelection(boolean invert) { m_SelectCols.setInvert(!invert); } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String ignoreRangeTipText() { return "Specify range of attributes to act on." + " This is a comma separated list of attribute indices, with" + " \"first\" and \"last\" valid values. Specify an inclusive" + " range with \"-\". E.g: \"first-3,5,6-10,last\"."; } /** * Get the current range selection. * * @return a string containing a comma separated list of ranges */ public String getIgnoreRange() { return m_SelectCols.getRanges(); } /** * Set which attributes are to be ignored * * @param rangeList a string representing the list of attributes. Since * the string will typically come from a user, attributes are indexed from * 1. <br/> * eg: first-3,5,6-last */ public void setIgnoreRange(String rangeList) { m_SelectCols.setRanges(rangeList); } /** * Class to built the tree of the grammar:<br/> * * Exp -> Term '+' Exp <br/> * | Term '-' Exp <br/> * | Term <br/> * Term -> Atom '*' Term <br/> * | Atom '/' Term <br/> * | Atom <br/> * Atom -> <number> <br/> * | '('Exp')' <br/> * | function '(' Params ')' <br/> * | '-' Atom | VARIABLE <br/> * | Disjunction <br/> * Params -> E <br/> * | E,Params <br/> * Disjunction -> Conjunction <br/> * | Conjunction '|' Disjunction <br/> * Conjonction -> NumTest <br/> * | NumTest '&' Conjonction <br/> * NumTest -> Exp '<' Exp | Exp '>' Exp | Exp '=' Exp | '[' Disjunction ']' | '!' '[' Disjunction ']'<br/> */ static public class Parser { /** * A tokenizer for Math Expression */ static public class Tokenizer extends StreamTokenizer { /** token for a variable */ final static int TT_VAR = -5; /** token for a function */ final static int TT_FUN = -6; /** token for if-else */ final static int TT_IFELSE = -7; /** * Constructor * * @param r the reader to use */ public Tokenizer(Reader r) { super(r); resetSyntax(); parseNumbers(); whitespaceChars(' ',' '); wordChars('a','z'); wordChars('A','Z'); ordinaryChar('-'); } /** * returns the next token * * @return the next token * @throws IOException if something goes wrong */ public int nextToken() throws IOException { super.nextToken(); if (ttype == TT_WORD) { if (sval.equals("ifelse")) { ttype = TT_IFELSE; } else if (Character.isUpperCase(sval.charAt(0))) { ttype = TT_VAR; } else { ttype = TT_FUN; } } return ttype; } } /** * Tree Node of Math Expression */ static public class TreeNode implements Serializable { /** for serialization */ static final long serialVersionUID = -654720966350007711L; /** The known functions */ static public String[] funs = {"abs", "sqrt", "log", "exp","sin","cos","tan","rint","floor","pow", "ceil"}; /** The arity of the known functions */ static public int[] arity = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1}; /** Node type */ int type; /** Constant value */ double nval; /** Var name */ String sval = null; /** table of operands */ TreeNode operands[] = null; /**Construct a constant node *@param v the value of the constant */ TreeNode(double v) { type = Tokenizer.TT_NUMBER; nval = v; } /** * Construct a constant node * * @param n the value of the constant */ TreeNode(TreeNode n) { type = '!'; operands = new TreeNode[1]; operands[0] = n; } /** * Construct a variable node * * @param v the name of the variable */ TreeNode(String v) { type = Tokenizer.TT_VAR; sval = v; } /** * Construct an ifelse node * * @param p parameters of the ifelse */ TreeNode(Vector p) { type = Tokenizer.TT_IFELSE; operands = new TreeNode[p.size()]; for(int i=0;i<operands.length;i++) { operands[i] = (TreeNode) p.elementAt(i); } } /** * Construct a function node * * @param f the name of the function * @param ops the operands of the function * @throws Exception if function is unknown or wrong arity */ TreeNode(String f,Vector ops) throws Exception { int i = 0; while(i < funs.length && !funs[i].equals(f)) i++; if (i >= funs.length) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?