简体   繁体   中英

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.

For example "Text = " There are thousands of types of specific networks proposed by researchers as modifications or tweaks to existing models"

here if i input as networks and researchers , then i should get output as "Proposed by" or "networks proposed by 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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM