【问题标题】:Remove StopWords in JAVA passed by File删除文件传递的 JAVA 中的停用词
【发布时间】: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' .. 这样你就可以确保你得到了预期的一切。

标签: java arrays string char


【解决方案1】:

尝试将停用词也保存为小写

public  String[] loadStopwords(String targetFile) throws IOException {
    File fileTo = new File(targetFile);
    BufferedReader br;
    List<String> lines = new ArrayList<>();
    try {
        br = new BufferedReader(new FileReader(fileTo));
        String st;
        while((st=br.readLine()) != null){
            //Adding words en lowercase and without start end blanks
            lines.add(st.toLowerCase().trim);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return lines.toArray(new String[]{});
}

public  ArrayList<String> removeStopWords(String targetText, String[] stopwords) {
    //Make the text to LowerCase also
    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);

    return wordList;
}

【讨论】:

  • 我发现了问题!如果我从文件的上下文中存储停用词,我会得到类似于 stopwords = [一二三] 的内容。相反,我必须得到停用词 = [一、二、三]。我该怎么办?
  • 你可以使用 Split 让它更容易,我想是这样的:String[] stopWordsDivided = stopwords[0].split(" ");
【解决方案2】:

爱德华多

这对我有用。但是,有几个 cmets:

  1. 您没有在 loadStopWords 方法中使用 stopWords 参数。
  2. 您没有从 removeStopWords 方法返回 wordList。

查看您的 cmets,我怀疑区别在于停用词文本文件。我的每个停用词都在新行中,而您很可能将所有停用词都放在一行中,而您没有将其分开。

【讨论】:

  • 感谢您的评论,但它对我不起作用。我在 targetText 中传递了 textArea 的上下文(使用 textArea.getText()),在停用词中传递了文件的上下文
  • 我发现了问题!如果我从文件的上下文中存储停用词,我会得到类似于 stopwords = [一二三] 的内容。相反,我必须得到停用词 = [一、二、三]。我该怎么办?
  • @EdoardoTavilla - 用可能的解释更新了我的答案。
猜你喜欢
  • 2011-03-12
  • 2016-05-21
  • 2012-09-10
  • 2021-03-21
  • 1970-01-01
  • 2010-12-12
  • 2019-01-24
  • 1970-01-01
  • 2014-06-06
相关资源
最近更新 更多