python - 用于识别关键字的正则表达式

标签 python

我想检查文本数据中是否存在关键字, 关键词是,

Keywords=["just checking to see if you are there so we can continue.",
          "please let me know if you're receiving my responses or i will need to end our session", 
          "our chat session is now ending. thanks for choosing at&t!  we appreciate your business."]


Text= "Agent 'jl759s' enters chat (as Jacqueline)", "Hi Cristina!  My name is Jacqueline.  I'm happy to help.", 
  'Sure.', 
  'I see that you have issue with the internet service.', 
  "I'm sorry to hear that Cristina. Let me check that for you", 
  'Please not to worry, I can help you with that!', 
  'have you rebooted the router recently?', 
  'Thanks for the info!', 
  'Let me quickly run a line test to check if there is any issue detected either with the network lines or with the modem.',
  "You're welcome.",
  'Thank you for waiting Cristina.',
  'I wish I could resolve this issue through the chat session however it looks like this particular issue demands the expertise of the field technician.',
  'I understand that it is a little inconvenient to be waiting for a technician however we want to ensure that there is a permanent solution to this issue. I will do my best to get the earliest appointment available for you.',
  'I will dispatch a technician to the premise to help you on this.',
  'I am cehcking on the earliest available time slots now.',
  '*checking',
  'I see the earliest time slot available for the technician visit is on 10/16/2018 between 2:00 PM - 4:00 PM. Will it work for you?',
  'Ye sure.',
  'the technician will call you before the arrival.',
  'I will check that as well.',
  'The appointment is available on 10/20/2018.',
  'The available timings are 8:30 AM - 9:30 AM, 10:00 AM - 12:00 PM, 12:00 PM - 2:00 PM, 2:00 PM - 4:00 PM and 4:00 PM - 8:00 PM.',
  'Yes Cristina. Its available.',
  'Sure Cristina.',
  'Give me a minute.',
  'I have scheduled your appointment for October 16,
  xxxx. An AT&T technician will arrive as early as 4:00 PM or as late as 8:00 PM.',
  'Your service call may take 2-4 hours after arrival to resolve the issue.',
  'Please make sure all AT&T equipment is accessible to do repairs. Our technician will not move any furniture.',
  'An adult 18 years of age or older must be on-site for the duration of your Service Call and reachable on the day of the service call at 5863854186.',
  'With just two taps, you will able to track your repair/install appointment using the myAT&T app.',
  'Please launch the myAT&T app, input your member id and password, and then tap login. By tapping on the alert you will see all of your appointment details.',
  'Just to recap, You have reached us for the Internet service issue, as there is a line issue detected while troubleshooting I have dispatched a technician to help you fix this issue.',
  'I hope you do not have any concern with the assistance provided to you today. Is there anything else I can help you with I will be happy to assist?',
  'Pleasure is Mine Cristina!',
  'It was pleasure working with you!',
  'I appreciate your patience on this.',
  'Thank you for choosing AT&T. We appreciate your business. Have a great day!',
  'Bye!'

我编写了以下代码,

def Key_words(y):
    if(any(bool(re.search(r'\b'+x+r'\b', ''.join(y).lower()))) for x in keywords):
        return("Yes")
    else:
        return("No")

Key_words(Text)

它给出的输出是 但输出应该是No,因为文本中没有匹配的关键字。

请帮助我获得正确的输出。

最佳答案

这是使用re.search的一种方法。

例如:

import re

keywords=["just checking to see if you are there so we can continue.",  "please let me know if you're receiving my responses or i will need to end our session", "our chat session is now ending. thanks for choosing at&t!  we appreciate your business."]
Text= ["Agent 'jl759s' enters chat (as Jacqueline)", "Hi Cristina!  My name is Jacqueline.  I'm happy to help.", 'Sure.', 'I see that you have issue with the internet service.', "I'm sorry to hear that Cristina. Let me check that for you", 'Please not to worry, I can help you with that!', 'have you rebooted the router recently?', 'Thanks for the info!', 'Let me quickly run a line test to check if there is any issue detected either with the network lines or with the modem.', "You're welcome.", 'Thank you for waiting Cristina.', 'I wish I could resolve this issue through the chat session however it looks like this particular issue demands the expertise of the field technician.', 'I understand that it is a little inconvenient to be waiting for a technician however we want to ensure that there is a permanent solution to this issue. I will do my best to get the earliest appointment available for you.', 'I will dispatch a technician to the premise to help you on this.', 'I am cehcking on the earliest available time slots now.', '*checking', 'I see the earliest time slot available for the technician visit is on 10/16/2018 between 2:00 PM - 4:00 PM. Will it work for you?', 'Ye sure.', 'the technician will call you before the arrival.', 'I will check that as well.', 'The appointment is available on 10/20/2018.', 'The available timings are 8:30 AM - 9:30 AM, 10:00 AM - 12:00 PM, 12:00 PM - 2:00 PM, 2:00 PM - 4:00 PM and 4:00 PM - 8:00 PM.', 'Yes Cristina. Its available.', 'Sure Cristina.', 'Give me a minute.', 'I have scheduled your appointment for October 16, xxxx. An AT&T technician will arrive as early as 4:00 PM or as late as 8:00 PM.', 'Your service call may take 2-4 hours after arrival to resolve the issue.', 'Please make sure all AT&T equipment is accessible to do repairs. Our technician will not move any furniture.', 'An adult 18 years of age or older must be on-site for the duration of your Service Call and reachable on the day of the service call at 5863854186.', 'With just two taps, you will able to track your repair/install appointment using the myAT&T app.', 'Please launch the myAT&T app, input your member id and password, and then tap login. By tapping on the alert you will see all of your appointment details.', 'Just to recap, You have reached us for the Internet service issue,  as there is a line issue detected while troubleshooting I have dispatched a technician to help you fix this issue.', 'I hope you do not have any concern with the assistance provided to you today. Is there anything else I can help you with I will be happy to assist?', 'Pleasure is Mine Cristina!', 'It was pleasure working with you!', 'I appreciate your patience on this.', 'Thank you for choosing AT&T. We appreciate your business. Have a great day!', 'Bye!']


def Key_words(y):
    if re.search("(" +"|".join(keywords) + ")", r"\n".join(y), flags=re.IGNORECASE):
        return("Yes")
    else:
        return("No")

print(Key_words(Text))

关于python - 用于识别关键字的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53722662/

相关文章:

python - 值错误: Classification metrics can't handle a mix of multilabel-indicator and binary targets

在发送 spider_closed 信号之前调用 Python Scrapy 函数?

python - Tfidftransformer 和 Tfidfvectorizer 有什么区别?

Python:生成一定范围内的随机 float ,不包括起点

python - 在 Vim 中使用 Python 2 和 3(在 Windows 上)

python - 在 Vim 中使用 pythoncomplete 时防止拆分窗口

python - 在 apache 中运行 Tornado

python - 解析像 'ssh://git@gitlab.org.net:3333/org/repo.git' 这样的 git URL 吗?

Python 相当于 PyErr_Print()

python - 字典推导式中是否允许使用 lambda?