我使用breakIterator从一个句子中获取每个单词,当像“我的婆婆来拜访”这样的句子出现问题时,我无法将婆婆作为一个单词。
BreakIterator iterator = BreakIterator.getWordInstance(Locale.ENGLISH);
for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator.next())
{
String possibleWord = sentence.substring(start, end);
if (Character.isLetterOrDigit(possibleWord.charAt(0)))
{
// grab the word
}
}发布于 2015-02-14 14:39:07
正如我在您的代码中看到的,您要做的是检查每个单词中的第一个字符是字符还是数字。每次使用BreakIterator.getWordInstance()时,您总是会获得所有单词,这取决于区域设置的边界规则,并且在我知道之前,使用这个类实现您想要做的事情有点困难,所以我的建议是:
String text = "my mother-in-law is coming for a visit";
String[] words = text.split(" ");
for (String word : words){
if (Character.isLetterOrDigit(word.charAt(0))){
// grab the word
}
}https://stackoverflow.com/questions/28512064
复制相似问题