您的位置:首页 > 编程语言 > Python开发

python--更干净的词频统计

2015-12-05 17:07 417 查看
上篇文章(python--10行代码搞定词频统计)我们介绍了利用Counter模块轻松搞定词频统计的方法,因为重点是介绍模块的使用,代码显得比较粗糙。

import re,collections
def get_nums(file):
with open (file) as f:
words_box=[]
for line in f:
if re.match(r'[a-zA-Z0-9]*',line):#避免中文影响
words_box.extend(line.strip().split())
return collections.Counter(words_box)
print(get_nums('emma.txt')+get_nums('伊索寓言.txt'))

(注:文件下载地址:http://pan.baidu.com/s/1pKuO7fP)

运行之后发现问题主要在以下三个方面:

1.中文问题。

  统计结果仍然包括部分中文。原因是,有一些中文行存在英文字母,这使正则也对其进行了匹配。

2.大小写问题

比如,程序对the、The没有进行区分,这在某些情况下或许不是我们想要的结果。

3.单词存在标点尾

由于我们对字符串切分的依据是空格,所以不少单词连同后面的标点都被切分放到了words_box里面。

大小写问题很好解决,只需要调用字符串方法lower()。如何彻底地消灭中文呢?不妨放下正则,寻找更简便的工具。别说,还真有这样的字符串方法:isalpha()。它可以用来判断字符串是否由纯字母构成:

import collections
def get_words(file):
with open (file) as f:
words_box=[]
for line in f:
words_box.extend(line.lower().strip().split())
new_words_box=[]
for word in words_box:
if word.isalpha():
new_words_box.append(word)
return collections.Counter(new_words_box)
print(get_words('emma.txt')+get_words('伊索寓言.txt'))
运行结果基本干净了,为什么说基本呢?因为这样做存在一个缺点:将带有标点尾的单词彻底的排除在统计范围外了,这可不是我们想要的结果。我们可以再补充一段回收代码,最终代码如下:

import collections
def get_words(file):
    with open (file) as f:
        words_box=[]
        for line in f: 
            words_box.extend(line.lower().strip().split()) 
        new_words_box=[]
        for word in words_box:
            if word.isalpha():
                new_words_box.append(word)
            else:
                new_word=''
                for letter in word:
                    if letter.isalpha():
                        new_word+=letter
                if new_word!='':
                    new_words_box.append(new_word)    
    return collections.Counter(new_words_box)
print(get_words('emma.txt')+get_words('伊索寓言.txt'))


部分运行结果如下:

Counter({'the': 2881, 'and': 1441, 'a': 1145, 'to': 1112, 'of': 890, 'his': 679, 'he': 566, 'in': 543, 'you': 415, 'him': 384, 'i': 332, 'that': 328, 'with': 317, 'for': 312, 'was': 267, 'it': 264, 'as': 244, 'not': 239, 'had': 225, 'on': 219, 'by': 210, 'said': 209, 'but': 196, 'they': 178, 'be': 177, 'at': 175, 'her': 173, 'when': 166, 'them': 163, 'one': 163, 'is': 158, 'from': 153, 'me': 147, 'who': 147, 'their': 147, 'my': 141, 'all': 139, 'an': 136, 'lion': 135, 'have': 131, 'your': 125, 'which': 125, 'if': 123, 'up': 120, 'man': 114, 'fox': 108, 'would': 107, 'this': 103, 'will': 101, 'do': 101, 'so': 101, 'into': 100, 'ass': 99, 'out': 98, 'himself': 95, 'she': 95, 'no': 94, 'were': 94, 'upon': 90, 'wolf': 88, 'are': 86, 'what': 85, 'should': 79, 'replied': 79, 'page': 75, 'very': 71, 'could': 65, 'its': 64, 'time': 62, 'day': 62, 'being': 61, 'made': 61, 'saw': 61, 'own': 61, 'only': 60, 'down': 60, 'am': 59, 'came': 58, 'some': 58, 'while': 55, 'thus': 53, 'two': 52, 'we': 51, 'other': 50, 'why': 49, 'eagle': 48, 'dog': 48, 'how': 47, 'make': 47, 'there': 47, 'great': 47, 'after': 46, 'much': 46, 'having': 45, 'well': 44, 'more': 43, 'about': 43, 'saying': 43, 'these': 42, 'than': 42, 'sheep': 41, 'been': 41, 'good': 41, 'jupiter': 41, 'now': 40, 'men': 40, 'ol
9a33
d': 39, 'food': 39, 'long': 39, 'us': 38, 'off': 38, 'life': 38, 'friend': 37, 'away': 37, 'before': 36, 'found': 36, 'again': 35, 'seeing': 35, 'death': 35, 'little': 35, 'water': 35, 'can': 34, 'young': 34, 'once': 34, 'see': 34, 'same': 34, 'then': 33, 'fell': 33, 'horse': 33, 'went': 33, 'might': 33, 'shall': 32, 'mother': 32, 'such': 32 ···}

当然,使用正则的split方法会简单很多。当我们需要更灵活切割字符串的时候,最好使用re.split()方法:

import collections,re
def get_words(file):
with open (file) as f:
words_box=[]
for line in f:
words_box.extend(re.split(r'[;\.\s]*', line))
new_words_box=[]
for word in words_box:
if word.isalpha():
new_words_box.append(word)
return collections.Counter(new_words_box)
print(get_words('emma.txt')+get_words('伊索寓言.txt'))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息