python - 如何使用 RegExpTokenizer 排除所有小写字母 (a-z) 的组合?

标签 python regex python-3.x list nltk

我需要使用 regexptokenizerpattern 选项删除所有小写字母组合。有什么办法吗?

我尝试过的方法如下:

import re
from nltk import regexp_tokenize
data = {'fresh air', 'entertainment system', 'ice cream', 'milk', 'dog', 'blood pressure', 'body temperature', 'car', 'ac', 'auto', 'air quality'}
data = {i: i.replace(" ", "_") for i in data}
pattern = re.compile(r"\b("+"|".join(data)+r")\b")
text_file = ['A is\'s vitamin-d in===(milk) "enough, carrying 321 active automatic body hi+al.', '{body temperature} [try] to=== improve air"s quality level by automatic intake of fresh air.', 'turn on the tv or entertainment system based on that individual preferences', 'blood pressure monitor', 'I buy more ice cream', 'proper method to add frozen wild blueberries in ice cream']
result = [pattern.sub(lambda x: "{}".format(data[x.group()]), i) for i in text_file]
tokens = [[word for word in regexp_tokenize(word, pattern=r"\s|[0-9!()\-+\$%;,.:@'\"/={}\[\]\']", gaps=True)] for word in result]  
print(tokens)

注意:我需要保留其当前形式的输出。我所需要的只是排除小写字母。提前致谢。

添加[^a-z]对我来说根本不起作用,并且它省略了某些单词中注入(inject)的下划线符号,我不能失去它。

最佳答案

尝试这个模式:

pattern=r"\s|[0-9!()\-+\$%;,.:@'\"/={}\[\]\'].|[^\w a-z]"

关于python - 如何使用 RegExpTokenizer 排除所有小写字母 (a-z) 的组合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56537204/

相关文章:

python - 如何过滤字典以仅选择大于值的键?

regex - ColdFusion REGEX - 确定文件的扩展名

python - 如何在 Python 3.x 中打开给定绝对路径的文件

python - 在Python OpenCV中将字符串渲染为库属性

Python 3.6 DateTime Strptime 返回错误,而 Python 3.7 运行良好

Python:使用可哈希对象访问字典失败

python - 为什么我不能比较 python 2.7 中的集合和不可变集合

php - 用于提取嵌套元素的正则表达式

python-3.x - Pandas 正则表达式提取两个不同符号前后的所有内容

python-3.x - MD5 哈希,Python 3。如何在 Python 中生成