juanman
juanman

Reputation: 147

Find word after a match if word in list in python

How to find the word after a match, if word in list? For example, I want to find the word after match1 if this word is in list:

r = ["word1", "word2", "word3"]

If found, then return the word(i). If not, then return Unknown.

Toy example:

Text1 = "This is a match1 for example match1 random text match1 anotherword"
Text2 = "This is a match1 word1 example"
Text3 = "This is an example without word of interest"

I want to look the word after match1 if this word is in list r = ["word1", "word2", "word3"] Expected result: For Text1 I expect to get Unknown, for Text2 word1, and for Text3 Unknown.

I have managed so far to extract "word1" only if it's among the first two occurences, but if for example we have Text4 (below) I can'textract it because I'm only going until the second time I see the match, and just keep going further and deeper with if-else statements I dont think its the way to go, because the word1 can even not be present at all.

Text4 = "example match1 example match1 example match1 word1"

def get_labels(text):
    q = ["match1"] #Here the idea is to have several, but its the same logic
    r = ["word1", "word2", "word3"]
    labels = []
    for i,item in enumerate(q):
        label = text[text.find(q[i])+len(q[i]):].split()[0]
        if label in r:
            labels.append(label)
        else:
            texto_temp = text[text.find(q[i])+len(q[i]):]
            label2 = texto_temp[texto_temp.find(q[i])+len(q[i]):].split()[0]
            labels.append(label2)
    return labels

Any ideas will be appreciated.

Upvotes: 0

Views: 696

Answers (3)

Aaditya Ura
Aaditya Ura

Reputation: 12669

You can try Positive Lookbehind (?<=match1\s)

import re
pattern=r'(?<=match1\s)[a-zA-Z0-9]+'

Text1 = "This is a match1 for example match1 random text match1 anotherword"
Text2 = "This is a match1 word1 example"
Text3 = "This is an example without word of interest"
Text4 = "example match1 example match1 example match1 word1"

r = ["word1", "word2", "word3"]

def word_checker(list_):
    data=re.findall(pattern,list_)
    list_data=[i for i in data if i in r]
    if list_data:
        return list_data[0]
    else:
        return 'Unknown'

output:

print(word_checker(Text1))
print(word_checker(Text2))
print(word_checker(Text3))
print(word_checker(Text4))

output:

Unknown
word1
Unknown
word1

Upvotes: 0

wjk2a1
wjk2a1

Reputation: 836

If I understand you correctly. This should work:

def get_labels(text):
    q = ['match1']
    r = ['word1', 'word2', 'word3']
    labels = []
    terms = text.split()
    for i, term in enumerate(terms[:-1]):
        if term in q and terms[i+1] in r:
            labels.append(terms[i+1])
    return labels if labels else 'Unknown'

Upvotes: 1

Sven-Eric Kr&#252;ger
Sven-Eric Kr&#252;ger

Reputation: 1327

Use can use regular expressions to find the matches.

Code

from __future__ import print_function
import re

def get_labels(text, match, words)
    tmp = re.findall(r'(?<={})\s+({})'.format(match, '|'.join(words)), text)

    return tmp if tmp else "Unknown"

Text1 = "This is a match1 for example match1 random text match1 anotherword"
Text2 = "This is a match1 word1 example"
Text3 = "This is an example without word of interest"
Text4 = "example match1 example match1 example match1 word1"

match = "match1"
words = ["word1", "word2", "word3"]

print(get_labels(Text1, match, words))
print(get_labels(Text2, match, words))
print(get_labels(Text3, match, words))
print(get_labels(Text4, match, words))

Console Output

Unknown
['word1']
Unknown
['word1']

Ask for more detail, if you are in need...

Upvotes: 1

Related Questions