简体   繁体   中英

Extract Index of a sentence where the keyword is matched using python

i want to extract the index number of a sentence where the keyword is matched in the text using python regular expressions. the key word is "I can help you with that" And the text data is,

keyword=["I can help you with that"]

str1=[nv707g]: Agent 'nv707g' enters chat (as Sandra) * [nv707g]: Hi. My name is Sandra. How can I help you? * [nv707g]: Sure, please don't worry. I can help you with that. *** [nv707g]: Can I have a contact number so that we can reach you.

str2=[ta250h]: Agent 'ta250h' enters chat (as Steve) * [ta250h]: Hi. My name is Steve. How can I help you? * [ta250h]: I can help you with that.

str3= * [virtualAssistant.nina]: Hmmm. Could you rephrase your question? Virtual Assistants understand simple questions best. [virtualAssistant.nina]: You will now be connected to a specialist for your issue. [sv0573]: Agent 'sv0573' enters chat (as Rosen) Agent 'virtualAssistant.nina' exits chat [sv0573]: Hello, my name is Rosen. With whom do I have the pleasure of speaking with today? [sv0573]: Hi, Jerone. [sv0573]: I am sorry to know that you have issues with the E-mail. * [sv0573]: I apologize for the inconvenience. I can help you with that. *** [sv0573]: Can I have a contact number so that we can reach you by phone or text with information about your AT&T services?

str4= [sm0036]: Agent 'sm0036' enters chat (as Sean) * [sm0036]: Hi. My name is Sean. How can I help you? [sm0036]: I can see you are typing I am waiting for your response. [sm0036]: I apologize for the inconvenience. I can help you with that. * [sm0036]: I'll find out what is happening and will help you resolve this.

Use for loop for every string and extract the sentence index when ever the keyword is matched.

Thanks in advance.

Convert your conversations into lists, splitting the strings at the * and then parse the elements for the keyword and return the index of the element containing the keyword:

str1="[nv707g]: Agent 'nv707g' enters chat (as Sandra) * [nv707g]: Hi. My name is Sandra. How can I help you? * [nv707g]: Sure, please don't worry. I can help you with that. *** [nv707g]: Can I have a contact number so that we can reach you."

keyword = "I can help you with that"

a = str1.strip().split('[')

def f(L, key_word):
    for i in L: 
        if key_word in i: 
            return L.index(i)

print f(a, keyword)

>>> 2

returns None if the keyword is not in the conversation.

Edit: Seeing how the * doesnt cleanly appear in all strings to denote a new speaker, you probably should use "[" to split your strings.

def f_new(convo, key_word, splitter = "["): 
    c = [e for e in convo.strip().split('[') if e != '']
    for i in c:
        if key_word in i: 
            return c.index(i)

The default splitter is now "[" bute you can change it optionally when calling the function.

As for your comment, heres a pointer: Cleanly define all your strings and put them in a list

convos = [str1, str2, str3, str4]

Then simply loop over them:

for i in convos: 
    print(f_new(i, keyword))

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