vishwa
vishwa

Reputation: 41

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"

Upvotes: 0

Views: 1159

Answers (2)

Patrick
Patrick

Reputation: 334

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

Upvotes: 1

Tom
Tom

Reputation: 725

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

Upvotes: 1

Related Questions