⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utils.java

📁 wekaUT是 university texas austin 开发的基于weka的半指导学习(semi supervised learning)的分类器
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	    if (i + 1 == options.length) {	      throw new Exception("No value given for -" + flag + " option.");	    }	    options[i] = "";	    newString = new String(options[i + 1]);	    options[i + 1] = "";	    return newString;	  }	  if (options[i].charAt(1) == '-') {	    return "";	  }	}      }    }    return "";  }  /**   * Quotes a string if it contains special characters.   *    * The following rules are applied:   *   * A character is backquoted version of it is one    * of <tt>" ' % \ \n \r \t</tt>.   *   * A string is enclosed within single quotes if a character has been   * backquoted using the previous rule above or contains    * <tt>{ }</tt> or is exactly equal to the strings    * <tt>, ? space or ""</tt> (empty string).   *   * A quoted question mark distinguishes it from the missing value which   * is represented as an unquoted question mark in arff files.   *   * @param string the string to be quoted   * @return the string (possibly quoted)   */  public static String quote(String string) {      boolean quote = false;      // backquote the following characters       if ((string.indexOf('\n') != -1) || (string.indexOf('\r') != -1) || 	  (string.indexOf('\'') != -1) || (string.indexOf('"') != -1) || 	  (string.indexOf('\\') != -1) || 	  (string.indexOf('\t') != -1) || (string.indexOf('%') != -1)) {	  string = backQuoteChars(string);	  quote = true;      }      // Enclose the string in 's if the string contains a recently added      // backquote or contains one of the following characters.      if((quote == true) || 	 (string.indexOf('{') != -1) || (string.indexOf('}') != -1) ||	 (string.indexOf(',') != -1) || (string.equals("?")) ||	 (string.indexOf(' ') != -1) || (string.equals(""))) {	  string = ("'".concat(string)).concat("'");      }      return string;  }  /**   * Converts carriage returns and new lines in a string into \r and \n.   * Backquotes the following characters: ` " \ \t and %   * @param string the string   * @return the converted string   */  public static String backQuoteChars(String string) {    int index;    StringBuffer newStringBuffer;    // replace each of the following characters with the backquoted version    char   charsFind[] =    {'\\',   '\'',  '\t',  '"',    '%'};    String charsReplace[] = {"\\\\", "\\'", "\\t", "\\\"", "\\%"};    for(int i = 0; i < charsFind.length; i++) {	if (string.indexOf(charsFind[i]) != -1 ) {	    newStringBuffer = new StringBuffer();	    while ((index = string.indexOf(charsFind[i])) != -1) {		if (index > 0) {		    newStringBuffer.append(string.substring(0, index));		}		newStringBuffer.append(charsReplace[i]);		if ((index + 1) < string.length()) {		    string = string.substring(index + 1);		} else {		    string = "";		}	    }	    newStringBuffer.append(string);	    string = newStringBuffer.toString();	}    }    return Utils.convertNewLines(string);  }  /**   * Converts carriage returns and new lines in a string into \r and \n.   * @param string the string   * @return the converted string   */  public static String convertNewLines(String string) {        int index;    // Replace with \n    StringBuffer newStringBuffer = new StringBuffer();    while ((index = string.indexOf('\n')) != -1) {      if (index > 0) {	newStringBuffer.append(string.substring(0, index));      }      newStringBuffer.append('\\');      newStringBuffer.append('n');      if ((index + 1) < string.length()) {	string = string.substring(index + 1);      } else {	string = "";      }    }    newStringBuffer.append(string);    string = newStringBuffer.toString();    // Replace with \r    newStringBuffer = new StringBuffer();    while ((index = string.indexOf('\r')) != -1) {      if (index > 0) {	newStringBuffer.append(string.substring(0, index));      }      newStringBuffer.append('\\');      newStringBuffer.append('r');      if ((index + 1) < string.length()){	string = string.substring(index + 1);      } else {	string = "";      }    }    newStringBuffer.append(string);    return newStringBuffer.toString();  }      /**   * Returns the secondary set of options (if any) contained in   * the supplied options array. The secondary set is defined to   * be any options after the first "--". These options are removed from   * the original options array.   *   * @param options the input array of options   * @return the array of secondary options   */  public static String [] partitionOptions(String [] options) {    for (int i = 0; i < options.length; i++) {      if (options[i].equals("--")) {	options[i++] = "";	String [] result = new String [options.length - i];	for (int j = i; j < options.length; j++) {	  result[j - i] = options[j];	  options[j] = "";	}	return result;      }    }    return new String [0];  }  /**   * Split up a string containing options into an array of strings,   * one for each option.   *   * @param optionString the string containing the options   * @return the array of options   */  public static String [] splitOptions(String optionString) {    FastVector optionsVec = new FastVector();    StringTokenizer st = new StringTokenizer(optionString);    while (st.hasMoreTokens())      optionsVec.addElement(st.nextToken());    String [] options = new String[optionsVec.size()];    for (int i = 0; i < optionsVec.size(); i++) {      options[i] = (String)optionsVec.elementAt(i);    }    return options;  }  /**   * Joins all the options in an option array into a single string,   * as might be used on the command line.   *   * @param optionArray the array of options   * @return the string containing all options.   */  public static String joinOptions(String [] optionArray) {    String optionString = "";    for (int i = 0; i < optionArray.length; i++) {      if (optionArray[i].equals("")) {	continue;      }      if (optionArray[i].indexOf(' ') != -1) {	optionString += '"' + optionArray[i] + '"';      } else {	optionString += optionArray[i];      }      optionString += " ";    }    return optionString.trim();  }    /**   * Creates a new instance of an object given it's class name and   * (optional) arguments to pass to it's setOptions method. If the   * object implements OptionHandler and the options parameter is   * non-null, the object will have it's options set. Example use:<p>   *   * <code> <pre>   * String classifierName = Utils.getOption('W', options);   * Classifier c = (Classifier)Utils.forName(Classifier.class,   *                                          classifierName,   *                                          options);   * setClassifier(c);   * </pre></code>   *   * @param classType the class that the instantiated object should   * be assignable to -- an exception is thrown if this is not the case   * @param className the fully qualified class name of the object   * @param options an array of options suitable for passing to setOptions. May   * be null. Any options accepted by the object will be removed from the   * array.   * @return the newly created object, ready for use.   * @exception Exception if the class name is invalid, or if the   * class is not assignable to the desired class type, or the options   * supplied are not acceptable to the object   */  public static Object forName(Class classType,			       String className,			       String [] options) throws Exception {    Class c = null;    try {      c = Class.forName(className);    } catch (Exception ex) {      throw new Exception("Can't find class called: " + className);    }    if (!classType.isAssignableFrom(c)) {      throw new Exception(classType.getName() + " is not assignable from "			  + className);    }    Object o = c.newInstance();    if ((o instanceof OptionHandler)	&& (options != null)) {      ((OptionHandler)o).setOptions(options);      Utils.checkForRemainingOptions(options);    }    return o;  }  /**   * Computes entropy for an array of integers.   *   * @param counts array of counts   * @return - a log2 a - b log2 b - c log2 c + (a+b+c) log2 (a+b+c)   * when given array [a b c]   */  public static double info(int counts[]) {        int total = 0; int c;    double x = 0;    for (int j = 0; j < counts.length; j++) {      x -= xlogx(counts[j]);      total += counts[j];    }    return x + xlogx(total);  }  /**   * Tests if a is smaller or equal to b.   *   * @param a a double   * @param b a double   */  public static boolean smOrEq(double a,double b) {        return (a-b < SMALL);  }  /**   * Tests if a is greater or equal to b.   *   * @param a a double   * @param b a double   */  public static boolean grOrEq(double a,double b) {        return (b-a < SMALL);  }    /**   * Tests if a is smaller than b.   *   * @param a a double   * @param b a double   */  public static boolean sm(double a,double b) {        return (b-a > SMALL);  }  /**   * Tests if a is smaller than b.   *   * @param a a double   * @param b a double    */  public static boolean gr(double a,double b) {        return (a-b > SMALL);  }  /**   * Returns the logarithm of a for base 2.   *   * @param a a double   */  public static double log2(double a) {        return Math.log(a) / log2;  }  /**   * Returns index of maximum element in a given   * array of doubles. First maximum is returned.   *   * @param doubles the array of doubles   * @return the index of the maximum element   */  public static int maxIndex(double [] doubles) {    double maximum = 0;    int maxIndex = 0;    for (int i = 0; i < doubles.length; i++) {      if ((i == 0) || (doubles[i] > maximum)) {	maxIndex = i;	maximum = doubles[i];      }    }    return maxIndex;  }  /**   * Returns index of maximum element in a given   * array of integers. First maximum is returned.   *   * @param ints the array of integers   * @return the index of the maximum element   */  public static int maxIndex(int [] ints) {    int maximum = 0;    int maxIndex = 0;    for (int i = 0; i < ints.length; i++) {      if ((i == 0) || (ints[i] > maximum)) {	maxIndex = i;	maximum = ints[i];      }    }    return maxIndex;  }  /**   * Computes the mean for an array of doubles.   *   * @param vector the array   * @return the mean   */  public static double mean(double[] vector) {      double sum = 0;    if (vector.length == 0) {      return 0;    }    for (int i = 0; i < vector.length; i++) {      sum += vector[i];    }    return sum / (double) vector.length;  }  /**   * Returns index of minimum element in a given   * array of integers. First minimum is returned.   *   * @param ints the array of integers   * @return the index of the minimum element   */  public static int minIndex(int [] ints) {    int minimum = 0;    int minIndex = 0;    for (int i = 0; i < ints.length; i++) {      if ((i == 0) || (ints[i] < minimum)) {	minIndex = i;	minimum = ints[i];      }    }    return minIndex;  }  /**   * Returns index of minimum element in a given   * array of doubles. First minimum is returned.   *   * @param doubles the array of doubles   * @return the index of the minimum element   */  public static int minIndex(double [] doubles) {    double minimum = 0;    int minIndex = 0;    for (int i = 0; i < doubles.length; i++) {      if ((i == 0) || (doubles[i] < minimum)) {	minIndex = i;	minimum = doubles[i];      }    }    return minIndex;  }  /**   * Normalizes the doubles in the array by their sum.   *   * @param doubles the array of double   * @exception IllegalArgumentException if sum is Zero or NaN   */  public static void normalize(double[] doubles) {    double sum = 0;    for (int i = 0; i < doubles.length; i++) {      sum += doubles[i];    }    normalize(doubles, sum);  }  /**   * Normalizes the doubles in the array using the given value.   *   * @param doubles the array of double   * @param sum the value by which the doubles are to be normalized   * @exception IllegalArgumentException if sum is zero or NaN   */  public static void normalize(double[] doubles, double sum) {    if (Double.isNaN(sum)) {      throw new IllegalArgumentException("Can't normalize array. Sum is NaN.");    }    if (sum == 0) {      // Maybe this should just be a return.      throw new IllegalArgumentException("Can't normalize array. Sum is zero.");    }    for (int i = 0; i < doubles.length; i++) {      doubles[i] /= sum;    }  }  /**

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -