我需要识别所有的缩写和连词在我的句子开始。当他们被识别出来时,需要打印出来。对于此标识,我的代码似乎不能很好地工作。
import re
sentence_stream2=df1['Open End Text']
for sent in sentence_stream2:
abbs_ = re.findall(r'(?:[A-Z]\.)+', sent) #abbreviations
hypns_= re.findall(r'\w+(?:-\w+)*', sent) #hyphenated words
print("new sentence:")
print(sent)
print(abbs_)
print(hypns_)我的语料库中的一句话之一是:使用云数据分析环境的DevOps与APIs &事件驱动的体系结构,自助服务BI。
这方面的产出是:
new sentence:
DevOps with APIs & event-driven architecture using cloud Data Analytics environment Self-service BI
[]
['DevOps', 'with', 'APIs', 'event-driven', 'architecture', 'using', 'cloud', 'Data', 'Analytics', 'environment', 'Self-service', 'BI']预期产出如下:
new sentence:
DevOps with APIs & event-driven architecture using cloud Data Analytics environment Self-service BI
['APIs','BI']
['event-driven','Self-service']发布于 2019-10-01 05:46:21
缩略语的规则不匹配。如果您想要找到超过一个连续大写字母的单词,您可以使用的规则是:
abbs_ = re.findall(r'(?:[A-Z]{2,}s?\.?)', sent) #abbreviations这将与API和BI相匹配。
t = "DevOps with APIs & event-driven architecture using cloud Data Analytics environment Self-service BI"
import re
abbs_ = re.findall(r'(?:[A-Z]\.)+', t) #abbreviations
cap_ = re.findall(r'(?:[A-Z]{2,}s?\.?)', t) #abbreviations
hypns_= re.findall(r'\w+-\w+', t) #hyphenated words fixed
print("new sentence:")
print(t)
print(abbs_)
print(cap_)
print(hypns_)输出:
DevOps with APIs & event-driven architecture using cloud Data Analytics environment Self-service BI
[] # your abbreviation rule - does not find any capital letter followed by .
['APIs', 'BI'] # cap_ rule
['event-driven', 'Self-service'] # fixed hyphen rule这很可能找不到所有的缩写,比如
t = "Prof. Dr. S. Quakernack"所以你可能需要用更多的数据和f.e来调整它。http://www.regex101.com
发布于 2019-10-01 05:48:27
我建议:
abbs_ = re.findall(r'\b[A-Z]+s?\b', sent) #abbreviations
hypns_ = re.findall(r'\w+(?:-\w+)*', sent) #hyphenated words发布于 2019-10-01 06:00:08
如你所知,我在我的课程中得到了一切
"As“是缩写词吗?如果没有,那么您需要丢弃单个大写字母后面的Ss,并且只收集至少对,可选地后面跟着一个在API中。所以,
abbs_ = re.findall(r'\b(?:[A-Z][A-Z]+s?)\b', sent) #abbreviations\b需要确保您也不会收获诸如ImNotAGirl之类的东西,因为在中间是AG对。
然后你必须得到缩写:一个单词(\w+),后面至少有一个连字符-词序列:
hypns_= re.findall(r'\b(?:\\w+(-\w+)+)\b', sent) #hyphenated wordshttps://stackoverflow.com/questions/58178639
复制相似问题