【发布时间】:2018-10-10 02:50:59
【问题描述】:
我正在尝试从 PDF 中提取所有文本并将其存储在 HashSet 中。据我所知, HashSet 不包含重复项,因此当我提取它们时它会忽略重复项。但是,当我打印出哈希结果时,我注意到其中有重复的空格。
我想将哈希值插入到我在 MySQL 中的表中,但它有一个主键约束,这给我带来了一些麻烦。 有没有办法完全删除哈希中的各种重复项?
我提取文本的代码:
public static void main(String[] args) throws Exception {
String path ="D:/PDF/searchable.pdf";
HashSet<String> uniqueWords = new HashSet<>();
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);
}
}
System.out.println(uniqueWords);
}
} catch (IOException e){
System.err.println("Exception while trying to read pdf document - " + e);
}
Object[] words = uniqueWords.toArray();
System.out.println(words[1].toString());
MysqlAccess connection=new MysqlAccess();
for(int i = 1 ; i <= words.length - 1 ; i++ ) {
connection.readDataBase(path, words[i].toString());
}
System.out.println("Completed");
}
}
这是我的哈希:
[, highlight, of, Even, copy, file,, or, ., ,, 1, reader,, different, D, F, ll, link, ea, This, ed, document, V, P, ability, regardless, g, d, text., e, b, a, n, o, web, l, footnote., should, Most, IDRH, selection, text-searchable, positioning, u, s, what, r, PDF., happens, er, y, x, to, body, single, ca, te, together, ti, th, would, when, be, Text-Searchable, document,, text, isn't, such, kinds, sh, co, ld, font,, example, ch, this, attempt, have, t,, Notice,, contained, from, re, text.1, page,, style, page., able, if, is, You, standard, PDF, your, as, readers, you, the, in, main, an, iz]
如果它们是唯一的,为什么当我尝试插入主键列时会抛出 " Duplicate entry for key PRIMARY"?
任何建议都将不胜感激。
【问题讨论】:
-
显然它们不是同一个字符串。
-
您的输入可能还包含
,、空格、制表符等内容。 -
reader,可以是一个词吗?你似乎不会处理标点符号。 -
您的数据库可能有不同的唯一性概念。例如,它可能会将
foo和FOO视为相同的值。错误消息应该准确地告诉您失败的地方。 -
您可以使用不区分大小写的集合:
new TreeSet<>(String.CASE_INSENSITIVE_ORDER)
标签: java