使用java在文本文件中查找字符串的问题

时间:2017-12-31 10:32:39

标签: java file-io awt

我正在研究一个非常基本的情绪分析程序,它将句子分成一个单词数组,并根据每个情绪的单词数量搜索三个文件(正面,负面和中性)中的每个单词,我想显示平均分。我想按1-10的等级评定句子(10个是快乐的,0个是悲伤的)。我已将分数初始化为5,这是一个中立分数。

当我运行程序时,结果只显示5作为分数。看起来在文件中找到字符串存在一些问题。

    b.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e)
        {
            String str = t1.getText();
            Label result;
            int score = 5;
            String[] words = str.split("\\s+");

            for (int i = 0; i < words.length; i++) {                    
                words[i] = words[i].replaceAll("[^\\w]", "");
            }

            for (int i = 0; i < words.length; i++){

                if(search_pos(words[i])){
                    score = (score + 10)/2;
                    break;
                }

                if(search_neg(words[i])){
                    score = (score + 5)/2;
                    break;
                }

                if(search_neu(words[i])){
                    score = (score - 10)/2;
                    break;
                }
            }


            result=new Label("score is " + score);
            result.setBounds(50,350, 200,30); 

            f.add(result);


        }

    });



static boolean search_pos(String s){

    Scanner scanner=new Scanner("F:\\pos-words.txt");

    boolean flag = false;

    while(scanner.hasNextLine()){
        if(s.equalsIgnoreCase(scanner.nextLine().trim())){
            // found
            flag = true;
        }
    }
    return flag;
}

static boolean search_neg(String s){

    Scanner scanner=new Scanner("F:\\neg-words.txt");

    boolean flag = false;

    while(scanner.hasNextLine()){
        if(s.equalsIgnoreCase(scanner.nextLine().trim())){
            // found
            flag = true;
        }
    }
    return flag;
}

    static boolean search_neu(String s){

    Scanner scanner=new Scanner("F:\\neu-words.txt");

    boolean flag = false;

    while(scanner.hasNextLine()){
        if(s.equalsIgnoreCase(scanner.nextLine().trim())){
            // found
            flag = true;
        }
    }
    return flag;
}

}

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

出于问题的目的,我在单个文件中提供感情组合,你不必将这些单词与中性,正面和负面相匹配就足够了,但如果你想要,你可以算一下中性词,好吧,让我们举个例子。

首先,包含文件的分数为words_score.txt

good    8
best    10
awesome 9
right   5
correct 7
outstanding 9
bad -8
worst   -10
flop    -7
wrong   -6
disgusting  -9
sucks   -8

然后使用假设类:

package swing_practice;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class TestClass {

    private static final File scoreFile = new File("/home/arif/workspace/swing_practice/src/swing_practice/words_score.txt");

    public static void main(String[] args) {        
        try{
            String str = purifySentence("I think what did that fellow does, is good for her but If I speak from that girl side, it's worst kind of thing.");
            int score = 5;
            String[] words = str.split("\\s+");

            for (int i = 0; i < words.length; i++) {
                //System.out.println("Score for the word is : " + words[i] + " - " + getScore(words[i]));
                score += getScore(words[i]);
            }

            //if you want with 3 files, just write three methods like getScore and append the score variable similarly as above

            if(score < 0)
                score = 0;
            if(score > 10)
                score = 10;

            System.out.println("Score of the sentence is : " + score);

        }catch(FileNotFoundException ioe){
            ioe.printStackTrace();
        }

    }

    private static String purifySentence(final String sentence){
        String purifiedValue = "";

        if(sentence.length() == 0){
            return "";
        }else{
            for(int i = 0; i < sentence.length(); i++){
                char ch = sentence.charAt(i);
                if(Character.isAlphabetic(ch) || ch == ' ')
                    purifiedValue += String.valueOf(ch);
            }
        }
        return purifiedValue;
    }

    private static int getScore(final String word) throws FileNotFoundException{
        int score = 0;
        final Scanner scanner = new Scanner(scoreFile);
        while (scanner.hasNextLine()) {
            final String line = scanner.nextLine();
            String[] wordNScore = line.split("\t", -1);
               if(wordNScore[0].equalsIgnoreCase(word)) {
                   score = Integer.parseInt(wordNScore[1]);
                   scanner.close();
                   break;
               }
        }
        return score;
    }

}

输出如下:

Score of the sentence is : 3