java - 识别文本中的正面和负面词语

标签 java java.util.scanner

我正在尝试找出如何扫描对话的文本文件,找出有多少正面词和负面词。正面和负面词包含在两个单独的文本文件中,用于“扫描”对话文本文件。

在它找到正面和负面词的数量后,我试图让它对每个词进行统计,然后告诉我是否找到了更多正面或负面词。

到目前为止,我有下面的代码,它只给我一个正面词的计数。在这个阶段,我关注的不是像 NLP 这样的东西,而是更基础的东西。

我想我有第二部分在错误的位置寻找否定词。而且我想我需要使用 boolean 值来告诉我是否找到了更多正面或负面的词,但我不知道该怎么做。

我很困惑,因为我是 Java 和一般编程的新手。

如有任何帮助,我们将不胜感激。

package omgilisearch;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;

public class SentimentTest {

    public static void main(String[] args) throws Exception {
          printAllCounts(
            readWordFile("ConversationTest.txt", loadKeywords("PositiveWords.txt")));
        }
    public static void main1(String[] args) throws Exception {
          printAllCounts(
            readWordFile("ConversationTest.txt", loadKeywords("NegativeWords.txt")));
        }

        private static Map<String, Integer> readWordFile(
          String fname, Set<String> keywords) throws FileNotFoundException
        {
          final Map<String, Integer> frequencyData = new TreeMap<String, Integer>();
          for (Scanner wordFile = new Scanner(new FileReader(fname)); 
            wordFile.hasNext();) 
          {
            final String word = wordFile.next();
            if (keywords.contains(word)) 
              frequencyData.put(word, getCount(word, frequencyData) + 1);
          }
          return frequencyData;
        }


        private static void printAllCounts(Map<String, Integer> frequencyData) {
          System.out.println("-----------------------------------------------");
          System.out.println(" Occurrences Word");
          for(Map.Entry<String, Integer> e : frequencyData.entrySet())
            System.out.printf("%15d %s\n", e.getValue(), e.getKey());
          System.out.println("-----------------------------------------------");
        }

        private static int getCount(String word, Map<String, Integer> frequencyData) {
            return frequencyData.containsKey(word)? frequencyData.get(word) : 0;
        }

        private static Set<String> loadKeywords(String fname) 
        throws FileNotFoundException 
        {
          final Set<String> result = new HashSet<String>();
          for (Scanner s = new Scanner(new FileReader(fname)); s.hasNext();) 
            result.add(s.next());
          return result;
        }
}

最佳答案

您必须有一些所谓的“坏”词数组(它们是硬编码的),然后遍历整个文本文件并将数组中的每个词与您当前检查的词进行比较。如果单词与数组中的单词之一匹配,则增加一些保存坏词数量的变量,例如。坏词++;。我相信这种方法应该有效。

关于java - 识别文本中的正面和负面词语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10663220/

相关文章:

java - 扫描仪在使用 next() 或 nextFoo() 后跳过 nextLine()?

java - 在 switch-case 中切换到默认情况

Java,泛型不起作用

java - 使用 Comparable 比较通用变量

java - 在扫描仪中返回包含 char c 的字符串

java - 在 csv 上使用 Scanner,每次调用方法时迭代

java - 将 HQL 子查询转换为 Criteria

java - 具体的Java继承查询 - 需要建议

java - 扫描仪在使用 next() 或 nextFoo() 后跳过 nextLine()?

java - 计算字符串中的行数和单词数