Я работаю над задачей, в которой мне нужно разбить абзац на предложения. Например, приведенный абзац:
"This is a long string with some numbers 123.456,78 or 100.000 and e.g. some abbreviations in it, which shouldn't split the sentence. Sometimes there are problems, i.e. in this one. here and abbr at the end x.y.. cool."
Мне нужно 4 предложения:
This is a long string with some numbers 123.456,78 or 100.000 and e.g. some abbreviations in it, which shouldn't split the sentence.
Sometimes there are problems, i.e. in this one.
here and abbr at the end x.y..
cool
Теперь это очень похоже на эту задачу который реализован в JavaScript.
<code>var re = /\b(\w\.\w\.)|([.?!])\s+(?=[A-Za-z])/g;
var str = 'This is a long string with some numbers 123.456,78 or 100.000 and e.g. some abbreviations in it, which shouldn\'t split the sentence. Sometimes there are problems, i.e. in this one. here and abbr at the end x.y.. cool.';
var result = str.replace(re, function(m, g1, g2){
return g1 ? g1 : g2+"\r";
});
var arr = result.split("\r");
document.body.innerHTML = "<pre>" + JSON.stringify(arr, 0, 4) + "
";
Я пытаюсь реализовать это в Java с помощью этой ссылки , но застрял как использовать функцию replace
из описанного выше snipper в моем Java коде.
public static void main(String[] args) {
String content = "This is a long string with some numbers 123.456,78 or 100.000 and e.g. some abbreviations in it, which shouldn't split the sentence. Sometimes there are problems, i.e. in this one. here and abbr at the end x.y.. cool.";
Pattern p = Pattern.compile("/\\b(\\w\\.\\w\\.)|([.?!])\\s+(?=[A-Za-z])/g");
Matcher m = p.matcher(content);
List<String> tokens = new LinkedList<String>();
while (m.find()) {
String token = m.group(1); // group 0 is always the entire match
tokens.add(token);
}
System.out.println(tokens);
}
Как сделать то же самое в Java программировании? Есть ли лучший способ, чем разделить абзац на предложения в Java для данного образца текста?