Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用nltk和regex为python中的英文文本编写标记器_Python_Regex_Nltk - Fatal编程技术网

使用nltk和regex为python中的英文文本编写标记器

使用nltk和regex为python中的英文文本编写标记器,python,regex,nltk,Python,Regex,Nltk,我想为英文文本编写一个标记器,我正在使用python中nltk模块中的RegExp标记器 这是我现在用来拆分单词的表达: [\w\.]+ (所以像u.s.a这样的东西不会被屠杀。) 问题:同时我想删除单词中的标点:usa 当然,我可以分步骤完成,但我认为必须有一种比仅仅为了删除标点符号而重复整个文本更平滑的方法。 由于它需要可伸缩性,我希望尽可能优化运行时。 我对正则表达式很陌生,也很难理解,所以我很高兴能得到任何帮助。该模块使用的不仅仅是正则表达式(专门训练的集合),而且它自己在缩写方面做

我想为英文文本编写一个标记器,我正在使用python中
nltk
模块中的RegExp标记器

这是我现在用来拆分单词的表达:

[\w\.]+ 
(所以像
u.s.a
这样的东西不会被屠杀。)

问题:同时我想删除单词中的标点:
usa

当然,我可以分步骤完成,但我认为必须有一种比仅仅为了删除标点符号而重复整个文本更平滑的方法。 由于它需要可伸缩性,我希望尽可能优化运行时。
我对正则表达式很陌生,也很难理解,所以我很高兴能得到任何帮助。

该模块使用的不仅仅是正则表达式(专门训练的集合),而且它自己在缩写方面做得很好,真的:

from nltk import sent_tokenize, word_tokenize

text = """
    In recent times, the U.S. has had to endure difficult 
    political times and many trials and tribulations. 
    Maybe things will get better soon - but only with the 
    right punctuation marks. Am I right, Dr.?"""

words = []
for nr, sent in enumerate(sent_tokenize(text, 1)):
    print("{}. {}".format(nr, sent))
    for word in word_tokenize(sent):
        words.append(word)

print(words)
不要在这里用自己的正则表达式重新发明轮子