正面词被认为:“好”或“令人敬畏和消极的词被认为:”坏“或”可怕“。我想在每次使用正面或负面词时计算并将其添加到总pos / neg现在代码的问题是它无法计算重复数。我可以使用什么逻辑来有效地返回pos和neg字的统计数据?此外,我如何忽略用户输入的情况?
package practice;
import java.io.*;
public class SentimentAnalyser {
public static String analyse(String text) {
int posCount = 0;
int negCount = 0;
String positive = "good";
String pos1 = "awesome";
String negative = "bad";
String neg1 = "terrible";
if(text.contains(positive) | text.contains(pos1)){
posCount++;
}
if(text.contains(negative) | text.contains(neg1)){
negCount++;
}
String res = Integer.toString(posCount);
String res1 = Integer.toString(negCount);
String result = (posCount+"P"+negCount+"N");
return result;
}
public static void main(String arg[]) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the text with spaces(press enter to done):");
String text=br.readLine();
System.out.println("Line:\n"+analyse(text));
}
}
答案 0 :(得分:0)
您可以先将输入字符串转换为小写,以使检查不区分大小写。然后,您可以使用简单的regular expression来计算单词的匹配项:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
...
public static String analyse(String text) {
text = text.toLowerCase();
int posCount = 0;
int negCount = 0;
String positive = "good|awesome";
String negative = "bad|terrible";
Pattern positivePattern = Pattern.compile(positive);
Pattern negativePattern = Pattern.compile(negative);
Matcher matcher = positivePattern.matcher(text);
while(matcher.find()) {
posCount++;
}
matcher = negativePattern.matcher(text);
while(matcher.find()) {
negCount++;
}
String res = Integer.toString(posCount);
String res1 = Integer.toString(negCount);
String result = (posCount+"P"+negCount+"N");
return result;
}
P.S。:在原始代码中,您应该使用条件OR运算符||
而不是按位运算符|
。