从标记化的URL计数单词

时间:2012-06-05 08:50:33

标签: python count tokenize words

非常新的Python,希望你们能给我一些帮助。

我有一本关于伟大战争的书,想要计算一个国家出现在书中的时间。到目前为止我有这个:

>>> from __future__ import division 
>>> import nltk, re, pprint
>>> from urllib import urlopen
>>> url = "http://www.gutenberg.org/files/29270/29270.txt"
>>> raw = urlopen(url).read() 
>>> type(raw)
<type 'str'>
>>> len(raw)
1067008
>>> raw[:75]
'The Project Gutenberg EBook of The Story of the Great War, Volume II (of\r\nV'
>>>

符号化。 将字符串分解为单词和标点符号。

>>> tokens = nltk.word_tokenize(raw)
>>> type(tokens)
<type 'list'>
>>> len(tokens)
189743
>>> tokens[:10] //vind de eerste 10 tokens
['The', 'Project', 'Gutenberg', 'EBook', 'of', 'The', 'Story', 'of', 'the', 'Great']
>>>

纠正书的开头和结尾

    >>> raw.find("PART I")
    >>> 2629
    >>> raw.rfind("End of the Project Gutenberg")
    >>> 1047663
    >>> raw = raw[2629:1047663]
    >>> raw.find("PART I")
    >>> 0

我很遗憾不知道如何将这本书实施到wordcount中。我理想的结果将是这样的:

Germany 2000
United Kingdom 1500
USA 1000
Holland 50
Belgium 150

请帮忙!

1 个答案:

答案 0 :(得分:1)

Python有一个内置方法来计算字符串中的子字符串。

from urllib import urlopen

url = "http://www.gutenberg.org/files/29270/29270.txt"
raw = urlopen(url).read()
raw = raw[raw.find("PART I"):raw.rfind("End of the Project Gutenberg")]

countries = ['Germany', 'United Kingdom', 'USA', 'Holland', 'Belgium']
for c in countries:
    print c, raw.count(c)

产生

Germany 117
United Kingdom 0
USA 0
Holland 10
Belgium 63

编辑:eumiro是对的,如果您想要计算确切的单词,这不起作用。如果要搜索确切的单词,请使用此选项:

import re
from urllib import urlopen

url = "http://www.gutenberg.org/files/29270/29270.txt"
raw = urlopen(url).read()
raw = raw[raw.find("PART I"):raw.rfind("End of the Project Gutenberg")]

for key, value in {c:len(re.findall(c + '[^A-Za-z]', raw)) for c in countries}.items():
    print key, value

编辑:如果你想要亚麻布:

from urllib import urlopen
import re
from collections import defaultdict

url = "http://www.gutenberg.org/files/29270/29270.txt"
raw = urlopen(url).readlines()

count = defaultdict(list)
countries = ['Germany', 'United Kingdom', 'USA', 'Holland', 'Belgium']
for c in countries:
    for nr, line in enumerate(raw):
        if re.search(c + r'[^A-Za-z]', line):
            count[c].append(nr + 1) #nr + 1 so the first line is 1 instead of 0
    print c, len(count[c]), 'lines:', count[c]