Python 通过将标点符号与单词(而不是缩写和撇号)分开来标记文本

Python 通过将标点符号与单词(而不是缩写和撇号)分开来标记文本,python,nltk,tokenize,Python,Nltk,Tokenize,我输入了一些文本,我想通过将标点符号与单词分开,同时考虑缩写和撇号来标记这些文本。我正在使用python和nltk库,但我认为我的正则表达式不正确,因为我仍然得到错误的输出 # coding: utf-8 import re import nltk from nltk.tokenize import * text = "\"Predictions suggesting that large changes in weight will accumulate indefinitely in r

我输入了一些文本,我想通过将标点符号与单词分开,同时考虑缩写和撇号来标记这些文本。我正在使用python和nltk库,但我认为我的正则表达式不正确,因为我仍然得到错误的输出

# coding: utf-8
import re
import nltk
from nltk.tokenize import *

text = "\"Predictions suggesting that large changes in weight will 
accumulate indefinitely in response to small sustained lifestyle 
modifications rely on the half-century-old 3,500 calorie rule, which 
equates a weight alteration of 2.2 lb to a 3,500 calories cumulative 
deficit or increment,\" write the study authors Dr. Jampolis, Dr. 
Chaudry, and Prof. Harlen, from N.P.C Clinic in OH. The 3,500- calorie 
rule \"predicts that a person who increases daily energy expenditure by 
100 calories by walking 1 mile per day\" will lose 50 pounds over five 
years, the authors say. But the true weight loss is only about 10 
pounds if calorie intake doesn't increase, \"because changes in mass 
... alter the energy requirements of the body’s make-up.\" \"This is a 
myth, strictly speaking, but the smaller amount of weight loss achieved 
with small changes is clinically significant and should not be 
discounted,\" says Dr. Melina Jampolis, CNN diet and fitness expert."

print(regexp_tokenize(text, pattern='(?:(?!\d)\w)+|\S+') )

非常感谢您的帮助。

这应该可以解决问题。在这里使用re.sub将任何不需要的标点符号替换为零(即“')是有意义的

这个正则表达式最困难的部分是避开所有的反斜杠。要分解这一点:

(\"\\\")
查找任何“\”

查找任何\“

找到任何

,
找到任何


管道充当“或”操作符。希望这能满足您的所有要求。

这应该可以做到。在此处使用re.sub来替换任何不需要的标点符号(即“.”)是有意义的

这个正则表达式最困难的部分是避开所有的反斜杠。要解决这个问题:

(\"\\\")
查找任何“\”

查找任何\“

找到任何

,
找到任何


管道充当“或”操作符。希望这能满足您的所有要求。

我不清楚您想要的输出是什么。想要的输出将是标记化文本,但不分离带有标点符号的单词,如撇号(不作为一个标记保留),以及缩写(N.p.C.作为一个标记保留),所以您基本上只想删除“/”、“\”、“,”还有引号?是的,还有“…”如果这是琐碎的话,很抱歉,我正在尝试学习如何使用nltk库,有些小事情让我感到困惑。我不清楚您想要的输出是什么。想要的输出将是标记化文本,但不分离带有标点符号(如撇号)的单词(不作为一个标记),还有缩写(N.P.C.作为一个标记),所以你基本上只想删除“/”、“\”、“、”和引号?是的,还有“…”对不起,如果这是琐碎的,我正在尝试学习如何使用nltk库,有些小事情让我困惑。
,