据我了解,您希望删除重复项(系统字典中已存在)。不过,您可能想先问一下,这是否真的有必要。我想它们不会造成任何问题,也不会过度增加单词拼写检查,所以在我看来,第 2 步没有真正的理由。
我认为第 1 步会让您的日子更加艰难。从 PDF 中提取纯文本可能听起来很容易,但事实并非如此。你最终会得到很多未知的符号。您需要在行尾修复拆分词,并且您可能希望排除方程式/链接/数字/等。在将所有这些添加到您的字典之前。
但是,如果您有一些工具可以完成这项工作,并且可以创建几个真正只包含您需要的单词/句子的 .txt 文件,那么我会使用类似于以下 python 代码的内容来“解决”合并仅适用于您的本地字典。当然,您也可以扩展它以加载系统字典(无论在哪里?)并按照我在下面显示的相同方式合并它。
请注意,我故意遗漏了任何错误处理。
另存为import_to_dict.py,根据您的要求调整路径并拨打python import_to_dict.py
#!/usr/bin/env python
import os,re
# 1 - load existing dictionaries from files (adjust paths here!)
dictionary_file = '~/Library/Spelling/LocalDictionary'
global_dictionary_file = '/Library/Spelling/GlobalDictionary'
txt_file_folder = '~/Documents/ConvertedPapers'
reg_exp = r'[\s,.|/]+' #add symbols here
with open(local_dictionary_file, 'r') as f:
# splitting with regular expressions shouldn't really be needed for the dictionary, but it should work
dictionary = set(re.split(reg_exp,f.read()))
with open(global_dictionary_file, 'r') as f:
# splitting with regular expressions shouldn't really be needed for the dictionary, but it should work
global_dictionary = set(re.split(reg_exp,f.read()))
# 2 - walk over all sub-dirs in your folder
for root, dirs, files in os.walk(txt_file_folder):
# open all files (this could easily be limited to only .txt files)
for file in files:
with open(os.path.join(root, file), 'r') as txt_f:
# read the file contents
words = txt_f.read()
# split into word-set (set guarantees no duplicates)
word_set = set(re.split(reg_exp,words))
# remove any already in dictionary existing words
missing_words = (word_set - dictionary) - global_dictionary
# add missing words to dictionary
dictionary |= missing_words
# 3 - write dictionary file
with open(dictionary_file, 'w') as f:
f.write('\n'.join(dictionary))