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

📄 utils.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:

  /**
   * Tests if a is equal to b.
   *
   * @param a a double
   * @param b a double
   */
  public static boolean eq(double a, double b){
    
    return (a - b < SMALL) && (b - a < SMALL); 
  }

  /**
   * Checks if the given array contains any non-empty options.
   *
   * @param strings an array of strings
   * @exception Exception if there are any non-empty options
   */
  public static void checkForRemainingOptions(String [] options) 
    throws Exception {
    
    int illegalOptionsFound = 0;
    StringBuffer text = new StringBuffer();

    if (options == null) {
      return;
    }
    for (int i = 0; i < options.length; i++) {
      if (options[i].length() > 0) {
	illegalOptionsFound++;
	text.append(options[i] + ' ');
      }
    }
    if (illegalOptionsFound > 0) {
      throw new Exception("Illegal options: " + text);
    }
  }
  
  /**
   * Checks if the given array contains the flag "-Char". Stops
   * searching at the first marker "--". If the flag is found,
   * it is replaced with the empty string.
   *
   * @param flag the character indicating the flag.
   * @param strings the array of strings containing all the options.
   * @return true if the flag was found
   * @exception Exception if an illegal option was found
   */

  public static boolean getFlag(char flag, String [] options) 
    throws Exception {

    if (options == null) {
      return false;
    }
    for (int i = 0; i < options.length; i++) {
      if ((options[i].length() > 1) && (options[i].charAt(0) == '-')) {
	try {
	  Double dummy = Double.valueOf(options[i]);
	} catch (NumberFormatException e) {
	  if (options[i].length() > 2) {
	    throw new Exception("Illegal option: " + options[i]);
	  }
	  if (options[i].charAt(1) == flag) {
	    options[i] = "";
	    return true;
	  }
	  if (options[i].charAt(1) == '-') {
	    return false;
	  }
	}
      }
    }
    return false;
  }

  /**
   * Gets an option indicated by a flag "-Char" from the given array
   * of strings. Stops searching at the first marker "--". Replaces 
   * flag and option with empty strings.
   *
   * @param flag the character indicating the option.
   * @param options the array of strings containing all the options.
   * @return the indicated option or an empty string
   * @exception Exception if the option indicated by the flag can't be found
   */
  public static String getOption(char flag, String [] options) 
    throws Exception {

    String newString;

    if (options == null)
      return "";
    for (int i = 0; i < options.length; i++) {
      if ((options[i].length() > 0) && (options[i].charAt(0) == '-')) {
	
	// Check if it is a negative number
	try {
	  Double dummy = Double.valueOf(options[i]);
	} catch (NumberFormatException e) {
	  if (options[i].length() != 2) {
	    throw new Exception("Illegal option: " + options[i]);
	  }
	  if (options[i].charAt(1) == flag) {
	    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];
  }
    
  /**
   * The inverse operation of backQuoteChars().
   * Converts back-quoted carriage returns and new lines in a string 
   * to the corresponding character ('\r' and '\n').
   * Also "un"-back-quotes the following characters: ` " \ \t and %
   * @param string the string
   * @return the converted string
   */
  public static String unbackQuoteChars(String string) {

    int index;
    StringBuffer newStringBuffer;
    
    // replace each of the following characters with the backquoted version
    String charsFind[]    = {"\\\\", "\\'", "\\t", "\\\"", "\\%"};
    char   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 + charsFind[i].length()) < string.length()) {
	    string = string.substring(index + charsFind[i].length());
	  } else {
	    string = "";
	  }
	}
	newStringBuffer.append(string);
	string = newStringBuffer.toString();
      }
    }
    return Utils.convertNewLines(string);
  }    
  
  /**
   * 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 quotedOptionString) throws Exception{

    FastVector optionsVec = new FastVector();
    String str = new String(quotedOptionString);
    int i;
    
    while (true){

      //trimLeft 
      i = 0;
      while ((i < str.length()) && (Character.isWhitespace(str.charAt(i)))) i++;
      str = str.substring(i);
      
      //stop when str is empty
      if (str.length() == 0) break;
      
      //if str start with a double quote
      if (str.charAt(0) == '"'){
	
	//find the first not anti-slached double quote
	i = 1;
	while(i < str.length()){
	  if (str.charAt(i) == str.charAt(0)) break;
	  if (str.charAt(i) == '\\'){
	    i += 1;
	    if (i >= str.length()) 
	      throw new Exception("String should not finish with \\");
	    if (str.charAt(i) != '\\' &&  str.charAt(i) != '"') 
	      throw new Exception("Unknow character \\" + str.charAt(i));
	  }
	  i += 1;
	}
	if (i >= str.length()) throw new Exception("Quote parse error.");
	
	//add the founded string to the option vector (without quotes)
	String optStr = str.substring(1,i);
	optStr = unbackQuoteChars(optStr);
	optionsVec.addElement(optStr);
	str = str.substring(i+1);
      } else {
	//find first whiteSpace
	i=0;
	while((i < str.length()) && (!Character.isWhitespace(str.charAt(i)))) i++;
	
	//add the founded string to the option vector
	String optStr = str.substring(0,i);
	optionsVec.addElement(optStr);
	str = str.substring(i);
      }
    }
    
    //convert optionsVec to an array of String
    String [] options = new String[optionsVec.size()];
    for (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) {

⌨️ 快捷键说明

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