繁体   English   中英

在python中使用字典查找文本文件中的字典单词

[英]Looking for dictionary words in text file using dictionary in python

我阅读了如何检查字典中的单词,然后有了使用字典检查文本文件的想法。 我已经阅读了pyenchant说明,并且我认为如果我使用get_tokenizer将文本文件中的所有字典单词还给我。

因此,这就是我遇到的问题:我希望我的程序以段的形式给我所有的字典单词组。 一旦遇到任何垃圾字符,就认为该段中断,并忽略那里的所有内容,直到找到X个连续的单词。

我希望它按filename_nnn.txt的顺序读取文本文件,对其进行解析,然后写入parsed_filname_nnn.txt 我还没有做任何文件操作。

到目前为止,我有:

import enchant
from enchant.tokenize import get_tokenizer, HTMLChunker
dictSentCheck = get_tokenizer("en_US")
sentCheck = raw_input("Check Sentense: ")

def check_dictionary():
    outcome = dictCheck.check(wordCheck) 
    test = [w[0] for w in dictSentCheck(sentCheck)]

- - - 示范文本 - - -

2008年6月25日,星期三,英国板球与津巴布韦的关系中断文本< void(0);>< void(0);> < void(0);>发送电子邮件< void(0);>通过电子邮件发送此文章您的姓名:您的电子邮件地址:收件人的姓名:收件人的电子邮件地址:&lt ;;>添加其他收件人您的评论:发送邮件< ; void(0);> 关闭此表单< http://ad.au.doubleclick.net/jump/sbs.com.au/worldnews;sz=300x250;tile=2;ord=123456789?&gt ; 英格兰和威尔士板球委员会(ECB)宣布将暂停与津巴布韦的所有联系,并取消明年津巴布韦的英格兰之旅。

该脚本应返回:

英国板球周三与津巴布韦断绝联系

英格兰和威尔士板球委员会(ECB)宣布将暂停与津巴布韦的所有联系,并取消明年津巴布韦的英格兰之旅

我接受了阿巴纳特的回应。 以下是我的最终脚本。 注意这是非常低效的,应该清理一些。 另外免责声明我很久以前就没有写过大学代码。

import enchant
from enchant.tokenize import get_tokenizer
import os

def clean_files():
    os.chdir("TARGET_DIRECTORY")
    for files in os.listdir("."):
           #get the numbers out file names 
           file_number = files[files.rfind("_")+1:files.rfind(".")]

           #Print status to screen
           print "Working on file: ", files

           #Read and process original file
           original_file = open("name_"+file_number+".txt", "r+")
           read_original_file = original_file.read();

           #Start the parsing of the files
           token_words = tokenize_words(read_original_file)
           parse_result = ('\n'.join(split_on_angle_brackets(token_words,file_number)))
           original_file.close()

           #Commit changes to parsed file
           parsed_file = open("name_"+file_number+"_parse.txt", "wb")
           parsed_file.write(parse_result);
           parsed_file.close()

def tokenize_words(file_words):
    tokenized_sentences = get_tokenizer("en_US")
    word_tokens = tokenized_sentences(file_words)
    token_result = [w[0] for w in word_tokens]
    return token_result

def check_dictionary(dict_word):
    check_word = enchant.Dict("en_US")
    validated_word = check_word.check(dict_word)
    return validated_word

def split_on_angle_brackets(token_words, file_number):
    para = []
    bracket_stack = 0
    ignored_words_per_file = open("name_"+file_number+"_ignored_words.txt", "wb")
    for word in token_words:
        if bracket_stack:
            if word == 'gt':
                bracket_stack -= 1
            elif word == 'lt':
                bracket_stack += 1
        else:
            if word == 'lt':
                if len(para) >= 7:
                    yield ' '.join(para)
                para = []
                bracket_stack = 1
            elif word != 'amp':
                if check_dictionary(word) == True:
                    para.append(word)
                    #print "append ", word
                else:
                       print "Ignored word: ", word
                       ignored_words_per_file.write(word + " \n")
    if para:
        yield ' '.join(para)

    #Close opened files
    ignored_words_per_file.close()

clean_files()

我仍然不确定您的问题到底是什么,或者代码应该做什么。

但是这行似乎是关键:

test = [w[0] for w in dictSentCheck(sentCheck)]

这样就可以列出所有单词。 它包括ltgt类的单词。 而且您想去除ltgt对中的所有内容。

而且,正如您在评论中所说,“我可以将所需的连续单词数设置为7”。

因此,如下所示:

def split_on_angle_brackets(words):
    para = []
    bracket_stack = 0
    for word in words:
        if bracket_stack:
            if word == 'gt':
                bracket_stack -= 1
            elif word == 'lt':
                bracket_stack += 1
        else:
            if word == 'lt':
                if len(para) >= 7:
                    yield ' '.join(para)
                para = []
                bracket_stack = 1
            else:
                para.append(word)
    if para:
        yield ' '.join(para)

如果将其与样本数据一起使用:

print('\n'.join(split_on_angle_brackets(test)))

你得到这个:

English cricket cuts ties with Zimbabwe Wednesday June text
print EMAIL THIS ARTICLE your name your email address recipient's name recipient's email address
add another recipient your comment Send Mail
The England and Wales Cricket Board ECB announced it was suspending all ties with Zimbabwe and was cancelling Zimbabwe's tour of England next year

那与您的示例输出不匹配,但是我想不出任何可以提供示例输出的规则,因此,我正在尝试实现您描述的规则。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM