如何使用NLTK tokenizer摆脱标点符号?

时间:2013-03-21 12:22:08

标签: python nlp tokenize nltk

我刚刚开始使用NLTK而且我不太了解如何从文本中获取单词列表。如果我使用nltk.word_tokenize(),我会得到一个单词和标点符号列表。我只需要单词代替。我怎样才能摆脱标点符号? word_tokenize也不能处理多个句子:在最后一个单词中添加了点。

11 个答案:

答案 0 :(得分:138)

查看nltk提供的其他标记化选项here。例如,您可以定义一个标记生成器,它将字母数字字符序列作为标记并删除其他所有内容:

from nltk.tokenize import RegexpTokenizer

tokenizer = RegexpTokenizer(r'\w+')
tokenizer.tokenize('Eighty-seven miles to go, yet.  Onward!')

输出:

['Eighty', 'seven', 'miles', 'to', 'go', 'yet', 'Onward']

答案 1 :(得分:41)

你真的不需要NLTK去除标点符号。你可以用简单的python删除它。对于字符串:

import string
s = '... some string with punctuation ...'
s = s.translate(None, string.punctuation)

或者对于unicode:

import string
translate_table = dict((ord(char), None) for char in string.punctuation)   
s.translate(translate_table)

然后在您的tokenizer中使用此字符串。

P.S。字符串模块还有一些其他可以删除的元素(如数字)。

答案 2 :(得分:19)

下面的代码将删除所有标点符号以及非字母字符。复制自他们的书。

http://www.nltk.org/book/ch01.html

import nltk

s = "I can't do this now, because I'm so tired.  Please give me some time. @ sd  4 232"

words = nltk.word_tokenize(s)

words=[word.lower() for word in words if word.isalpha()]

print(words)

输出

['i', 'ca', 'do', 'this', 'now', 'because', 'i', 'so', 'tired', 'please', 'give', 'me', 'some', 'time', 'sd']

答案 3 :(得分:15)

正如注释中所注意到的那样,以sent_tokenize()开头,因为word_tokenize()仅适用于单个句子。您可以使用filter()过滤标点符号。如果你有一个unicode字符串,请确保它是一个unicode对象(不是'str'编码的某些编码,如'utf-8')。

from nltk.tokenize import word_tokenize, sent_tokenize

text = '''It is a blue, small, and extraordinary ball. Like no other'''
tokens = [word for sent in sent_tokenize(text) for word in word_tokenize(sent)]
print filter(lambda word: word not in ',-', tokens)

答案 4 :(得分:9)

我刚刚使用了以下代码,删除了所有标点符号:

tokens = nltk.wordpunct_tokenize(raw)

type(tokens)

text = nltk.Text(tokens)

type(text)  

words = [w.lower() for w in text if w.isalpha()]

答案 5 :(得分:6)

我认为你需要某种正则表达式匹配(以下代码在Python 3中):

import string
import re
import nltk

s = "I can't do this now, because I'm so tired.  Please give me some time."
l = nltk.word_tokenize(s)
ll = [x for x in l if not re.fullmatch('[' + string.punctuation + ']+', x)]
print(l)
print(ll)

输出:

['I', 'ca', "n't", 'do', 'this', 'now', ',', 'because', 'I', "'m", 'so', 'tired', '.', 'Please', 'give', 'me', 'some', 'time', '.']
['I', 'ca', "n't", 'do', 'this', 'now', 'because', 'I', "'m", 'so', 'tired', 'Please', 'give', 'me', 'some', 'time']

在大多数情况下应该可以正常工作,因为它会保留标记,例如" n&#t;",这些标记不能从wordpunct_tokenize等正则表达式标记符中获取。 / p>

答案 6 :(得分:4)

我使用此代码删除标点符号:

import nltk
def getTerms(sentences):
    tokens = nltk.word_tokenize(sentences)
    words = [w.lower() for w in tokens if w.isalnum()]
    print tokens
    print words

getTerms("hh, hh3h. wo shi 2 4 A . fdffdf. A&&B ")

如果您想检查令牌是否是有效的英语单词,您可能需要PyEnchant

教程:

 import enchant
 d = enchant.Dict("en_US")
 d.check("Hello")
 d.check("Helo")
 d.suggest("Helo")

答案 7 :(得分:1)

删除标点符号(它将删除。以及使用以下代码进行的标点符号处理的一部分)

        tbl = dict.fromkeys(i for i in range(sys.maxunicode) if unicodedata.category(chr(i)).startswith('P'))
        text_string = text_string.translate(tbl) #text_string don't have punctuation
        w = word_tokenize(text_string)  #now tokenize the string 

样本输入/输出:

direct flat in oberoi esquire. 3 bhk 2195 saleable 1330 carpet. rate of 14500 final plus 1% floor rise. tax approx 9% only. flat cost with parking 3.89 cr plus taxes plus possession charger. middle floor. north door. arey and oberoi woods facing. 53% paymemt due. 1% transfer charge with buyer. total cost around 4.20 cr approx plus possession charges. rahul soni

['direct', 'flat', 'oberoi', 'esquire', '3', 'bhk', '2195', 'saleable', '1330', 'carpet', 'rate', '14500', 'final', 'plus', '1', 'floor', 'rise', 'tax', 'approx', '9', 'flat', 'cost', 'parking', '389', 'cr', 'plus', 'taxes', 'plus', 'possession', 'charger', 'middle', 'floor', 'north', 'door', 'arey', 'oberoi', 'woods', 'facing', '53', 'paymemt', 'due', '1', 'transfer', 'charge', 'buyer', 'total', 'cost', 'around', '420', 'cr', 'approx', 'plus', 'possession', 'charges', 'rahul', 'soni']

答案 8 :(得分:0)

真诚地问,什么词?如果您假设一个单词仅由字母字符组成,那么您是错误的,因为诸如can't之类的单词会被破坏成碎片(例如cant如果在标记化之前删除标点符号,这很可能会对您的程序产生负面影响。

因此,解决方案是先标记然后删除标点符号

import string

from nltk.tokenize import word_tokenize

tokens = word_tokenize("I'm a southern salesman.")
# ['I', "'m", 'a', 'southern', 'salesman', '.']

tokens = list(filter(lambda token: token not in string.punctuation, tokens))
# ['I', "'m", 'a', 'southern', 'salesman']

...然后,如果需要,可以用'm替换某些标记,例如am

答案 9 :(得分:0)

只需通过@rmalouf添加到解决方案中,就不会包含任何数字,因为\ w +等效于[a-zA-Z0-9 _]

from nltk.tokenize import RegexpTokenizer
tokenizer = RegexpTokenizer(r'[a-zA-Z]')
tokenizer.tokenize('Eighty-seven miles to go, yet.  Onward!')

答案 10 :(得分:0)

您可以在没有nltk(python 3.x)的情况下一行完成此操作。

import string
string_text= string_text.translate(str.maketrans('','',string.punctuation))
相关问题