[英]Sentence formation: Punctuation checks in java
我想檢查句子結構的質量。 具體來說,我想看看最終用戶是否在標點符號后輸入空格。 我可以使用NLP庫或簡單的Java regex解決方案。
例如:
句子1是完美的,句子2是較差的,因為它后面有1個標點,后面沒有空格,而句子3是最差的,因為所有標點都沒有空格。
您能建議使用Java方法嗎? 我嘗試了languagetool API,但是沒有用。
您為什么不嘗試使用模式和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");
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.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.