创建一个使用python和fi的字典

2024-04-27 15:18:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我从projectgutenberg http://www.gutenberg.org/cache/epub/29765/pg29765.txt下载了下面的字典(它是25MB,所以如果你使用的是慢速连接,请避免点击链接)

在我的字典里,有一些词是专门为我在字典里找的,比如说在我的字典里有一些过时的词。在

我要提取的是定义,用“Defn”表示,然后打印这些行。我想出了一个相当难看的“解决方案”

def lookup(search):
    find = search.upper()                   # transforms our search parameter all upper letters
    output = []                             # empty dummy list
    infile = open('webster.txt', 'r')       # opening the webster file for reading
    for line in infile:
        for part in line.split():
            if (find == part):
                for line in infile:
                    if (line.find("Defn:") == 0):  # ugly I know, but my only guess so far
                        output.append(line[6:])
                        print output               # uncertain about how to proceed
                        break

当然,这只打印第一行在“Defn:”之后。因此,当Python来处理新的文件时,我将如何处理它。我读过元组中的行,注意到有一些特殊的新行字符。在

所以我想告诉Python继续读,直到新行字符用完为止,但这不算最后一行必须读的。在

有人能用一些有用的功能来增强我的能力吗?我可以用它来解决这个问题(如果有一个最小的例子将不胜感激)。在


期望输出示例:

查找(“幻觉”)

出局:漫游;误入歧途;犯错;犯错误;犯错误;用于精神 过程。[R.]拜伦。在

查找(“幻觉”)

out:对没有真实感的对象的感知,或\r\n 没有相应外因的感觉,由\r\n 精神错乱或神经系统紊乱,如震颤性谵妄;妄想症。\r\n 幻觉总是大脑紊乱的证据,并且是\r\n 常见的精神错乱现象。W、 A.哈蒙德。在


从文本:

^{pr2}$

Tags: intxt精神foroutputsearch字典line
3条回答

您可以拆分为段落并使用搜索词的索引,然后在以下位置找到第一个Defn段落:

def find_def(f,word):
    import re
    with open(f) as f:
        lines = f.read() 
        try:
            start = lines.index("{}\r\n".format(word)) # find where our search word is
        except ValueError: 
            return "Cannot find search term" 
        paras = re.split("\s+\r\n",lines[start:],10) # split into paragraphs using maxsplit = 10 as there are no grouping of paras longer in the definitions
        for para in paras:
            if para.startswith("Defn:"): # if para startswith Defn: we have what we need
                return para # return the  para

print(find_def("in.txt","HALLUCINATION"))

使用整个文件返回:

^{pr2}$

略短的版本:

def find_def(f,word):
    import re
    with open(f) as f:
        lines = f.read()
        try:
            start = lines.index("{}\r\n".format(word))
        except ValueError:
            return "Cannot find search term"
        defn = lines[start:].index("Defn:")
        return re.split("\s+\r\n",lines[start+defn:],1)[0]

here我学到了一种简单的方法来处理内存映射文件,并将它们当作字符串使用。你可以先用这个词来定义。在

def lookup(search):
    term = search.upper()
    f = open('webster.txt')
    s = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
    index = s.find('\r\n\r\n' + term + '\r\n')
    if index == -1:
        return None
    definition = s.find('Defn:', index) + len('Defn:') + 1
    endline = s.find('\r\n\r\n', definition)
    return s[definition:endline]

print lookup('hallucination')
print lookup('hallucinate')

假设:

  • 每个术语至少有一个定义
  • 如果有多个,则只返回第一个

以下是返回第一个定义的函数:

def lookup(word):
    word_upper = word.upper()
    found_word = False
    found_def = False
    defn = ''
    with open('dict.txt', 'r') as file:
        for line in file:
            l = line.strip()
            if not found_word and l == word_upper:
                found_word = True
            elif found_word and not found_def and l.startswith("Defn:"):
                found_def = True
                defn = l[6:]
            elif found_def and l != '':
                defn += ' ' + l
            elif found_def and l == '':
                return defn
    return False

print lookup('hallucination')
<>强>解释<强>:我们必须考虑四种不同的情况。在

  • 我们还没有找到这个词。我们必须将当前行与我们要查找的大写单词进行比较。如果他们是平等的,我们找到了这个词。在
  • 我们找到了这个词,但还没有找到定义的开头。因此,我们必须寻找以Defn:开头的行。如果找到它,我们将该行添加到定义中(不包括Defn:的六个字符)。在
  • 我们已经找到了定义的起点。在这种情况下,我们只需在定义中添加一行。在
  • 我们已经找到定义的开始,当前行为空。定义已完成,我们返回定义。在

如果我们什么也没找到,我们返回False。在

:某些条目,如起重机,有多种定义。上面的代码不能处理这个问题。它只返回第一个定义。然而,考虑到文件的格式,编写一个完美的解决方案远非易事。在

相关问题 更多 >