Word&线一致性计划

时间:2018-05-01 21:31:20

标签: python punctuation

我最初在这里发布了这个问题,但随后被告知将其发布到代码审查中;然而,他们告诉我,我的问题需要在这里发布。我会尽力更好地解释我的问题,所以希望没有混淆。我正在尝试编写一个word-concordance程序,它将执行以下操作:

1)将stop_words.txt文件读入字典(使用与您正在计时的相同类型的字典),该字典仅包含停用词,称为stopWordDict。 (警告:在将停用词的末尾添加到stopWordDict之前,从停用词的末尾删除换行符('\ n'))

2)一次处理WarAndPeace.txt文件一行,以构建包含键的“主要”单词的单词一致性词典(称为wordConcordanceDict),并将相关行号列表作为其值。

3)按键按字母顺序遍历wordConcordanceDict,生成一个文本文件,其中包含按字母顺序打印的一致词及其对应的行号。

我在一个带有一小段停用词的小文件上测试了我的程序,并且它正常工作(提供了下面的示例)。结果就是我的预期,一个主要单词列表及其行数,不包括stop_words_small.txt文件中的单词。我测试的小文件和我实际尝试测试的主文件之间的唯一区别是主文件更长并且包含标点符号。所以我遇到的问题是当我用主文件运行我的程序时,我得到了更多的结果然后预期。我之所以得到更多结果的原因是因为没有从文件中删除标点符号。

例如,下面是结果的一部分,其中我的代码将Dmitri这个词计为四个单独的单词,因为该单词后面的大小写和标点符号不同。如果我的代码要正确删除标点符号,则Dmitri一词将被计为一个单词,后跟所有找到的位置。我的输出也是分隔大写和小写的单词,所以我的代码也没有使文件小写。

我的代码当前显示的内容:

Dmitri : [2528, 3674, 3687, 3694, 4641, 41131]

Dmitri! : [16671, 16672]

Dmitri, : [2530, 3676, 3685, 13160, 16247]

dmitri : [2000]

我的代码应显示的内容:

dmitri : [2000, 2528, 2530, 3674, 3676, 3685, 3687, 3694, 4641, 13160, 16671, 16672, 41131]

单词被定义为由任何非字母分隔的字母序列。大写和小写字母之间也应该没有区别,但我的程序也将它们分开;但是,在行号中要计算空白行。

以下是我的代码,如果有人可以看一下它并给我任何关于我做错的反馈,我将不胜感激。提前谢谢。

import re

def main():
    stopFile = open("stop_words.txt","r")
    stopWordDict = dict()

    for line in stopFile:
        stopWordDict[line.lower().strip("\n")] = []

    hwFile = open("WarAndPeace.txt","r")
    wordConcordanceDict = dict()
    lineNum = 1

    for line in hwFile:
        wordList = re.split(" |\n|\.|\"|\)|\(", line)
        for word in wordList:
            word.strip(' ')
            if (len(word) != 0) and word.lower() not in stopWordDict:
                if word in wordConcordanceDict:
                    wordConcordanceDict[word].append(lineNum)
                else:
                    wordConcordanceDict[word] = [lineNum]
        lineNum = lineNum + 1

    for word in sorted(wordConcordanceDict):
        print (word," : ",wordConcordanceDict[word])


if __name__ == "__main__":
main()

就像另一个例子和参考一样,我测试的小文件中有一小段停顿词可以很好地工作。

stop_words_small.txt文件

a, about, be, by, can, do, i, in, is, it, of, on, the, this, to, was

small_file.txt

This is a sample data (text) file to
be processed by your word-concordance program.

The real data file is much bigger.

正确输出

bigger: 4

concordance: 2

data: 1 4

file: 1 4

much: 4

processed: 2

program: 2

real: 4

sample: 1

text: 1

word: 2

your: 2

1 个答案:

答案 0 :(得分:1)

你可以这样做:

import re
from collections import defaultdict

wordConcordanceDict = defaultdict(list)

with open('stop_words_small.txt') as sw:
    words = (line.strip() for line in sw)
    stop_words = set(words)

with open('small_file.txt') as f:
    for line_number, line in enumerate(f, 1):
        words = (re.sub(r'[^\w\s]','',word).lower() for word in line.split())
        good_words = (word for word in words if word not in stop_words)
        for word in good_words:
            wordConcordanceDict[word].append(line_number)

for word in sorted(wordConcordanceDict):
    print('{}: {}'.format(word, ' '.join(map(str, wordConcordanceDict[word]))))

输出:

bigger: 4
data: 1 4
file: 1 4
much: 4
processed: 2
program: 2
real: 4
sample: 1
text: 1
wordconcordance: 2
your: 2

 我明天会加上解释,这里要迟到了;)。同时,您可以在评论中询问代码的某些部分是否不清楚。