【发布时间】:2015-02-25 11:03:07
【问题描述】:
我有一个包含很多单词的字符串,我有一个包含一些停用词的文本文件,我需要从我的字符串中删除这些停用词。 假设我有一个字符串
s="I love this phone, its super fast and there's so much new and cool things with jelly bean....but of recently I've seen some bugs."
删除停用词后,字符串应该是这样的:
"love phone, super fast much cool jelly bean....but recently bugs."
我已经能够做到这一点,但我面临的问题是,当字符串中有相邻的停用词时,它只删除第一个停用词,我得到的结果是:
"love phone, super fast there's much and cool with jelly bean....but recently seen bugs"
这是我的 stopwordslist.txt 文件: Stopwords
我该如何解决这个问题。这是我到目前为止所做的:
int k=0,i,j;
ArrayList<String> wordsList = new ArrayList<String>();
String sCurrentLine;
String[] stopwords = new String[2000];
try{
FileReader fr=new FileReader("F:\\stopwordslist.txt");
BufferedReader br= new BufferedReader(fr);
while ((sCurrentLine = br.readLine()) != null){
stopwords[k]=sCurrentLine;
k++;
}
String s="I love this phone, its super fast and there's so much new and cool things with jelly bean....but of recently I've seen some bugs.";
StringBuilder builder = new StringBuilder(s);
String[] words = builder.toString().split("\\s");
for (String word : words){
wordsList.add(word);
}
for(int ii = 0; ii < wordsList.size(); ii++){
for(int jj = 0; jj < k; jj++){
if(stopwords[jj].contains(wordsList.get(ii).toLowerCase())){
wordsList.remove(ii);
break;
}
}
}
for (String str : wordsList){
System.out.print(str+" ");
}
}catch(Exception ex){
System.out.println(ex);
}
【问题讨论】:
-
首先拆分字符串会有帮助吗?类似“phrase.split(delims);”的东西您可以在再次缝合之前过滤掉不需要的部分。这可能会解决您的“这个”和“他的”问题。
标签: java string stop-words