3

我编写了这个程序来对文本文档中的单词进行分类和枚举。如果我不必用string.translate()... 挑出每一个可能的标点符号,代码会非常简洁。是否有可能,而不是排除特定情况,只允许字母(也许是数字)字符?

from sys import argv

script_, filename = argv

bang = open(filename, 'r+')
words = bang.read()
words = words.translate(None, ',')
words = words.translate(None, '"')
words = words.translate(None, '.')
words = words.translate(None, '...')
words = words.translate(None, '?')
words = words.translate(None, '!')
words = words.translate(None, ';')
words = words.translate(None, '-')
words = words.translate(None, '\'')
words = words.translate(None, '.\'')
words = words.translate(None, '(')
words = words.translate(None, ')')
words = words.translate(None, ':')
words = str(words)
words = words.lower()
liste = words.split()
sorte = sorted(liste)

i = 0
f = 'nullooosdfgkjlkjasdihaiwuehlfkj898'
z = 1
w = 0

for wordss in sorte:
    if f == wordss:
        z += 1
        w += 1
    elif f != wordss: 
        w += 1
        print "-", z
        z = 1
        i += 1
        print "%d. %s" % (i, wordss),
        f = wordss

print "\n\n word count - %d\n" % w
4

3 回答 3

6

我想列出文本文档中的单词

这个算法怎么样。在空白处拆分文本,然后去除标点符号。

>>> text = "'I wonder how many miles I've fallen by this time?' she said aloud."
>>> import string
>>> words = [x.strip(string.punctuation) for x in text.split()]
>>> words
['I', 'wonder', 'how', 'many', 'miles', "I've", 'fallen', 'by', 'this', 'time', 'she', 'said', 'aloud']

请参阅 this 处理诸如don't之类的缩略词。所以你可以区分we'rewere

于 2013-05-23T22:41:12.513 回答
2

您可以使用正则表达式:

import re

cleaned_text = re.sub(r'[^a-zA-Z0-9]', '', your_text)

虽然使用正则表达式会更干净Counter

>>> import re
>>> from collections import Counter
>>> sentence = 'I went to the store. It was a red store.'
>>> words = re.findall(r'\w+', sentence)
>>> words
['I', 'went', 'to', 'the', 'store', 'It', 'was', 'a', 'red', 'store']
>>> Counter(words)
Counter({'store': 2, 'a': 1, 'I': 1, 'It': 1, 'to': 1, 'red': 1, 'went': 1, 'was': 1, 'the': 1})
>>> d = Counter(words)
>>> d['store']
2
于 2013-05-23T22:35:23.820 回答
2

您可以使用string.punctuation,我相信这也考虑了语言环境:

import string

text = 'asdkjhqa23789ryh &*(Y(E%R W*YP@#*>WFKJHSF(P*H$ >'
cleaned_text = text
for p in string.punctuation:
    cleaned_text = cleaned_text.replace(p,'')
于 2013-05-23T22:40:57.230 回答