import nltk
sent = "I am good. I e.g. wash the dishes."
pattern = r'''(?x) # set flag to allow verbose regexps
(?:[A-Za-z]\.)+ # abbreviations(both upper and lower case, like "e.g.", "U.S.A.")
| \w+(?:-\w+)* # words with optional internal hyphens
| [][.,;"'?():_`-] # these are separate tokens; includes ], [
'''
nltk.regexp_tokenize(sent, pattern)
#Output:
['I', 'am', 'good', '.', 'I', 'e.g.', 'wash', 'the', 'dishes', '.']
NLTK regexp_tokenize模块使用正则表达式将字符串拆分为子字符串。可以定义一个regex
pattern
,它将构建一个与此模式中的组匹配的标记器。我们可以为您的特定用例编写一个模式,它查找单词、缩写(大写和小写)和符号,如'.'
,';'
等缩写的Regex模式是
(?:[A-Za-z]\.)+
。\.
与包含a-Z或a-Z字符的前向查找中的"."
匹配另一方面,在以下模式中,句号作为独立符号匹配,该模式不绑定到一组字母表中的正或负先行或包含:
^{pr2}$相关问题 更多 >
编程相关推荐