【发布时间】:2019-07-02 11:14:14
【问题描述】:
我必须从 txt 文件中获取一些 StopWords 并将它们从文本中删除。 我使用此方法从文件中获取 StopWords,将它们保存在字符串数组中并返回:
public String[] loadStopwords(File targetFile, String[] stopWords) throws IOException {
File fileTo = new File(targetFile.toString());
BufferedReader br;
List<String> lines = new ArrayList<String>();
try {
br = new BufferedReader(new FileReader(fileTo));
String st;
while((st=br.readLine()) != null){
lines.add(st);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stopWords = lines.toArray(new String[]{});
return stopWords;
}
然后,我通过 StopWords[] 和要在其中更新的文本:
public void removeStopWords(String targetText, String[] stopwords) {
targetText = targetText.toLowerCase().trim();
ArrayList<String> wordList = new ArrayList<>();
wordList.addAll(Arrays.asList(targetText.split(" ")));
List<String> stopWordsList = new ArrayList<>();
stopWordsList.addAll(Arrays.asList(stopwords));
wordList.removeAll(stopWordsList);
}
但是没有从 wordList 中删除。为什么?
【问题讨论】:
-
读取文件时为什么不删除单词?
-
您已将 targetText 字符串转换为小写,请确保 stopWords 也是小写。
-
我试过了。一样的
-
@Shail016 他们是小写的
-
您如何以及在何处检查 wordList?因为这种方法应该可以正常工作。我建议您也打印 'wordList' 和 'stopWordList' .. 这样你就可以确保你得到了预期的一切。