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

📄 stringutils.java

📁 Standord Classifier实现了一个基于Java的最大熵分类器。用于模式识别
💻 JAVA
字号:
package edu.stanford.nlp.util;import java.util.*;import java.util.regex.*;import java.io.*;import java.net.*;/** *  Stringtils is a class for random String things.  * *  @author Dan Klein *  @author Christopher Manning *  @version 2003/02/03 */public class StringUtils {    /**     * Don't let anyone instantiate this class.     */    private StringUtils() {}    /** Say whether this regular expression can be found inside      *  this String.  This method provides one of the two "missing"     *  convenience methods for regular expressions in the String class     *  in JDK1.4.  This is the one you'll want to use all the time if     *  you're used to Perl.  What were they smoking?     *  @param str String to search for match in     *  @param regex String to compile as the regular expression     *  @return Whether the regex can be found in str     */    public static boolean find(String str, String regex) {	return Pattern.compile(regex).matcher(str).find();    }    /** Say whether this regular expression can be found at the beginning of     *  this String.  This method provides one of the two "missing"     *  convenience methods for regular expressions in the String class     *  in JDK1.4.      *  @param str String to search for match at start of     *  @param regex String to compile as the regular expression     *  @return Whether the regex can be found at the start of str     */    public static boolean lookingAt(String str, String regex) {	return Pattern.compile(regex).matcher(str).lookingAt();    }    /** Say whether this regular expression matches     *  this String.  This method is the same as the String.matches() method,     *  and is included just to give a call that is parallel to the other     *  static regex methods in this class.     *  @param str String to search for match at start of     *  @param regex String to compile as the regular expression     *  @return Whether the regex matches the whole of this str     */    public static boolean matches(String str, String regex) {	return Pattern.compile(regex).matcher(str).matches();    }    public static String slurpFile(File file) throws IOException {        String lineSeparator = System.getProperty("line.separator");		BufferedReader br = new BufferedReader(new FileReader(file));	String temp;	StringBuffer buff = new StringBuffer(16000);  // make biggish	while ((temp = br.readLine()) != null) {	    buff.append(temp);	    buff.append(lineSeparator);	}	br.close();	return buff.toString();    }      public static String slurpURL(URL u) throws IOException {        String lineSeparator = System.getProperty("line.separator");	URLConnection uc = u.openConnection();	InputStream is = uc.getInputStream();	BufferedReader br = new BufferedReader(new InputStreamReader(is));	String temp;	StringBuffer buff = new StringBuffer(16000);  // make biggish	while ((temp = br.readLine()) != null) {	    buff.append(temp);	    buff.append(lineSeparator);	}	br.close();	return buff.toString();    }    public static String join(List l, String glue) {    StringBuffer sb = new StringBuffer();    for (int i = 0; i < l.size(); i++) {      if (i > 0)	sb.append(glue);      sb.append(l.get(i).toString());    }    return sb.toString();  }    public static String join(Object[] elements,String glue)  {      return(join(Arrays.asList(elements),glue));  }  public static String join(List l) {    return join(l, " ");  }    public static String join(Object[] elements) {      return(join(elements," "));  }  public static List split(String s) {    return(Arrays.asList(s.split("\\s+")));  }  /** Return a String of length a minimum of totalChars characters by   *  padding the input String str with spaces.  If str is already longer   *  than totalChars, it is returned unchanged.    */  public static String pad(String str, int totalChars) {    StringBuffer sb = new StringBuffer(str);    for (int i = 0; i < totalChars-str.length(); i++) {      sb.append(" ");    }    return sb.toString();  }  public static String pad(Object obj, int totalChars) {    return pad(obj.toString(), totalChars);  }  public static String leftPad(String str, int totalChars) {    StringBuffer sb = new StringBuffer();    for (int i = 0; i < totalChars-str.length(); i++) {      sb.append(" ");    }    sb.append(str);    return sb.toString();  }  public static String leftPad(Object obj, int totalChars) {    return leftPad(obj.toString(), totalChars);  }    public static String leftPad(int i, int totalChars) {      return leftPad(new Integer(i), totalChars);  }  public static String leftPad(double d, int totalChars) {      return leftPad(new Double(d), totalChars);  }    /** Returns s if it's at most maxWidth chars, otherwise chops right side to fit. */  public static String trim(String s, int maxWidth) {      if(s.length()<=maxWidth) return(s);      return(s.substring(0,maxWidth));  }    public static String trim(Object obj, int maxWidth) {      return trim(obj.toString(), maxWidth);  }  public static String fileNameClean(String s) {    char[] chars = s.toCharArray();    StringBuffer sb = new StringBuffer();    for (int i = 0; i < chars.length; i++) {      char c = chars[i];      if ((c >= 'A' && c <= 'Z') ||	  (c >= 'a' && c <= 'z') ||	  (c >= '0' && c <= '9') ||	  (c == '_'))	  sb.append(c);      else {	if (c == ' ' ||	    c == '-')	  sb.append('_');	else 	  sb.append("x"+(int)c+"x");      }    }    return sb.toString();  }}

⌨️ 快捷键说明

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