python - Python:NLTK - 正则表达式标记器产生空输出
问题描述
我试图标记 NLTK 教科书上可用的文本(使用 python 2.7),但输出不符合预期。有什么我想念的吗?
text = 'That U.S.A. poster-print costs $12.40...'
pattern = r'''(?x) # set flag to allow verbose regexps
([A-Z]\.)+ # abbreviations, e.g. U.S.A.
| \w+(-\w+)* # words with optional internal hyphens
| \$?\d+(\.\d+)?%? # currency and percentages, e.g. $12.40, 82%
| \.\.\. # ellipsis
| [][.,;"'?():-_`] # these are separate tokens; includes ], [
'''
nltk.regexp_tokenize(text, pattern)
Output:
[('', '', ''),
('A.', '', ''),
('', '-print', ''),
('', '', ''),
('', '', '.40'),
('', '', '')]
Expected:
['That', 'U.S.A.', 'poster-print', 'costs', '$12.40', '...']
解决方案
推荐阅读
- visual-studio - Visual Studio 抑制未定义的 SASS 变量上的错误
- single-sign-on - 在自定义 Azure B2C SSO HTML 页面中使用“href=tel:”
- python - Keras 后端函数中的 TensorBoard 回调
- javascript - 如何从在 NestJS CQRS 的传奇中失败的后续命令引发 HTTP 异常?
- ruby-on-rails - 如何在本地主机上为 Ruby on Rails 应用程序运行 SSL 证书?
- javascript - 将文件从 s3 存储桶写入本地后返回状态
- php - 使用 CodeIgniter 框架在实时生产中不会触发 CSS
- html - 如何在css中制作双角边缘
- javascript - Gumroad Embedded Widget 在角度导航时中断
- html - 如何为每个容器创建不同的滚动条