【问题标题】:How do I autoscan local documents to add words to a custom dictionary?如何自动扫描本地文档以将单词添加到自定义词典?
【发布时间】:2015-10-19 12:33:40
【问题描述】:

我希望我的字典能了解更多我使用的单词 - 并且不想在我最终输入所有可能的单词时手动添加它们(我是生物学家/生物信息学家 - 有很多行话和具体软件和物种名称)。相反,我想:

  1. 获取现有文档的目录。这些是科学文章的 PDF 或 Word/latex 文档;我想它们可以“轻松”转换为纯文本。
  2. 抽出所有不在“正常”词典中的单词。
  3. 将这些添加到我的本地自定义词典(在我的 Mac 上是 ~/Library/Spelling/LocalDictionary。但也可以将它们添加到 libreoffice/word/ispell 自定义词典中。

1 和 3 很简单。我该怎么做2?谢谢!

【问题讨论】:

  • cmets/suggestions 来证明反对票的合理性?
  • 如果你有一个单词表为什么不直接导入单词表呢?您正在使用的应用程序将合并额外的单词。
  • 您如何定义“普通”字典?你有参考吗?
  • 对不起,我不清楚。 mac 或 ispell 有内置的英语词典。我的 PDF 是英文的,但它们也包含一些额外的特定领域的行话。如果我写一个文本,每个特定领域的单词都有下划线,我可以手动将它添加到字典中。我想根据“受信任的”pdf目录自动执行此操作,我确信没有拼写错误......我只是想将内部字典中尚未包含的单词添加到自定义字典中......

标签: macos unix dictionary scripting


【解决方案1】:

据我了解,您希望删除重复项(系统字典中已存在)。不过,您可能想先问一下,这是否真的有必要。我想它们不会造成任何问题,也不会过度增加单词拼写检查,所以在我看来,第 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))

【讨论】:

  • 谢谢 - 你能补充一下如何删除系统字典中已经存在的单词吗?
  • 正如我试图说的 - 我不认为这是必要的 - 你为什么认为你需要这个?重复的单词不是问题 - 我可以添加一些代码,但您必须知道全局字典的格式/文件。
【解决方案2】:

这是一个基本的 java 程序,它将生成一个文本文件,其中包含纯文本文件目录中的所有唯一单词,由换行符分隔。

您可以将输入目录和输出文件路径字符串替换为适合您系统的正确值并运行它。

import java.io.*;
import java.util.*;

public class MakeDictionary {
    public static void main(String args[]) throws IOException {
        Hashtable<String, Boolean> dictionary = new Hashtable<String, Boolean>();

        String inputDir = "C:\\test";
        String outputFile = "C:\\out\\dictionary.txt";


        File[] files = new File(inputDir).listFiles();

        BufferedWriter out = new BufferedWriter(new FileWriter(outputFile));
        for (File file : files) {
            if (file.isFile()) {
                BufferedReader in = null;
                try {
                    in = new BufferedReader(new FileReader(file.getCanonicalPath()));
                    String line;
                    while ((line = in.readLine()) != null) {
                        String[] words = line.split(" ");
                        for (String word : words) {
                            dictionary.put(word, true);
                        }
                    }
                } finally {
                    if (in != null) {
                        in.close();
                    }
                }
            }
        }

        Set<String> wordset = dictionary.keySet();
        Iterator<String> iter = wordset.iterator();
        while(iter.hasNext()) {
            out.write(iter.next());
            out.newLine();
        }
        out.close();
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 2017-03-03
    相关资源
    最近更新 更多