我想检查句子形成的质量。具体来说,我想看看最终用户是否在标点符号后键入空格。 我对NLP库或简单的java正则表达式解决方案也没问题。
例如:
句子1是完美的,句子2是坏的,因为它后面有一个没有空格的标点符号,而句子3是最差的,因为没有一个标点用空格成功。
你能建议一个java方法吗? 我尝试了语言工具API,但没有用。
答案 0 :(得分:1)
为什么不尝试使用模式和Unicode类别?
例如:
Pattern pattern = Pattern.compile("\\p{P} ");
Matcher matcher = pattern.matcher("Hi, my name is Tom Cruise. I like movies");
while (matcher.find()) {
System.out.println(matcher.group());
}
此处的模式搜索任何标点符号后跟空格。 输出将是:
,
.
(注意逗号和点后面的空格)
您可以通过指定哪些准确的标点符号有资格后跟空格来优化您的模式。
最后,为了检查相反的情况(标点符号不后跟空格):
Pattern otherPattern = Pattern.compile("\\p{P}\\S");
答案 1 :(得分:1)
Pattern pattern = Pattern.compile("\\p{P}\\S");
String[] tests = new String[] {
"Hi, my name is Tom Cruise. I like movies",
"Hi,my name is Tom Cruise. I like movies",
"Hi,my name is Tom Cruise.I like movies"
};
int[] results = new int[] { 0, 0, 0 };
for (int i = 0; i < tests.length; i++) {
Matcher matcher = pattern.matcher(tests[i]);
while(matcher.find()) {
results[i] += 1;
}
if (results[i] == 0) {
System.out.println("Sentence " + (i + 1) + " is perfect");
} else if (results[i] > 1 && results[i] < 3) {
System.out.println("Sentence " + (i + 1) + " is good");
} else {
System.out.println("Sentence " + (i + 1) + " is bad");
}
}
// now you know how many violations there were on every line.
// do whatever you want with them.