使用lingpipe自然语言处理包进行文本分类

TrainTClassifier,基于TF/IDF算法的分类器,必须先把要语料库放到各自所属的分类文件夹中,比如:与金融相关的文章就放到金融这个文件夹中,我这的根目录是f:/data/category,训练完后会生成一个分类器模型tclassifier,之后其它文本的分类的确定就是通过它。
[java]  view plain copy
  1. <span style="font-size:18px;">/** 
  2.  * 使用 Lingpipe的TF/IDF分类器训练语料 
  3.  *  
  4.  * @author laigood 
  5.  */  
  6. public class TrainTClassifier {  
  7.   
  8.     //训练语料文件夹  
  9.     private static File TDIR = new File("f:\\data\\category");  
  10.     //定义分类  
  11.     private static String[] CATEGORIES = { "金融""军事""医学""饮食" };  
  12.   
  13.     public static void main(String[] args) throws ClassNotFoundException,  
  14.             IOException {  
  15.           
  16.         TfIdfClassifierTrainer<CharSequence> classifier = new TfIdfClassifierTrainer<CharSequence>(  
  17.                 new TokenFeatureExtractor(CharacterTokenizerFactory.INSTANCE));  
  18.   
  19.         // 开始训练  
  20.         for (int i = 0; i < CATEGORIES.length; i++) {  
  21.             File classDir = new File(TDIR, CATEGORIES[i]);  
  22.             if (!classDir.isDirectory()) {  
  23.                 System.out.println("不能找到目录=" + classDir);  
  24.             }  
  25.   
  26.             // 训练器遍历分类文件夹下的所有文件  
  27.             for (File file : classDir.listFiles()) {  
  28.                 String text = Files.readFromFile(file, "utf-8");  
  29.                 System.out.println("正在训练 " + CATEGORIES[i] + file.getName());  
  30.                 Classification classification = new Classification(  
  31.                         CATEGORIES[i]);  
  32.                 Classified<CharSequence> classified = new Classified<CharSequence>(  
  33.                         text, classification);  
  34.                 classifier.handle(classified);  
  35.             }   
  36.         }  
  37.           
  38.   
  39.         // 把分类器模型写到文件上  
  40.         System.out.println("开始生成分类器");  
  41.         String modelFile = "f:\\data\\category\\tclassifier";  
  42.         ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(  
  43.                 modelFile));  
  44.         classifier.compileTo(os);  
  45.         os.close();  
  46.           
  47.         System.out.println("分类器生成完成");  
  48.     }  
  49. }</span>  

TestTClassifier ,测试分类的准确度,测试数据的存放与上面的类似

