CAO RUI
CAO RUI

Reputation: 43

match sentence in multilines

I got a problem to slove how to matching a sentence in a paragraph. the sentences in the paragraph will be in multilines,usually a line will be a part of a sentence. for example:

search_str = "I have a pen"

paragraph can be like:

My name is XXX. 
I have # 01
a pen which I like it. # 02
Because I have a pen. So I #03
can have a pen pen. # 04
I have a pen. I have a pen xxx. # 05

the desired output is:

I have
a pen
I have a pen
\[nothing...\]
I have a pen I have a pen
search_sentence = "for learning distinctive features among"

raw_sentences = ["methods it is possible to among recognize Instagram filters and at-", # 01
                 "tenuate the sensor pattern noise signal in images. Amerini", # 02
                 "et al. [10] introduced a CNN for learning distinctive features", # 03
                 "among social networks. for learning distinctive features among from the histogram of the discrete co-", # 04
                 "sine transform (DCT) coefficients and the noise residual of", # 05
                 "the images. Phan et al. [11] proposed a method to track mul-", # 06
                 "tiple image sharing on social networks by using a CNN for ar-", # 07
                 "chitecture able to learn", # 08
                 "et al. [10] introduced a CNN for learning distinctive features among it is possible to among recognize Instagram filters", # 09
                 "and at- tenuate xx"] # 10


def longest_intersection(string1, string2):
    list1 = string1.split()
    list2 = string2.split()
    intersection = []
    for word in list1:
        if word in list2 and word == list2[0]:
            intersection.append(word)
            list2.remove(word)
    if " ".join(intersection) in search_sentence:
        return intersection


for line in raw_sentences:


    one_line_match = ' '.join(longest_intersection(line.strip(), search_sentence))


    if one_line_match != "" and one_line_match[0] == search_sentence[0]:
        print(one_line_match)
        search_sentence = search_sentence.replace(one_line_match, "").strip()
        if search_sentence == "":
            search_sentence = "for learning distinctive features among"
    else:
        print("[no matched sentences!]")
        search_sentence = "for learning distinctive features among"

for now, my outputs are:

[no matched sentences!] [no matched sentences!] for learning distinctive features among [no matched sentences!] [no matched sentences!] for [no matched sentences!] for learning distinctive features among [no matched sentences!]

but I desired output will be like this:

[no matched sentences!]
[no matched sentences!]
for learning distinctive features
among for learning distinctive features among
[no matched sentences!]
[no matched sentences!]
[no matched sentences!]
[no matched sentences!]
for learning distinctive features among
[no matched sentences!]

Upvotes: 1

Views: 44

Answers (0)

Related Questions