2017-12-31 32 views
1

私は非常に基本的な感情分析プログラムで、単語を単語の配列に分割し、各単語を3つのファイル(陽性、陰性、および中性)各感情の言葉、私は平均スコアを表示したい。私は1〜10のスケールで文を評価したい(10人は幸せ、0人は悲しい)。私は中立点である5でスコアを初期化しました。javaを使用してテキストファイル内の文字列を検索する問題

私がプログラムを実行すると、結果はスコアとして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

あなたの問題の目的では、私は感情的なミックスを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 
+0

がためにどうもありがとうございました答え、私はちょうどファイルパスであるコードを変更しました。私はそれを "F:\\ words_score.txt"にし、出力は再び5です。私が何を入力しても、答えは常に5です。私はNETBEANSでプロジェクトを実行しています。何が問題なの? –

+0

bro、正直言って、私はあなたのコードを見ていませんでしたが、最初のテストでもテストの部分で0が得られました。私はそれは単にgetScoreメソッドの結果がスコア= getScore(word)スコア+ = getScore(単語)に修正したので、コードをちょうど時間通りに書くことができたので、もう一度あなたにリクエストして、その番号をクリックするだけで答えになるあなたのために、他の人にも役立ちます。 – ArifMustafa

関連する問題