简体   繁体   中英

Trying to search for follow up questions in a text file

I have a piece of code that will search through a transcript of a call between a marketer and a customer. The code scans through the transcript line by line searching for a phrase specified in an external txt file, if it finds a match, it prints the entire line the match is on, followed by the next line, which is the response.

I've developed a bit of code that will identify if 'yes' has been said to one question, which will lead to more follow up questions, however, I cannot get it to loop through the rest of the script searching for these follow up questions after the line?

Could anyone give me a hand?

I have the following code:

with open ('/Users/owenmurray/Desktop/untitled folder/untitled folder/transcribe.txt') as my_new_file:
    contents = my_new_file.read()

partner_file = open('/Users/owenmurray/Desktop/untitled folder/untitled folder/P.txt')

with open('/Users/owenmurray/Desktop/untitled folder/untitled folder/follow_up_question.txt') as follow_up_question_file:
    follow_up = follow_up_question_file.read()

partners = partner_file.readlines()
# Converts it to a list
lines = contents.split("\n")
follow_up_list = follow_up.split("\n")
for p in partners:
    try:
        output = None
        for index, line in enumerate(lines):
                if p.strip() in line:
                    output = index
                    break
        if output:
            print ("\n" + lines[output] + "\n")
            print("-------------------------------------------------------------")
            print("\n" + lines[output +1] + "\n")
            if "yes" in lines[output +1].lower() or "yeah" in lines[output + 1].lower():
                print("-------------------------------------------------------------")
                print ("\n" + lines[output +2] + "\n")
                try:
                    for follow in follow_up_list():
                        if follow in lines[output+2].lower():
                            True
                        print("-------------------------------------------------------------")
                        print ("\n" + lines[output +3] + "\n")
                        break
                except (ValueError):
                    print("Nothing found")
                    break
    except:
        pass

An example of my transcript can be found here:

https://paste.pythondiscord.com/obucaweyuc.py

My p.txt can be found here:

have you spoken with a ARM partner in the last six months

And my follow_up_questions.txt has:

did you talk about similar issues?

However the current output only displays:

ch_0 : have you spoken with a ARM partner in the last six months about having a discussion about how ARM those, um, quality security could benefit you guys?


ch_1 : Yeah, we have


ch_0 : Oh, okay and did you talk about similar issues?

I have cleaned up your code a bit just to try to simplyfy the problem. I have included the output it produces, as per your comment it gives the line after

with open('transcribe.txt') as t_file, open('p.txt') as p_file, open('follow_up_question.txt') as f_file:
    t_lines = t_file.readlines()
    f_lines = f_file.readlines()
    p_lines = p_file.readlines()
    for p_line in p_lines:
        for index, line in enumerate(t_lines):
            if p_line.strip() in line:
                print(f'{t_lines[index]}{"-"*30}\n{t_lines[index + 1]}', end='')
                if "yes" in t_lines[index + 1].lower() or "yeah" in t_lines[index + 1].lower():
                    print(f'{"-"*30}\n{t_lines[index + 2]}', end='')
                    for follow in f_lines:
                        if follow in t_lines[index + 2].lower():
                            print(f'{"-"*30}\n{t_lines[index + 3]}', end='')

OUTPUT

ch_0 :  have you spoken with a ARM partner in the last six months about having a discussion about how ARM those,  um,  quality security could benefit you guys? 
------------------------------
ch_1 :  Yes, we have
------------------------------
ch_0 : Oh, okay and did you talk about similar issues?
------------------------------
ch_0 :  Okay,  Uh,  would you be willing to,  um,  have a discussion with ARM partner?  Um,  it's project making business sense

However just for your reference the issue in your code was this line

for follow in follow_up_list():

follow_up_list is a python list and is not callable so you need to drop the parentheses

for follow in follow_up_list:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM