使用java在文本文件中查找字符串时出现问题
我正在开发一个非常基本的情绪分析程序,该程序将句子分成一个单词数组,并根据每个情绪的单词数在三个文件(肯定、否定和中性)中搜索每个单词,我想显示平均分数。我想用1-10分来给这个句子打分(10分是快乐的,0分是悲伤的)。我已将分数初始化为5,这是一个中性分数 当我运行程序时,结果只显示5作为分数。看起来在文件中查找字符串时出现了一些问题使用java在文本文件中查找字符串时出现问题,java,file-io,awt,Java,File Io,Awt,我正在开发一个非常基本的情绪分析程序,该程序将句子分成一个单词数组,并根据每个情绪的单词数在三个文件(肯定、否定和中性)中搜索每个单词,我想显示平均分数。我想用1-10分来给这个句子打分(10分是快乐的,0分是悲伤的)。我已将分数初始化为5,这是一个中性分数 当我运行程序时,结果只显示5作为分数。看起来在文件中查找字符串时出现了一些问题 b.addActionListener(new ActionListener(){ @Override public v
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;
}
}
b.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e)
{
字符串str=t1.getText();
标注结果;
智力得分=5分;
String[]words=str.split(\\s+);
对于(inti=0;i
感谢您的帮助。例如,在您的问题上,我在一个文件中提供了情感混合,您不必将单词与中性、积极和消极匹配,但如果您需要,您可以计算这些中性单词,好的,让我们来举个例子 首先,包含文件的分数是
words\u 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;
}
}
非常感谢您的回答,我刚刚对代码做了一个更改,即文件路径。我将其设置为“F:\\words\u score.txt”,输出值再次为5。不管我打什么,答案总是5。我正在用NETBEANS运行这个项目。有什么问题吗?老兄,老实说,我没有看你的代码,但是在第一次测试中,我在测试部分也得到了结果0,我想这只是因为getScore方法result simple放在了score=getScore(word)上,从而将其更正为score+=getScore(word),所以我发现我可以及时编写代码,我就这么做了,还有一个请求,只要点击这个数字,它就会成为你的答案,对其他人也有帮助。
Score of the sentence is : 3