【发布时间】:2020-10-13 15:01:35
【问题描述】:
我正在编写这段代码来从我的文本中删除停用词。
问题 - 此代码非常适合删除停用词,但是当我的文本中出现像 ant、ide 这样的词时,问题就出现了,因为它删除了 ant 和 ide 这两个词,因为 ant 存在于 important、want 和 ide 中在里面。但我不想将单词拆分成一个字母来删除停用词。
String sCurrentLine;
List<String> stopWordsofwordnet=new ArrayList<>();
FileReader fr=new FileReader("G:\\stopwords.txt");
BufferedReader br= new BufferedReader(fr);
while ((sCurrentLine = br.readLine()) != null)
{
stopWordsofwordnet.add(sCurrentLine);
}
//out.println("<br>"+stopWordsofwordnet);
List<String> wordsList = new ArrayList<>();
String text = request.getParameter("textblock");
text=text.trim().replaceAll("[\\s,;]+", " ");
String[] words = text.split(" ");
// wordsList.addAll(Arrays.asList(words));
for (String word : words) {
wordsList.add(word);
}
out.println("<br>");
//remove stop words here from the temp list
for (int i = 0; i < wordsList.size(); i++)
{
// get the item as string
for (int j = 0; j < stopWordsofwordnet.size(); j++)
{
if (stopWordsofwordnet.get(j).contains(wordsList.get(i).toLowerCase()))
{
out.println(wordsList.get(i)+" ");
wordsList.remove(i);
i--;
break;
}
}
}
out.println("<br>");
for (String str : wordsList) {
out.print(str+" ");
}
【问题讨论】:
-
如果你不想停用词
"ant"删除词"want",你为什么使用contains()? -
okayyy.. 那我该怎么写呢?请告诉
-
用
equals替换contains -
好的..谢谢你的帮助
标签: java servlets nlp stanford-nlp