我最初将这个问题发布在这里,但随后被告知将其发布到代码审查中。但是,他们告诉我,我的问题需要改为在此处发布。我将尽力更好地解释我的问题,因此希望不会造成混乱。我正在尝试编写一个单词一致性程序,该程序将执行以下操作:

1)将stop_words.txt文件读入仅包含停用词(称为stopWordDict)的字典(使用与您选择的时间相同的字典类型)。 (警告:将停用词末尾的换行符(\ n)删除,然后再将其添加到stopWordDict)

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

最佳答案

您可以这样做:

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



``我明天将添加说明,这里已经很晚了;)。同时,您可以在注释中询问您是否不清楚部分代码。

关于python - 字和线协调程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50124134/

10-12 22:03
查看更多