我需要使用 regexptokenizer
的 pattern
选项删除所有小写字母组合。有什么办法吗?
我尝试过的方法如下:
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/