[java]  view plain copy
  1. <span style="font-size:18px;">/**  
  2.  * 测试TF/IDF分类器的准确度</span>  
[java]  view plain copy
  1. <span style="font-size:18px;"> *   
  2.  * @author laigood  
  3.  */  
  4. public class TestTClassifier {  
  5.   
  6.     //测试语料的存放目录  
  7.     private static File TDIR = new File("f:\\data\\test");  
  8.     private static String[] CATEGORIES = { "金融""军事""医学""饮食" };  
  9.   
  10.     public static void main(String[] args) throws ClassNotFoundException {  
  11.           
  12.         //分类器模型存放地址  
  13.         String modelFile = "f:\\data\\category\\tclassifier";  
  14.         ScoredClassifier<CharSequence> compiledClassifier = null;  
  15.         try {  
  16.             ObjectInputStream oi = new ObjectInputStream(new FileInputStream(  
  17.                     modelFile));  
  18.             compiledClassifier = (ScoredClassifier<CharSequence>) oi  
  19.                     .readObject();  
  20.             oi.close();  
  21.         } catch (IOException ie) {  
  22.             System.out.println("IO Error: Model file " + modelFile + " missing");  
  23.         }  
  24.   
  25.         // 遍历分类目录中的文件测试分类准确度  
  26.         ConfusionMatrix confMatrix = new ConfusionMatrix(CATEGORIES);  
  27.         NumberFormat nf = NumberFormat.getInstance();  
  28.         nf.setMaximumIntegerDigits(1);  
  29.         nf.setMaximumFractionDigits(3);  
  30.         for (int i = 0; i < CATEGORIES.length; ++i) {  
  31.             File classDir = new File(TDIR, CATEGORIES[i]);  
  32.   
  33.             //对于每一个文件,通过分类器找出最适合的分类  
  34.             for (File file : classDir.listFiles()) {  
  35.                 String text = "";  
  36.                 try {  
  37.                     text = Files.readFromFile(file, "utf-8");  
  38.                 } catch (IOException ie) {  
  39.                     System.out.println("不能读取 " + file.getName());  
  40.                 }  
  41.                 System.out.println("测试 " + CATEGORIES[i]  
  42.                         + File.separator + file.getName());  
  43.   
  44.                 ScoredClassification classification = compiledClassifier  
  45.                         .classify(text.subSequence(0, text.length()));  
  46.                 confMatrix.increment(CATEGORIES[i],  
  47.                         classification.bestCategory());  
  48.                 System.out.println("最适合的分类: "  
  49.                         + classification.bestCategory());  
  50.             }   
  51.         }   
  52.   
  53.         System.out.println("--------------------------------------------");  
  54.         System.out.println("- 结果 ");  
  55.         System.out.println("--------------------------------------------");  
  56.         int[][] imatrix = confMatrix.matrix();  
  57.         StringBuffer sb = new StringBuffer();  
  58.         sb.append(StringTools.fillin("CATEGORY"10true' '));  
  59.         for (int i = 0; i < CATEGORIES.length; i++)  
  60.             sb.append(StringTools.fillin(CATEGORIES[i], 8false' '));  
  61.         System.out.println(sb.toString());  
  62.   
  63.         for (int i = 0; i < imatrix.length; i++) {  
  64.             sb = new StringBuffer();  
  65.             sb.append(StringTools.fillin(CATEGORIES[i], 10true' ',  
  66.                     10 - CATEGORIES[i].length()));  
  67.             for (int j = 0; j < imatrix.length; j++) {  
  68.                 String out = "" + imatrix[i][j];  
  69.                 sb.append(StringTools.fillin(out, 8false' ',  
  70.                         8 - out.length()));  
  71.             }  
  72.             System.out.println(sb.toString());  
  73.         }  
  74.   
  75.         System.out.println("准确度: "  
  76.                 + nf.format(confMatrix.totalAccuracy()));  
  77.         System.out.println("总共正确数 : " + confMatrix.totalCorrect());  
  78.         System.out.println("总数:" + confMatrix.totalCount());  
  79.     }  
  80. }</span>  

补上StringTools

