Home > Software design >  Check which word appear first in sentence with list of array using python
Check which word appear first in sentence with list of array using python

Time:10-01

list = ["Sales Order", "Sales Invoice", "Payment no"]
sentence = "This is the Sales Order which have Sales Invoice of number 8976245"

Since the "Sales Order" occurs first in sentence the answer is Sales Order. Is there any solution using python

CodePudding user response:

word_list = ["Sales Order", "Sales Invoice", "Payment no"]
sentence = "This is the Sales Order which have Sales Invoice of number 8976245"
#Assuming no word is fully part of another word

#sort list by length
word_list.sort(key=len)

#get shortest element
min_word_length = len(word_list[0])

#start looking for words, starting with the minimum length of a word, adding it's length if no word is found
for ending in range(min_word_length, len(sentence)   1, min_word_length - 1):
    for word in word_list:
        if word in sentence[:ending]:
            print(word)
            exit()

print('None of the words found')

CodePudding user response:

2 solutions you can try

  • Use regex to "or" | all the words to find and then search the string. It will return the first occurrence
  • Or you can also just manually find per word in the sentence
import re


words = ["Sales Order", "Sales Invoice", "Payment no"]
for sentence in [
    "This is the Sales Order which have Sales Invoice of Payment no 8976245",
    "This is the which have Sales Invoice of Payment no Sales Order 8976245",
    "This is the which have of Payment no Sales Order Sales Invoice 8976245",
    "This is the which have of Sales Order Sales Invoice Payment no 8976245",
    "This is the which have of 8976245",
]:
    print(sentence)

    # Solution 1
    words_re = re.compile("("   "|".join(words)   ")", flags=re.IGNORECASE)
    match = words_re.search(sentence)
    if match:
        print("\t", match.group(1))
    else:
        print("\t", None)

    # Solution 2:
    indexes = {}
    for word in words:
        indexes[sentence.find(word)] = word
    indexes.pop(-1, None)
    if indexes:
        print("\t", indexes[min(indexes)])
    else:
        print("\t", None)

Output

This is the Sales Order which have Sales Invoice of Payment no 8976245
     Sales Order
     Sales Order
This is the which have Sales Invoice of Payment no Sales Order 8976245
     Sales Invoice
     Sales Invoice
This is the which have of Payment no Sales Order Sales Invoice 8976245
     Payment no
     Payment no
This is the which have of Sales Order Sales Invoice Payment no 8976245
     Sales Order
     Sales Order
This is the which have of 8976245
     None
     None
  • Related