我刚开始使用NLTK,我不太明白如何从文本中获取单词列表。如果我使用nltk.word_tokenize(),我会得到一个单词和标点的列表。我只需要这些词。我怎样才能去掉标点符号?另外,word_tokenize不适用于多个句子:在最后一个单词中添加点。

你为什么不自己去掉标点符号?nltk.word_tokenize(the_text.translate(None, string.punctuation))应该在python2中工作,而在python3中你可以在nltk.work_tokenize(the_text.translate(dict.fromkeys(string.p‌​unctuation)))中工作。

这不管用。文本没有任何变化。

NLTK假定的工作流程是先将句子标记化,然后将每个句子标记为单词。这就是为什么word_tokenize()不适用于多个句子。为了去掉标点符号,可以使用正则表达式或Python的isalnum()函数。

它确实有效:>>> 'with dot.'.translate(None, string.punctuation) 'with dot'(注意结果末尾没有点),如果你有像'end of sentence.No space'这样的东西,它可能会引起问题,在这种情况下,你可以这样做:the_text.translate(string.maketrans(string.punctuation, ' '*len(string.punctuation)))用空格替换所有标点符号。

糟糕,它确实有效,但不适用于Unicode字符串。

顺便说一下,isalnum()方法与unicode一起工作。

试试这个-stackoverflow.com/questions/265960/…

@"python2"是指python2的str,也就是python3的bytes。如果你使用我写的"python3"版本,它会起作用:the_text.translate(dict.fromkeys(string.punctuation))删除所有(ascii)标点符号。

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

1

2

3

4from nltk.tokenize import RegexpTokenizer

tokenizer = RegexpTokenizer(r'\w+')

tokenizer.tokenize('Eighty-seven miles to go, yet. Onward!')

输出:

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

请注意,如果使用此选项,则会丢失word_tokenize特有的自然语言功能,如拆分收缩。您可以天真地在regex \w+上拆分,而不需要任何NLTK。

为了说明@sffc comment,您可能会丢失诸如"mr"之类的词。

您不需要NLTK来删除标点符号。您可以用简单的python删除它。弦乐:

1

2

3import string

s = '... some string with punctuation ...'

s = s.translate(None, string.punctuation)

或用于Unicode:

1

2

3import string

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

s.translate(translate_table)

然后在标记器中使用这个字符串。

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

使用同样有效的列表表达式删除所有标点。a ="*fa,fd.1lk#$" print("".join([w for w in a if w not in string.punctuation]))

下面的代码将删除所有标点符号以及非字母字符。从他们的书上抄下来的。

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

1

2

3

4

5

6

7

8

9import 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)

输出

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

请注意,使用这种方法,在"不能"或"不要"这样的情况下,会丢失"不"一词,这对于理解和分类句子可能非常重要。最好使用sentence.translate(string.maketrans(",,),chars_to_remove),其中chars_to_remove可以是".,":;!"

正如在注释中注意到的,从sent_tokenize()开始,因为word_tokenize()只在一个句子上工作。您可以使用filter()过滤掉标点符号。如果您有一个Unicode字符串,请确保它是一个Unicode对象(不是用一些编码(如"utf-8")编码的"str")。

1

2

3

4

5from 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)

Penn Treebank标记器所涉及的大部分复杂性都与正确处理标点符号有关。如果你只想去掉标点符号,为什么要使用一个处理标点符号的昂贵的标记器?

word_tokenize是返回[token for sent in sent_tokenize(text, language) for token in _treebank_word_tokenize(sent)]的函数。所以我认为你的答案是做NLTK已经做的:在使用word_tokenize()之前使用sent_tokenize()。至少对NLTK3来说是这样。

@因为你不需要标点符号吗?所以你要的是did和n't,而不是.。

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

1

2

3

4

5

6

7

8

9tokens = nltk.wordpunct_tokenize(raw)

type(tokens)

text = nltk.Text(tokens)

type(text)

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

为什么要将令牌转换为文本?

我认为您需要某种正则表达式匹配(下面的代码在python 3中):

1

2

3

4

5

6

7

8

9import 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)

输出:

1

