📄 addexpression.java
字号:
/**
* Parses a list of options for this object. Valid options are:
* <p>
*
* -E expression <br>
* Specify the expression to apply. Eg. a1^2*a5/log(a7*4.0).
* <p>
*
* -N name <br>
* Specify a name for the new attribute. Default is to name it with the
* expression provided with the -E option.
* <p>
*
* -D<br>
* Debug. Names the attribute with the postfix parse of the expression.
* <p>
*
* @param options
* the list of options as an array of strings
* @exception Exception
* if an option is not supported
*/
public void setOptions(String[] options) throws Exception {
String expString = Utils.getOption('E', options);
if (expString.length() != 0) {
setExpression(expString);
} else {
throw new Exception("Must specify an expression with the -E option");
}
String name = Utils.getOption('N', options);
if (name.length() != 0) {
setName(name);
}
setDebug(Utils.getFlag('D', 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();
options[current++] = "-N";
options[current++] = getName();
if (getDebug()) {
options[current++] = "-D";
}
while (current < options.length) {
options[current++] = "";
}
return options;
}
/**
* Returns the tip text for this property
*
* @return tip text for this property suitable for displaying in the
* explorer/experimenter gui
*/
public String nameTipText() {
return "Set the name of the new attribute.";
}
/**
* Set the name for the new attribute. The string "expression" can be used
* to make the name of the new attribute equal to the expression provided.
*
* @param name
* the name of the new attribute
*/
public void setName(String name) {
m_attributeName = name;
}
/**
* Returns the name of the new attribute
*
* @return the name of the new attribute
*/
public String getName() {
return m_attributeName;
}
/**
* Returns the tip text for this property
*
* @return tip text for this property suitable for displaying in the
* explorer/experimenter gui
*/
public String debugTipText() {
return "Set debug mode. If true then the new attribute will be named with "
+ "the postfix parse of the supplied expression.";
}
/**
* Set debug mode. Causes the new attribute to be named with the postfix
* parse of the expression
*
* @param d
* true if debug mode is to be used
*/
public void setDebug(boolean d) {
m_Debug = d;
}
/**
* Gets whether debug is set
*
* @return true if debug is set
*/
public boolean getDebug() {
return m_Debug;
}
/**
* 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 "Set the math expression to apply. Eg. a1^2*a5/log(a7*4.0)";
}
/**
* Set the expression to apply
*
* @param expr
* a mathematical expression to apply
*/
public void setExpression(String expr) {
m_infixExpression = expr;
}
/**
* Get the expression
*
* @return the expression
*/
public String getExpression() {
return m_infixExpression;
}
/**
* Sets the format of the input instances.
*
* @param instanceInfo
* an Instances object containing the input instance structure
* (any instances contained in the object are ignored - only the
* structure is required).
* @return true if the outputFormat may be collected immediately
* @exception Exception
* if the format couldn't be set successfully
*/
public boolean setInputFormat(Instances instanceInfo) throws Exception {
convertInfixToPostfix(new String(m_infixExpression));
super.setInputFormat(instanceInfo);
Instances outputFormat = new Instances(instanceInfo, 0);
Attribute newAttribute;
if (m_Debug) {
newAttribute = new Attribute(m_postFixExpVector.toString());
} else if (m_attributeName.compareTo("expression") != 0) {
//<<Frank Xu, add relational operators, 09/09/2004
boolean addNominalAtt = false;
if ((m_infixExpression.indexOf("eq") != -1)
|| (m_infixExpression.indexOf("ne") != -1)
|| (m_infixExpression.indexOf("ge") != -1)
|| (m_infixExpression.indexOf("gt") != -1)
|| (m_infixExpression.indexOf("le") != -1)
|| (m_infixExpression.indexOf("lt") != -1)) {
addNominalAtt = true;
}
if (addNominalAtt) {
FastVector vals;
vals = new FastVector(2);
vals.addElement("No");
vals.addElement("Yes");
newAttribute = new Attribute(m_attributeName, vals);
} else {
newAttribute = new Attribute(m_attributeName);
}
//Frank Xu, add relational operators, 09/09/2004>>
} else {
newAttribute = new Attribute(m_infixExpression);
}
outputFormat.insertAttributeAt(newAttribute, instanceInfo
.numAttributes());
setOutputFormat(outputFormat);
return true;
}
/**
* Input an instance for filtering. Ordinarily the instance is processed
* and made available for output immediately. Some filters require all
* instances be read before producing output.
*
* @param instance the input instance
* @return true if the filtered instance may now be
* collected with output().
* @exception IllegalStateException if no input format has been defined.
* @exception Exception if there was a problem during the filtering.
*/
public boolean input(Instance instance) throws Exception {
if (getInputFormat() == null) {
throw new IllegalStateException("No input instance format defined");
}
if (m_NewBatch) {
resetQueue();
m_NewBatch = false;
}
double[] vals = new double[instance.numAttributes() + 1];
for (int i = 0; i < instance.numAttributes(); i++) {
if (instance.isMissing(i)) {
vals[i] = Instance.missingValue();
} else {
vals[i] = instance.value(i);
}
}
evaluateExpression(vals);
Instance inst = null;
if (instance instanceof SparseInstance) {
inst = new SparseInstance(instance.weight(), vals);
} else {
inst = new Instance(instance.weight(), vals);
}
copyStringValues(inst, false, instance.dataset(), getOutputFormat());
inst.setDataset(getOutputFormat());
push(inst);
return true;
}
/**
* Main method for testing this class.
*
* @param args should contain arguments to the filter: use -h for help
*/
public static void main(String[] args) {
try {
if (Utils.getFlag('b', args)) {
Filter.batchFilterFile(new AddExpression(), args);
} else {
Filter.filterFile(new AddExpression(), args);
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
//<<tyleung 22/03/2005
private void validateOperator (String expr) throws Exception{
char c;
int i =0;
while(i < expr.length()){
c = expr.charAt(i);
if (!(Character.isDigit(c)
|| Character.isWhitespace(c)
|| c=='+'
|| c=='-'
|| c=='*'
|| c=='/'
|| c=='('
|| c==')'
|| c=='.'))
{
if ((c =='a' && expr.charAt(i+1)=='b' && expr.charAt(i+2)=='s')
|| (c =='c' && expr.charAt(i+1) == 'o' && expr.charAt(i+2) == 's')
|| (c =='e' && expr.charAt(i+1) == 'x' && expr.charAt(i+2) == 'p')
|| (c =='l' && expr.charAt(i+1) == 'o' && expr.charAt(i+2) == 'g')
|| (c =='s' && expr.charAt(i+1) == 'i' && expr.charAt(i+2) == 'n')
|| (c =='t' && expr.charAt(i+1) == 'a' && expr.charAt(i+2) == 'n'))
{
i = i+3;
}
else if ((c =='c' && expr.charAt(i+1)=='e' && expr.charAt(i+2)=='i' && expr.charAt(i+3)=='l')
|| (c =='r' && expr.charAt(i+1)=='i' && expr.charAt(i+2)=='n' && expr.charAt(i+3)=='t')
|| (c =='s' && expr.charAt(i+1)=='q' && expr.charAt(i+2)=='r' && expr.charAt(i+3)=='t'))
{
i = i+4;
}
else if ((c=='e' && expr.charAt(i+1)=='q')
|| (c=='n' && expr.charAt(i+1)=='e')
|| (c=='g' && expr.charAt(i+1)=='e')
|| (c=='g' && expr.charAt(i+1)=='t')
|| (c=='l' && expr.charAt(i+1)=='e')
|| (c=='l' && expr.charAt(i+1)=='t'))
{
i = i+2;
}
else if (c=='f'
&& expr.charAt(i+1)=='l'
&& expr.charAt(i+2)=='o'
&& expr.charAt(i+3)=='o'
&& expr.charAt(i+4)=='r')
{
i = i+5;
}
else if (c=='a' && Character.isDigit(expr.charAt(i+1))){
i= i+2;
}
else {
throw new Exception("Invalid expression format");
}
}else
i++;
}
}
//tyleung 22/03/2005>>
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -