简体   繁体   English

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

[英]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" 在这里,如果我以networksresearchers身份输入,那么我应该以“提议者”或“研究人员提议的网络”形式输出

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.

相关问题 如何使用Python在文本文件中查找单词 - How to find words in text files with Python 使用python查找字符串中连接词的出现次数 - Find occurrences of connecting words in a string with python 如何在文本中查找特定单词并使用 python 计算它们? - How to find specific words in a text and count them using python? 如何在 python 的文本文件中查找重复 5 次的单词? - How to find words repeated 5 times in a text file in python? 如何在单独的文本文件中找到特定的单词? (蟒蛇) - How to find specific words in a separate text file? (PYTHON) Python:如何在字符串中的某些单词之间找到文本? - Python: How can I find text between certain words in a string? 如何在python的500个文本文件中找到500个最常用的单词? - How to find 500 most frequent words in 500 text files in python? 如何在文本文件中查找单词? - How to find words in a text file? 在进行文本分析时如何处理印地语单词? [等候接听] - How to deal with hindi words while doing text analysis? [on hold] 什么是仅读取文本文件中完整单词的 python 代码(词法分析仅检测整个单词)? - What is the python code to read only full words in a text file ( lexical analysis to only detect whole words)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM