在Python中过滤文本数据

时间:2011-06-30 00:09:23

标签: python

我在解决我在这里做错了什么问题。我有下面的代码(相当简单)。

def compileWordList(textList, wordDict):
    '''Function to extract words from text lines exc. stops,
        and add associated line nums'''
    i = 0;
    for row in textList:
        i = i + 1
        words = re.split('\W+', row)
        for wordPart in words:
            word = repr(wordPart)
            word = word.lower()
            if not any(word in s for s in stopsList):
                if word not in wordDict:
                    x = wordLineNumsContainer()
                    x.addLineNum(i)
                    wordDict[word] = x
                elif word in wordDict:
                    lineNumValues = wordDict[word]
                    lineNumValues.addLineNum(i)
                    wordDict[word] = lineNumValues
            elif any(word in s for s in stopsList):
                print(word)

代码从列表中获取字符串(句子)。然后使用re.split()方法将整个单词的字符串拆分,返回字符串列表(单词)。

然后我将字符串强制为小写。然后我希望它在我有一个停止词的列表中检查这个词是否存在(在英语中太常见的词来打扰)。检查word是否在stopsList中的部分似乎永远不会起作用,因为停用词每次都会在wordDict中结束。我还添加了底部print(word)语句,以便检查它是否正在捕获它们,但是没有任何内容被打印出来:(

在通过的字符串中使用了数百个停用词。

有人可以在这里开导我吗?为什么字符串永远不会因为停用词而被过滤?

非常感谢, 亚历

1 个答案:

答案 0 :(得分:7)

怎么样?

from collections import defaultdict
import re

stop_words = set(['a', 'is', 'and', 'the', 'i'])
text = [ 'This is the first line in my text'
       , 'and this one is the second line in my text'
       , 'I like texts with three lines, so I added that one'
       ]   
word_line_dict = defaultdict(list)

for line_no, line in enumerate(text, 1): 
    words = set(map(str.lower, re.split('\W+', line)))
    words_ok = words.difference(stop_words)
    for wok in words_ok:
        word_line_dict[wok].append(line_no)

print word_line_dict

非常感谢Gnibbler:更好的编写for-loop和amp;的方法。更多pythonic方式处理首次插入字典。

打印(除了字典的格式)

{ 'added': [3]
, 'like': [3]
, 'that': [3]
, 'this': [1, 2]
, 'text': [1, 2]
, 'lines': [3]
, 'three': [3]
, 'one': [2, 3]
, 'texts': [3]
, 'second': [2]
, 'so': [3]
, 'in': [1, 2]
, 'line': [1, 2]
, 'my': [1, 2]
, 'with': [3]
, 'first': [1]
}