当前位置:   article > 正文

java分词统计单词(简单基于字典)_java分词的字典

java分词的字典
最近帮他们做一个java分词的系统,没有用很复杂的方法实现。主要是把大小写和复数还原成单数等

其中主要的是做一个字典的匹配,把字典中出现的词组作为一个单词划分。

这里没有用到比较高升的算法,本来可以用个KMP的,但是减小开发时间就用了一个简单的算法

  1. public static Vector<String> DATA=new Vector<String>();
  2. public static Vector<String> divide(String s,String delim)//第一个是字符串,第二个是分隔符
  3. {
  4. s=RemoveChar(s,',');//去除标点符号
  5. s=RemoveChar(s,'.');//去除标点符号
  6. s=s.toLowerCase();
  7. Vector<String> word=new Vector<String>();
  8. StringTokenizer st;
  9. if(delim.isEmpty())
  10. {
  11. st= new StringTokenizer(s);
  12. }
  13. else
  14. {
  15. st= new StringTokenizer(s,delim);
  16. }
  17. while(st.hasMoreElements())
  18. {
  19. String token=st.nextToken();//获取下一个单词
  20. token=ToSingle(token);//转换为单数
  21. word.add(token);
  22. }
  23. return word;
  24. }
  25. //使用领域词典进行分词
  26. public static Vector<String> dividewithdict(String s,String delim)
  27. {
  28. int deep=3;//表示字典中的每一项最大单词数是多少
  29. if(DATA.size()==0)//没有加载字典时初始化它
  30. {
  31. loadDict("D:\\dic.txt");//加载字典文件
  32. }
  33. Vector<String> word=divide(s,delim);
  34. String tmp="";
  35. Vector<String> nword=new Vector<String>();
  36. boolean flag=true;
  37. boolean iswrap=true;
  38. for(int i=0;i<word.size();)
  39. {
  40. for(int j=i;j<(i+deep>word.size()?word.size():i+deep);j++)
  41. {
  42. tmp+=" "+word.get(j);
  43. if(iswrap)
  44. {
  45. tmp=tmp.substring(1,tmp.length());
  46. iswrap=false;
  47. }
  48. if(DATA.contains(tmp))
  49. {
  50. //nword.add(tmp);
  51. flag=false;
  52. i+=j-i+1;
  53. break;
  54. }
  55. }
  56. if(flag)
  57. {
  58. nword.add(word.get(i));
  59. i++;
  60. }
  61. tmp="";
  62. iswrap=true;
  63. flag=true;
  64. }
  65. return nword;
  66. }
  67. //转为小写
  68. public static String ChangeToLower(String s)
  69. {
  70. return s.toLowerCase();
  71. }
  72. //移除多余符号
  73. public static String RemoveChar(String s,char token)
  74. {
  75. return s.replace(token, ' ');
  76. }
  77. //将复数单词转为单数单词
  78. public static String ChangeDoubleToOne(String token)
  79. {
  80. if (token.equalsIgnoreCase("feet"))
  81. {
  82. token = "foot";
  83. }
  84. else if (token.equalsIgnoreCase("geese"))
  85. {
  86. token = "goose";
  87. }
  88. else if (token.equalsIgnoreCase("lice"))
  89. {
  90. token = "louse";
  91. }
  92. else if (token.equalsIgnoreCase("mice"))
  93. {
  94. token = "mouse";
  95. }
  96. else if (token.equalsIgnoreCase("teeth"))
  97. {
  98. token = "tooth";
  99. }
  100. else if (token.equalsIgnoreCase("oxen"))
  101. {
  102. token = "ox";
  103. }
  104. else if (token.equalsIgnoreCase("children"))
  105. {
  106. token = "child";
  107. }
  108. else if (token.endsWith("men"))
  109. {
  110. token = token.substring(0, token.length() - 3)+"man";
  111. }
  112. else if (token.endsWith("ies")) {
  113. token = token.substring(0, token.length() - 3)+"y";
  114. }
  115. else if (token.endsWith("ves"))
  116. {
  117. if (token.equalsIgnoreCase("knives")|| token.equalsIgnoreCase("wives")|| token.equalsIgnoreCase("lives"))
  118. {
  119. token = token.substring(0, token.length() - 3)+"fe";
  120. }
  121. else
  122. {
  123. token = token.substring(0, token.length() - 3)+"f";
  124. }
  125. }
  126. else if (token.endsWith("oes") || token.endsWith("ches")|| token.endsWith("shes") || token.endsWith("ses")|| token.endsWith("xes"))
  127. {
  128. token = token.substring(0, token.length() - 2);
  129. }
  130. else if (token.endsWith("s"))
  131. {
  132. token = token.substring(0, token.length() - 1);
  133. }
  134. return token;
  135. }
  136. //另外一种转为单数的方法
  137. public static String ToSingle(String token)
  138. {
  139. return Inflector.getInstance().singularize(token);
  140. }
  141. public static void loadDict(String path)
  142. {
  143. try {
  144. File file=new File(path);
  145. if(file.isFile() && file.exists())
  146. {
  147. InputStreamReader read = new InputStreamReader(new FileInputStream(file));
  148. BufferedReader bufferedReader = new BufferedReader(read);
  149. String lineTxt = null;
  150. while((lineTxt = bufferedReader.readLine()) != null){
  151. DATA.add(lineTxt.trim());
  152. }
  153. read.close();
  154. }
  155. } catch (Exception e) {
  156. e.printStackTrace();
  157. // TODO: handle exception
  158. }
  159. }


 

 

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/935871
推荐阅读
相关标签
  

闽ICP备14008679号