java-定义一个词的全文倒排索引
java- Full text inverted index defining a word
我正在研究一个简单的全文倒排索引,试图建立一个我从 PDF 文件中提取的单词的索引。我正在使用 PDFBox 库来实现这一点。
但是,我想知道如何将词的定义定义为 index.The,我的索引工作方式是用 space 定义每个词是一个词标记。例如,
This string, is a code.
在这种情况下:索引 table 将包含
This
string,
is
a
code.
这里的缺陷是 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);
}
}
}
如果您想删除所有标点符号,您可以这样做:
for(String word : words) {
uniqueWords.add(word.replaceAll("[.,!?]", ""));
}
这将替换所有句点、逗号、感叹号和问号。
如果您还想去掉引号,您可以这样做:
uniqueWords.add(word.replaceAll("[.,?!\"]", "")
是的。您可以使用 replaceAll 方法来删除非单词字符,如下所示:
uniqueWords.add(word.replaceAll("([\W]+$)|(^[\W]+)", ""));
我正在研究一个简单的全文倒排索引,试图建立一个我从 PDF 文件中提取的单词的索引。我正在使用 PDFBox 库来实现这一点。
但是,我想知道如何将词的定义定义为 index.The,我的索引工作方式是用 space 定义每个词是一个词标记。例如,
This string, is a code.
在这种情况下:索引 table 将包含
This
string,
is
a
code.
这里的缺陷是 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);
}
}
}
如果您想删除所有标点符号,您可以这样做:
for(String word : words) {
uniqueWords.add(word.replaceAll("[.,!?]", ""));
}
这将替换所有句点、逗号、感叹号和问号。
如果您还想去掉引号,您可以这样做:
uniqueWords.add(word.replaceAll("[.,?!\"]", "")
是的。您可以使用 replaceAll 方法来删除非单词字符,如下所示:
uniqueWords.add(word.replaceAll("([\W]+$)|(^[\W]+)", ""));