Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python match target word

For each target word, I want to check if the next words that appear before the next target word are corresponding to what I've set in a document. If match, I want it to output to true and write out to txt file. If false, write out false.

I'm using regex, but iteration way is fine

import re
re.findall("([a-zA-Z]+) " + tofind, txt)

Target and next words:

target word: document
next words: set is complete

Sample Doc:

Document that I set is complete now. Document is great set. Is document is great complete document set is complete. Document is complete document is good but not complete.

Document appears 6 times in this excerpt, but I want it to return and output the below to txt file

first document -> true
second document -> false
third document -> false
fourth document -> true
fifth document -> false
sixth document -> false
like image 769
user5942037 Avatar asked Nov 09 '22 20:11

user5942037


1 Answers

Don't use regular expressions for this task, instead, string splicing will suffice. An example of a simple way:

sampleDoc = "Document that I set is complete now. Document is great set. Is document is great complete document set is complete. Document is complete document is good but not complete.".lower()
findWord = "document".lower()
wordToFind = "set is complete".lower()
splitList = sampleDoc.split(findWord)
splitList.pop(0)
for position,phrase in enumerate(splitList):
    if wordToFind in phrase:
        print("Document Number", str(position+1), "-> true")
    else:
        print("Document Number", str(position+1), "-> false")

We split the text on every word that we're trying to find, sending it to a list. We iterate over this list and if the important words are found, we output true, or if not, we output false.

like image 146
Alexander Craggs Avatar answered Nov 15 '22 11:11

Alexander Craggs