2

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

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

到目前为止,我有下面的代码,它只让我对积极的词进行计数。在这个阶段,我并不是在看 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

4 回答 4

1

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

于 2012-05-19T08:12:47.813 回答
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("-----------------------------------------------");
        }
}
于 2012-05-19T23:31:40.553 回答
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]);     
} 

} 
于 2012-05-20T00:30:35.627 回答
0

数组存储多个相同信息类型的项目,例如。字符串 [] 坏词;。我相信你应该使用它,因为我相信你会在对话文本中找到超过 1 个你想找到的坏词,如果没有,那么简单地使用 1 个字符串,例如。字符串坏词;。

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

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

}

如有逻辑错误,请见谅。代码尚未调试。;) 希望这将引导您走向正确的方向。

于 2012-05-20T09:09:58.670 回答