我正在研究一个简单的全文倒排索引,试图建立一个从 PDF 文件中提取的单词索引。我正在使用 PDFBox 库来实现这一点。
但是,我想知道如何定义要索引的单词的定义。我的索引工作方式是将每个带有空格的单词定义为单词标记。例如,
This string, is a code.
在这种情况下:索引表将包含
This
string,
is
a
code.
这里的缺陷是 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);
}
}
}
三国纷争
慕的地6264312
相关分类