我试图使用 Java ScannerhasNext
方法,但得到了奇怪的结果。也许我的问题很明显,但是为什么这个简单的简单表达式"[a-zA-Z']+"
不适用于这样的词:“points.anything, supervisor,”。我也试过这个"[\\w']+"
。
public HashMap<String, Integer> getDocumentWordStructureFromPath(File file) {
HashMap<String, Integer> dictionary = new HashMap<>();
try {
Scanner lineScanner = new Scanner(file);
while (lineScanner.hasNextLine()) {
Scanner scanner = new Scanner(lineScanner.nextLine());
while (scanner.hasNext("[\\w']+")) {
String word = scanner.next().toLowerCase();
if (word.length() > 2) {
int count = dictionary.containsKey(word) ? dictionary.get(word).intValue() + 1 : 1;
dictionary.put(word, new Integer(count));
}
}
scanner.close();
}
//scanner.useDelimiter(DELIMITER);
lineScanner.close();
return dictionary;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}