我正在研究一个简单的全文倒排索引,试图建立一个从 PDF 文件中提取的单词索引。我正在使用 PDFBox 库来实现这一点。但是,我想知道如何定义要索引的单词的定义。我的索引工作方式是将每个带有空格的单词定义为单词标记。例如,This string, is a code.在这种情况下:索引表将包含Thisstring,isacode.这里的缺陷是 like string,,它带有一个逗号,我认为string它就足够了,因为没有人搜索string,或code.回到我的问题,是否有一个特定的规则可以用来定义我的单词令牌,以防止我所拥有的这种问题?代码:File folder = new File("D:\\PDF1");File[] listOfFiles = folder.listFiles();for (File file : listOfFiles) { if (file.isFile()) { HashSet<String> uniqueWords = new HashSet<>(); String path = "D:\\PDF1\\" + file.getName(); try (PDDocument document = PDDocument.load(new File(path))) { if (!document.isEncrypted()) { PDFTextStripper tStripper = new PDFTextStripper(); String pdfFileInText = tStripper.getText(document); String lines[] = pdfFileInText.split("\\r?\\n"); for(String line : lines) { String[] words = line.split(" "); for (String word : words) { uniqueWords.add(word); } } } } catch (IOException e) { System.err.println("Exception while trying to read pdf document - " + e); } }}
2 回答

三国纷争
TA贡献1804条经验 获得超7个赞
如果您想删除所有标点符号,您可以这样做:
for(String word : words) {
uniqueWords.add(word.replaceAll("[.,!?]", ""));
}
它将替换所有句点、逗号、感叹号和问号。
如果您还想摆脱引号,您可以这样做:
uniqueWords.add(word.replaceAll("[.,?!\"]", "")

慕的地6264312
TA贡献1817条经验 获得超6个赞
是的。您可以使用 replaceAll 方法来摆脱非单词字符,如下所示:
uniqueWords.add(word.replaceAll("([\\W]+$)|(^[\\W]+)", ""));
添加回答
举报
0/150
提交
取消