nltk RegexpTokenizer类:python自然语言处理


前面的一些分词工具都是写好的的规则

如果我们想按照自己的规则进行分词 可以使用正则分词器

1.RegexpTokenizer类

from nltk.tokenize import RegexpTokenizer

text = " I won't just survive, Oh, you will see me thrive. Can't write my story,I'm beyond the archetype."

# 实例化RegexpTokenizer 会按照正则表达式进行re.findall()
regexp_tokenizer = RegexpTokenizer(pattern="\w+")
# 实例化RegexpTokenizer 指定gaps=True会按照正则表达式进行re.split()
regexp_tokenizer1 = RegexpTokenizer("[\s,'\.]", gaps=True)
print(regexp_tokenizer.tokenize(text))
# ['I', 'won', 't', 'just', 'survive', 'Oh', 'you', 'will', 'see', 'me', 'thrive', 'Can', 't', 'write', 'my', 'story', 'I', 'm', 'beyond', 'the', 'archetype']
print(regexp_tokenizer1.tokenize(text))
# ['I', 'won', 't', 'just', 'survive', 'Oh', 'you', 'will', 'see', 'me', 'thrive', 'Can', 't', 'write', 'my', 'story', 'I', 'm', 'beyond', 'the', 'archetype']

---------------------
作者:qq_41864652
来源:CSDN
原文:https://blog.csdn.net/qq_41864652/article/details/81505768
版权声明:本文为博主原创文章,转载请附上博文链接!


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM