如何在python中找到文本分析中的连接词?

2024-05-18 23:42:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我想检查文本分析中两个单词之间的联系python.当前在python中使用NLTK包。在

例如 "Text = "研究人员提出了数千种特定网络,作为对现有模型的修改或调整。”

在这里,如果我以networksresearchers的形式输入,那么我应该将输出作为 “提议人” 或“研究人员提出的网络”


Tags: text模型文本网络人员单词形式nltk
2条回答

你可以用正则表达式匹配这两个词

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))

汤姆的回答更清楚。这是我的答案,不需要额外的库

找到每个单词的位置,然后使用这些位置提取它

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])

相关问题 更多 >

    热门问题