识别文本中的正面和负面单词

时间:2012-05-19 07:22:58

标签: java java.util.scanner

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

在找到正面和负面词语的数量之后,我试图让它们统计每一个,然后告诉我是否有更多的正面或负面词语。

到目前为止,我有下面的代码,它只给了我一些积极的话。在这个阶段,我并没有像NLP这样的东西看到更基本的东西。

我想我有第二部分在错误的位置寻找负面词。而且我认为我需要使用布尔值来告诉我是否找到了更多的正面或负面的单词,但我无法解决该怎么做。

我很困惑,因为我不熟悉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;
        }
}

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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"));
        }

private static Map<String, Integer> readWordFile(String string) {

        return null;
    }

String[] goodWordsHolder = new String[3];{

goodWordsHolder[0] = "good"; goodWordsHolder[1] = "great";goodWordsHolder[2] = "excellent";

for(int iteration = 0; iteration < goodWordsHolder.length; iteration++) { String currentWordInText;
if(goodWordsHolder[iteration] == currentWordInText) { }// The word is a bad word } }

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("-----------------------------------------------");
        }
}

答案 2 :(得分:0)

package omgilisearch;

import java.io.*;

   public class SentimentTest {     

public static void main(String[] args) {

        String[] lines = new String[0];         
    String path = "ConversationTest.txt";         
    BufferedReader br = null;      
    try {

             File file = new File(path);

        br = new BufferedReader(                  
             new InputStreamReader(                  
             new FileInputStream(file)));             

    String line;             
    while( (line = br.readLine()) != null ) {                 

    lines = add(line, lines);

             }             

    br.close(); 

      } catch(IOException e) {             

    System.out.println("read error: " + e.getMessage());

         }         
    print(lines);     

    }       

    private static String[] add(String s, String[] array) { 

        String[] goodWordsHolder = new String[3];{

        }goodWordsHolder[0] = "good"; goodWordsHolder[1] = "great";goodWordsHolder[2] = "excellent";
        for(int iteration = 0; iteration < goodWordsHolder.length; iteration++) { String currentWordInText = null; if(goodWordsHolder[iteration] == currentWordInText) { }}
        return goodWordsHolder; } 

    private static void print(String[] data) {

       for(int i = 0; i < data.length; i++)             
    System.out.println(data[i]);     
} 

} 

答案 3 :(得分:0)

Arrays存储多个相同信息类型的项目,例如。 String [] badWords;。我相信你应该使用它,因为我确定你会在对话文本中找到超过1个坏词,如果没有,那么简单使用1 String例如。 String badWord;。

我不会写出所有可以使它工作的代码,我只会给你一个算法。

public class test {

// The process of picking out all the good and bad words
public static void main(String[] args) {
    // Setting up all the needed variables
        // Set up all the good words
        String[] goodWordsHolder = new String[2];
        goodWordsHolder[0] = "firstGoodWord";
        goodWordsHolder[1] = "secondGoodWord";
        // Set up all the bad words
        String[] badWordsHolder = new String[2];
        badWordsHolder[0] = "firstBadWord";
        badWordsHolder[1] = "secondBadWord";
        // Set up the counters
        int amountOfGoodWords = 0;
        int amountOfBadWords = 0;
        int currentWordInText = 0;
        // boolean that will exit the loop
        boolean ConversationEnded = false;

    while(!ConversationEnded) {
        // Compare the currentWord from the conversation with the hard coded words
        for(int iteration = 0; iteration < goodWordsHolder.length; iteration++) { 
            if(goodWordsHolder[iteration] == getWordInText(currentWordInText)) {
                amountOfGoodWords++;
            }   
        }
        for(int iteration = 0; iteration < badWordsHolder.length; iteration++) { 
            if(badWordsHolder[iteration] == getWordInText(currentWordInText)) {
                amountOfBadWords++;
            }   
        }
        // Increase the current word value so the next time we compare the next word in the conversation will be compared
        currentWordInText++;

        // Check that we haven't reached the end of the conversation
        if(endOfTheConversationHasBeenReached()) {
            // This will exit the while loop
            ConversationEnded = true;
        }
    }

    // Now print all the information to the console
    System.out.println("Amount of good Words: " + amountOfGoodWords);
    System.out.println("Amount of bad Words: " + amountOfBadWords);
    if(amountOfGoodWords > amountOfBadWords) {
        System.out.println("There are more good words than bad words.");
    }
    else {
        System.out.println("There are more bad words than good words.");
    }

}


// The method(s) you'll have to code out yourself. I suggest you read up on the web and so on to assist you with this.

private static String getWordInText(int currentWordInText) {
    // TODO Auto-generated method stub
    return null;
}

private static boolean endOfTheConversationHasBeenReached() {
    // TODO Auto-generated method stub
    return false;
}

}

如果有任何逻辑错误,请原谅。该代码尚未调试。 ;)希望这会引导您走向正确的方向。