[java]  view plain copy
  1. /** 
  2.  * A class containing a bunch of string utilities - <br> 
  3.  * a. filterChars: Remove extraneous characters from a string and return a 
  4.  * "clean" string. <br> 
  5.  * b. getSuffix: Given a file name return its extension. <br> 
  6.  * c. fillin: pad or truncate a string to a fixed number of characters. <br> 
  7.  * d. removeAmpersandStrings: remove strings that start with ampersand <br> 
  8.  * e. shaDigest: Compute the 40 byte digest signature of a string <br> 
  9.  */  
  10. public class StringTools {  
  11.   public static final Locale LOCALE = new Locale("en");  
  12.   // * -- String limit for StringTools  
  13.   private static int STRING_TOOLS_LIMIT = 1000000;  
  14.   // *-- pre-compiled RE patterns  
  15.   private static Pattern extPattern = Pattern.compile("^.*[.](.*?){1}quot;);  
  16.   private static Pattern spacesPattern = Pattern.compile("\\s+");  
  17.   private static Pattern removeAmpersandPattern = Pattern.compile("&[^;]*?;");  
  18.   
  19.   /** 
  20.    * Removes non-printable spaces and replaces with a single space 
  21.    *  
  22.    * @param in 
  23.    *          String with mixed characters 
  24.    * @return String with collapsed spaces and printable characters 
  25.    */  
  26.   public static String filterChars(String in) {  
  27.     return (filterChars(in, ""' 'true));  
  28.   }  
  29.   
  30.   public static String filterChars(String in, boolean newLine) {  
  31.     return (filterChars(in, ""' ', newLine));  
  32.   }  
  33.   
  34.   public static String filterChars(String in, String badChars) {  
  35.     return (filterChars(in, badChars, ' 'true));  
  36.   }  
  37.   
  38.   public static String filterChars(String in, char replaceChar) {  
  39.     return (filterChars(in, "", replaceChar, true));  
  40.   }  
  41.   
  42.   public static String filterChars(String in, String badChars,  
  43.       char replaceChar, boolean newLine) {  
  44.     if (in == null)  
  45.       return "";  
  46.     int inLen = in.length();  
  47.     if (inLen > STRING_TOOLS_LIMIT)  
  48.       return in;  
  49.     try {  
  50.       // **-- replace non-recognizable characters with spaces  
  51.       StringBuffer out = new StringBuffer();  
  52.       int badLen = badChars.length();  
  53.       for (int i = 0; i < inLen; i++) {  
  54.         char ch = in.charAt(i);  
  55.         if ((badLen != 0) && removeChar(ch, badChars)) {  
  56.           ch = replaceChar;  
  57.         } else if (!Character.isDefined(ch) && !Character.isSpaceChar(ch)) {  
  58.           ch = replaceChar;  
  59.         }  
  60.         out.append(ch);  
  61.       }  
  62.   
  63.       // *-- replace new lines with space  
  64.       Matcher matcher = null;  
  65.       in = out.toString();  
  66.   
  67.       // *-- replace consecutive spaces with single space and remove  
  68.       // leading/trailing spaces  
  69.       in = in.trim();  
  70.       matcher = spacesPattern.matcher(in);  
  71.       in = matcher.replaceAll(" ");  
  72.     } catch (OutOfMemoryError e) {  
  73.       return in;  
  74.     }  
  75.   
  76.     return in;  
  77.   }  
  78.   
  79.   // *-- remove any chars found in the badChars string  
  80.   private static boolean removeChar(char ch, String badChars) {  
  81.     if (badChars.length() == 0)  
  82.       return false;  
  83.     for (int i = 0; i < badChars.length(); i++) {  
  84.       if (ch == badChars.charAt(i))  
  85.         return true;  
  86.     }  
  87.     return false;  
  88.   }  
  89.   
  90.   /** 
  91.    * Return the extension of a file, if possible. 
  92.    *  
  93.    * @param filename 
  94.    * @return string 
  95.    */  
  96.   public static String getSuffix(String filename) {  
  97.     if (filename.length() > STRING_TOOLS_LIMIT)  
  98.       return ("");  
  99.     Matcher matcher = extPattern.matcher(filename);  
  100.     if (!matcher.matches())  
  101.       return "";  
  102.     return (matcher.group(1).toLowerCase(LOCALE));  
  103.   }  
  104.   
  105.   public static String fillin(String in, int len) {  
  106.     return fillin(in, len, true' '3);  
  107.   }  
  108.   
  109.   public static String fillin(String in, int len, char fillinChar) {  
  110.     return fillin(in, len, true, fillinChar, 3);  
  111.   }  
  112.   
  113.   public static String fillin(String in, int len, boolean right) {  
  114.     return fillin(in, len, right, ' '3);  
  115.   }  
  116.   
  117.   public static String fillin(String in, int len, boolean right, char fillinChar) {  
  118.     return fillin(in, len, right, fillinChar, 3);  
  119.   }  
  120.   
  121.   /** 
  122.    * Return a string concatenated or padded to the specified length 
  123.    *  
  124.    * @param in 
  125.    *          string to be truncated or padded 
  126.    * @param len 
  127.    *          int length for string 
  128.    * @param right 
  129.    *          boolean fillin from the left or right 
  130.    * @param fillinChar 
  131.    *          char to pad the string 
  132.    * @param numFills 
  133.    *          int number of characters to pad 
  134.    * @return String of specified length 
  135.    */  
  136.   public static String fillin(String in, int len, boolean right,  
  137.       char fillinChar, int numFills) {  
  138.     // *-- return if string is of required length  
  139.     int slen = in.length();  
  140.     if ((slen == len) || (slen > STRING_TOOLS_LIMIT))  
  141.       return (in);  
  142.   
  143.     // *-- build the fillin string  
  144.     StringBuffer fillinStb = new StringBuffer();  
  145.     for (int i = 0; i < numFills; i++)  
  146.       fillinStb.append(fillinChar);  
  147.     String fillinString = fillinStb.toString();  
  148.   
  149.     // *-- truncate and pad string if length exceeds required length  
  150.     if (slen > len) {  
  151.       if (right)  
  152.         return (in.substring(0, len - numFills) + fillinString);  
  153.       else  
  154.         return (fillinString + in.substring(slen - len + numFills, slen));  
  155.     }  
  156.   
  157.     // *-- pad string if length is less than required length DatabaseEntry  
  158.     // dbe = dbt.getNextKey(); String dbkey = new String (dbe.getData());  
  159.     StringBuffer sb = new StringBuffer();  
  160.     if (right)  
  161.       sb.append(in);  
  162.     sb.append(fillinString);  
  163.     if (!right)  
  164.       sb.append(in);  
  165.     return (sb.toString());  
  166.   }  
  167.   
  168.   /** 
  169.    * Remove ampersand strings such as \  
  170.    *  
  171.    * @param in 
  172.    *          Text string extracted from Web pages 
  173.    * @return String Text string without ampersand strings 
  174.    */  
  175.   public static String removeAmpersandStrings(String in) {  
  176.     if (in.length() > STRING_TOOLS_LIMIT)  
  177.       return (in);  
  178.     Matcher matcher = removeAmpersandPattern.matcher(in);  
  179.     return (matcher.replaceAll(""));  
  180.   }  
  181.   
  182.   /** 
  183.    * Escape back slashes 
  184.    *  
  185.    * @param in 
  186.    *          Text to be escaped 
  187.    * @return String Escaped test 
  188.    */  
  189.   public static String escapeText(String in) {  
  190.     StringBuffer sb = new StringBuffer();  
  191.     for (int i = 0; i < in.length(); i++) {  
  192.       char ch = in.charAt(i);  
  193.       if (ch == '\\')  
  194.         sb.append("\\\\");  
  195.       else  
  196.         sb.append(ch);  
  197.     }  
  198.     return (sb.toString());  
  199.   }  
  200.   
  201.   /** 
  202.    * Get the SHA signature of a string 
  203.    *  
  204.    * @param in 
  205.    *          String 
  206.    * @return String SHA signature of in 
  207.    */  
  208.   public static String shaDigest(String in) {  
  209.     StringBuffer out = new StringBuffer();  
  210.     if ((in == null) || (in.length() == 0))  
  211.       return ("");  
  212.     try {  
  213.       // *-- create a message digest instance and compute the hash  
  214.       // byte array  
  215.       MessageDigest md = MessageDigest.getInstance("SHA-1");  
  216.       md.reset();  
  217.       md.update(in.getBytes());  
  218.       byte[] hash = md.digest();  
  219.   
  220.       // *--- Convert the hash byte array to hexadecimal format, pad  
  221.       // hex chars with leading zeroes  
  222.       // *--- to get a signature of consistent length (40) for all  
  223.       // strings.  
  224.       for (int i = 0; i < hash.length; i++) {  
  225.         out.append(fillin(Integer.toString(0xFF & hash[i], 16), 2false'0',  
  226.             1));  
  227.       }  
  228.     } catch (OutOfMemoryError e) {  
  229.       return ("<-------------OUT_OF_MEMORY------------>");  
  230.     } catch (NoSuchAlgorithmException e) {  
  231.       return ("<------SHA digest algorithm not found--->");  
  232.     }  
  233.   
  234.     return (out.toString());  
  235.   }  
  236.   
  237.   /** 
  238.    * Return the string with the first letter upper cased 
  239.    *  
  240.    * @param in 
  241.    * @return String 
  242.    */  
  243.   public static String firstLetterUC(String in) {  
  244.     if ((in == null) || (in.length() == 0))  
  245.       return ("");  
  246.     String out = in.toLowerCase(LOCALE);  
  247.     String part1 = out.substring(01);  
  248.     String part2 = out.substring(1, in.length());  
  249.     return (part1.toUpperCase(LOCALE) + part2.toLowerCase(LOCALE));  
  250.   }  
  251.   
  252.   /** 
  253.    * Return a pattern that can be used to collapse consecutive patterns of the 
  254.    * same type 
  255.    *  
  256.    * @param entityTypes 
  257.    *          A list of entity types 
  258.    * @return Regex pattern for the entity types 
  259.    */  
  260.   public static Pattern getCollapsePattern(String[] entityTypes) {  
  261.     Pattern collapsePattern = null;  
  262.     StringBuffer collapseStr = new StringBuffer();  
  263.     for (int i = 0; i < entityTypes.length; i++) {  
  264.       collapseStr.append("(<\\/");  
  265.       collapseStr.append(entityTypes[i]);  
  266.       collapseStr.append(">\\s+");  
  267.       collapseStr.append("<");  
  268.       collapseStr.append(entityTypes[i]);  
  269.       collapseStr.append(">)|");  
  270.     }  
  271.     collapsePattern = Pattern.compile(collapseStr.toString().substring(0,  
  272.         collapseStr.length() - 1));  
  273.     return (collapsePattern);  
  274.   }  
  275.   
  276.   /** 
  277.    * return a double that indicates the degree of similarity between two strings 
  278.    * Use the Jaccard similarity, i.e. the ratio of A intersection B to A union B 
  279.    *  
  280.    * @param first 
  281.    *          string 
  282.    * @param second 
  283.    *          string 
  284.    * @return double degreee of similarity 
  285.    */  
  286.   public static double stringSimilarity(String first, String second) {  
  287.     if ((first == null) || (second == null))  
  288.       return (0.0);  
  289.     String[] a = first.split("\\s+");  
  290.     String[] b = second.split("\\s+");  
  291.   
  292.     // *-- compute a union b  
  293.     HashSet<String> aUnionb = new HashSet<String>();  
  294.     HashSet<String> aTokens = new HashSet<String>();  
  295.     HashSet<String> bTokens = new HashSet<String>();  
  296.     for (int i = 0; i < a.length; i++) {  
  297.       aUnionb.add(a[i]);  
  298.       aTokens.add(a[i]);  
  299.     }  
  300.     for (int i = 0; i < b.length; i++) {  
  301.       aUnionb.add(b[i]);  
  302.       bTokens.add(b[i]);  
  303.     }  
  304.     int sizeAunionB = aUnionb.size();  
  305.   
  306.     // *-- compute a intersect b  
  307.     Iterator <String> iter = aUnionb.iterator();  
  308.     int sizeAinterB = 0;  
  309.     while (iter != null && iter.hasNext()) {  
  310.       String token = (String) iter.next();  
  311.       if (aTokens.contains(token) && bTokens.contains(token))  
  312.         sizeAinterB++;  
  313.     }  
  314.     return ((sizeAunionB > 0) ? (sizeAinterB + 0.0) / sizeAunionB : 0.0);  
  315.   }  
  316.   
  317.   /** 
  318.    * Return the edit distance between the two strings 
  319.    *  
  320.    * @param s1 
  321.    * @param s2 
  322.    * @return double 
  323.    */  
  324.   public static double editDistance(String s1, String s2) {  
  325.     if ((s1.length() == 0) || (s2.length() == 0))  
  326.       return (0.0);  
  327.     return EditDistance.editDistance(s1.subSequence(0, s1.length()), s2  
  328.         .subSequence(0, s2.length()), false);  
  329.   }  
  330.   
  331.   /** 
  332.    * Return a string with the contents from the passed reader 
  333.    *  
  334.    * @param r Reader 
  335.    * @return String 
  336.    */  
  337.   public static String readerToString(Reader r) {  
  338.     int charValue;  
  339.     StringBuffer sb = new StringBuffer(1024);  
  340.     try {  
  341.       while ((charValue = r.read()) != -1)  
  342.         sb.append((char) charValue);  
  343.     } catch (IOException ie) {  
  344.       sb.setLength(0);  
  345.     }  
  346.     return (sb.toString());  
  347.   }  
  348.   
  349.   /** 
  350.    * Clean up a sentence by consecutive non-alphanumeric chars with a single 
  351.    * non-alphanumeric char 
  352.    *  
  353.    * @param in Array of chars 
  354.    * @return String 
  355.    */  
  356.   public static String cleanString(char[] in) {  
  357.     int len = in.length;  
  358.     boolean prevOK = true;  
  359.     for (int i = 0; i < len; i++) {  
  360.       if (Character.isLetterOrDigit(in[i]) || Character.isWhitespace(in[i]))  
  361.         prevOK = true;  
  362.       else {  
  363.         if (!prevOK)  
  364.           in[i] = ' ';  
  365.         prevOK = false;  
  366.       }  
  367.     }  
  368.     return (new String(in));  
  369.   }  
  370.   
  371.   /** 
  372.    * Return a clean file name 
  373.    *  
  374.    * @param filename 
  375.    * @return String 
  376.    */  
  377.   public static String parseFile(String filename) {  
  378.     return (filterChars(filename, "\\/_:."));  
  379.   }  
  380. }  
  381. 地址:http://blog.csdn.net/laigood/article/details/6680201
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值