[英]how find the connecting words in text analysis in python?
I wanted to check the connection between 2 words in text analytics in python.currently using NLTK package in python. 我想检查python文本分析中2个单词之间的连接。当前使用python中的NLTK包。
For example "Text = "
There are thousands of types of specific networks proposed by researchers as modifications or tweaks to existing models" 例如, "Text = "
研究人员提出了成千上万种特定的网络类型,以对现有模型进行修改或调整。
here if i input as networks
and researchers
, then i should get output as "Proposed by" or "networks proposed by researchers" 在这里,如果我以networks
和researchers
身份输入,那么我应该以“提议者”或“研究人员提议的网络”形式输出
You could Regex match between the two words 你可以在两个词之间匹配正则表达式
import re
word_one = "networks"
word_two = "researchers"
string = "There are thousands of types of specific networks proposed by researchers as modifications or tweaks to existing models"
result = re.search(f'{word_one}(.+?){word_two}', string)
print(result.group(1))
Tom's answer is cleaner. 汤姆的答案更干净。 Here is my answer without additional libraries 这是我的回答,没有其他库
Find the locations of each word, then use those locations to extract it 找到每个单词的位置,然后使用这些位置提取它
text = "There are thousands of types of specific networks proposed by researchers as modifications or tweaks to existing models"
word1 = "networks"
word2 = "researchers"
start = text.find(word1)
end = text.find(word2)
if start != -1 and end != -1 and start < end:
print(text[start + len(word1):end])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.