gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:21:32 26 4
gpt4 key购买 nike

我正在开发一个非常基本的情感分析程序,该程序将句子分成单词数组,并根据每个情感的单词数量在三个文件(正面、负面和中性)中搜索每个单词,我想显示平均分。我想对这句话进行 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;
}

}

感谢任何帮助。

最佳答案

例如,针对您的问题,我在单个文件中提供情感组合,您不必将单词与中性、积极和消极相匹配就足够了,但如果您愿意,您可以计算那些中性单词,好吧,让我们看一下例子。

首先,包含文件的分数是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

关于java - 使用java在文本文件中查找字符串的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48041727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com