使用java在文本文件中查找字符串的问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用java在文本文件中查找字符串的问题相关的知识,希望对你有一定的参考价值。
我正在研究一个非常基本的情绪分析程序,它将句子分成一个单词数组,并根据每个情绪的单词数量搜索三个文件(正面,负面和中性)中的每个单词,我想显示平均分数。我想按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(" ", -1);
if(wordNScore[0].equalsIgnoreCase(word)) {
score = Integer.parseInt(wordNScore[1]);
scanner.close();
break;
}
}
return score;
}
}
输出如下:
Score of the sentence is : 3
以上是关于使用java在文本文件中查找字符串的问题的主要内容,如果未能解决你的问题,请参考以下文章