2['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之类的regex标记化器获得。

这也将在保持收缩的同时去除像...和--这样的东西,而s.translate(None, string.punctuation)不会

我使用此代码删除标点:

1

2

3

4

5

6

7

8import 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

辅导的:

1

2

3

4

5import enchant

d = enchant.Dict("en_US")

d.check("Hello")

d.check("Helo")

d.suggest("Helo")

当心这个解决方案会杀死宫缩。这是因为word_tokenize使用标准标记器TreebankWordTokenizer,它将收缩(例如can't)分解为(ca,n't)。但是,n't不是字母数字,在这个过程中会丢失。

去除穿刺(它会去除。以及使用以下代码处理标点符号的一部分)

1

2

3tbl = 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

样品输入/输出:

1direct 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']

非常感谢你

rld在python_关于python:如何使用NLTK tokenizer摆脱标点符号?相关推荐

  1. 中文停用词文档_使用Python中的NLTK和spaCy删除停用词与文本标准化

    译者 | VK 来源 | Analytics Vidhya [磐创AI 导读]:本文介绍了如何使用Python中的NLTK和spaCy删除停用词与文本标准化,欢迎大家转发.留言.想要更多电子杂志的机器 ...

  2. python自然语言处理案例-Python自然语言处理 NLTK 库用法入门教程【经典】

    本文实例讲述了Python自然语言处理 NLTK 库用法.分享给大家供大家参考,具体如下: 在这篇文章中,我们将基于 Python 讨论自然语言处理(NLP).本教程将会使用 Python NLTK ...

  3. [转载] 使用Python中的NLTK和spaCy删除停用词与文本标准化

    参考链接: Python | 用NLTK进行词干分析 概述 了解如何在Python中删除停用词与文本标准化,这些是自然语言处理的基本技术  探索不同的方法来删除停用词,以及讨论文本标准化技术,如词干化 ...

  4. [转载] python无法从nltk中调取文本 from nltk.book import *

    参考链接: 在Python中使用NLTK标记文本 网上查资料说需要将nltk的数据下载到根目录或者默认目录,否则就会出现上图的无法读取文本的情况. 当我把下载的文件放到D盘的根目录后就可以正常输出了 ...

  5. 使用Python中的NLTK和spaCy删除停用词与文本标准化

    概述 了解如何在Python中删除停用词与文本标准化,这些是自然语言处理的基本技术 探索不同的方法来删除停用词,以及讨论文本标准化技术,如词干化(stemming)和词形还原(lemmatizatio ...

  6. Python自然语言处理 NLTK 库用法入门教程【经典】

    @本文来源于公众号:csdn2299,喜欢可以关注公众号 程序员学府 本文实例讲述了Python自然语言处理 NLTK 库用法.分享给大家供大家参考,具体如下: 在这篇文章中,我们将基于 Python ...

  7. Python爬虫识别中文字符和标点符号

    Python爬虫识别中文字符和标点符号,并且保存成txt文档 import requestshref_list = final_df["隐私政策"].values names = ...

  8. Python自然语言处理 NLTK 库用法入门教程

                                                               NLP (Natural Language Processing):自然语言处理 ...

  9. 人工智能为什么用python_使用python实现人工智能的优势

    作为一门高级面向对象编程语言,Python通常被亲切地昵称为"胶水语言".从云端.客户端,到物联网终端,python应用无处不在,同时,Python也是人工智能首选的编程语言.为什 ...

最新文章

  1. Linux下运行Jmeter脚本
  2. ActiveX插件的Z-Index属性无效问题解决
  3. js实现中文转拼音的两种方法
  4. 微信加人的108种方法
  5. visio中绘制空间坐标系,visio导入科学图形包
  6. 工业机器人pallet指令_工业机器人编程指令详解简介,目录书摘
  7. 在AndroidStudio中使用ARCore
  8. linux命令中ll和ls的区别
  9. 几家大厂求职与面试(一):Java必备,不看就是遗憾
  10. [论文解读] A Ranking-based, Balanced Loss Function Unifying Classification and Localisation in Object De
  11. Openpose 配置pose,pose+ face,pose+face+hand的方法
  12. sqlserver如何快速生成不重复的随机数据
  13. Halcon模版匹配
  14. Error running 'XXX': Cannot start process, the working directory 'F:\JavaSe\JavaSe' does not exis
  15. python怎么接单子平台有哪些_17个Python接私活的平台,总有适合你的,有技术就有收入...
  16. 简单解释op(面向过程procedure- oriented)与oo(面向对象object-oriented)
  17. 【CentOS】如何在线安装pcre
  18. “90后”女大学生奇怪 白毛女为何不嫁黄世仁
  19. 腾讯不敢发放的一个QQ号码
  20. 软件工程师能走多远?

热门文章

  1. 基于三极管的声控LED电路/光立方设计方案(无需单片机)
  2. 视频去水印免软件下载-视频去水印哪个软件好用
  3. 信息摘要算法之一:MD5算法解析及实现
  4. 日语入门资料(可下载)
  5. oracle坏块 戴明明,Oracle数据库问题解决方案和故障排除手册
  6. 【论文阅读】Drift with Devil
  7. H3C(华三)IMC与网络行为管理系统用户集成
  8. 《鹿鼎记》的第一遍读后感作文3600字
  9. 树莓派配置小记1:系统篇
  10. FreeRapid v0.9