繁体   English   中英

在Java中标记字符串后删除停用词

[英]removing stop words after tokenizing string in java

我想在标记字符串后删除停用词。 我有外部文件.txt并阅读,然后将其与标记化字符串进行比较。 如果标记词与停用词相等,则将其删除。

这是令牌化的代码

try{
            while ((msg =readBufferData.readLine()) != null) {
                int numberOfTokens;

                System.out.println("Before: "+msg);
                StringTokenizer tokens = new StringTokenizer(msg);

                numberOfTokens = tokens.countTokens();
                System.out.println("Tokens: "+numberOfTokens);

                System.out.print("After : ");
                while (tokens.hasMoreTokens()) {
                    msg = tokens.nextToken();
                    String msgLower = msg.toLowerCase();
                    String punctuationremove = punctuationRemover(msgLower);  
          //          buffWriter.write(punctuationremove+" "); --> write into file .txt
                    System.out.print(punctuationremove+" ");
                    removingStopWord(punctuationremove, readStopWordsFile());
                    numberOfTotalTokens++;   
                }
           //     buffWriter.newLine(); make a new line after tokening new message
                System.out.println("\n");
                numberOfMessages++;
            }
        // write close    buffWriter.close();
            System.out.println("Total Tokens: "+numberOfTotalTokens);
            System.out.println("Total Messages: "+numberOfMessages);
        }
        catch (Exception e){
            System.out.println("Error Exception: "+e.getMessage());
        } 

然后我有一个读取停用词文件的代码

public static Set<String> readStopWordsFile() throws FileNotFoundException, IOException{
    String fileStopWords = "\\stopWords.txt";

    Set<String> stopWords = new LinkedHashSet<String>();
    FileReader readFileStopWord = new FileReader(fileStopWords);
    BufferedReader stopWordsFile = new BufferedReader(readFileStopWord);

    String line;

    while((line = stopWordsFile.readLine())!=null){
        line = line.trim();
        stopWords.add(line);
    }
    stopWordsFile.close();
    return stopWords;
}

如何比较令牌与停用词集并删除与停用词相同的令牌。 你能帮我吗,谢谢

您可以简单地先阅读停用词,然后检查您的令牌是否为停用词。

Set<String> stopWords = readStopWordsFile();

  // some file reading logic
  while (tokens.hasMoreTokens()) {
       msg = tokens.nextToken();
       if(stopWords.contains(msg)){
         continue; // skip over a stopword token
       }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM