【发布时间】:2017-02-22 19:21:05
【问题描述】:
我想要的是将域名保留为单个令牌。例如:“https://www.twitter.com”应保留为单个令牌。
我的代码:
import nltk
from nltk.tokenize.regexp import RegexpTokenizer
line="My website: http://www.cartoon.com is not accessible."
pattern = r'^(((([A-Za-z0-9]+){1,63}\.)|(([A-Za-z0-9]+(\-)+[A-Za-z0-9]+){1,63}\.))+){1,255}$'
tokeniser=RegexpTokenizer(pattern)
print (tokeniser.tokenize(line))
输出:
[]
我做错了什么?有更好的域名正则表达式吗?
编辑:特殊字符必须保留为单独的标记,就像上面的示例一样,标记化必须分开 ('website' , ':')。
【问题讨论】:
-
有点懂,一直在this上用,不知道怎么用!
-
也许
tokeniser=RegexpTokenizer(r'\S+')只抓取非空白块就足够了。 -
@WiktorStribiżew,这很好!但我仍然需要将特殊字符分隔为标记。例如: ('accessible' , '.')
-
请在问题中添加这些详细信息。
标签: python regex nltk tokenize