【问题标题】:How to extract (speaker, text) tuples from earning call transcripts with regex?如何使用正则表达式从通话记录中提取(说话者、文本)元组?
【发布时间】:2019-01-13 12:33:02
【问题描述】:

对于我的硕士论文,我需要从公司盈利电话记录中提取(演讲者、文本)元组。

成绩单采用以下形式:

OPERATOR: Some text with numbers, special characters and linebreaks.

NAME, COMPANY, POSITION: Some text with numbers, special characters and linebreaks.

NAME: Some text with numbers, special characters and linebreaks.

我想从文档中提取所有(说话者、文本)元组。例如:

[("OPERATOR", "Some text with numbers, special characters and linebreaks."), ..]

到目前为止,我已经用 Python 中的re.findall 函数尝试了不同的正则表达式。

这是一个示例摘录:

example = """OPERATOR: Good day, ladies and gentlemen, and welcome to the first-quarter 2012
Agilent Technologies earnings conference call. My name is Keith, and I will be
your operator for today. At this time, all participants are in a listen-only
mode. Later on, we will have a question and answer session. (Operator
Instructions) As a reminder, today's conference is being recorded for replay
purposes.

And I would now like to turn the conference over to your host for today, Ms.
Alicia Rodriguez, Vice President of Investor Relations. Please go ahead, ma'am.

ALICIA RODRIGUEZ, VP - IR, AGILENT TECHNOLOGIES INC: Thank you, Keith, and
welcome, everyone, to Agilent's first quarter conference call for fiscal-year
2012. With me are Agilent's President and CEO, Bill Sullivan, as well as Senior
Vice President and CFO, Didier Hirsch. Joining in the Q&A after Didier's
comments will be Agilent's Chief Operating Officer, Ron Nersesian, and the
Presidents of our Electronic Measurement, Life Sciences, and Chemical Analysis
Groups -- Guy Sene, Nick Roelofs, and Mike McMullen.

You can find the press release and information to supplement today's discussion
on our website at www.investor.agilent.com. While there, please click on the
link for financial results, where you will find revenue breakouts and historical
financials for Agilent's operations. We will also post a copy of the prepared
remarks following this call. For any non-GAAP financial measures, you will find
the most directly comparable GAAP financial metrics and reconciliations on our
website.

We will make forward-looking statements about the financial performance of the
Company. These statements are subject to risks and uncertainties, and are only
valid as of today. The Company assumes no obligation to update them. Please look
at the Company's recent SEC filings for a more complete picture of our risks and
other factors.

Before turning the call over to Bill, I would like to remind you that Agilent
will host its annual analysts meeting in New York City on March 8. Details about
the meeting and webcast will be available on the Agilent investor relations
website two weeks prior.

And now, I'd like to turn the call over to Bill.

BILL SULLIVAN, PRESIDENT AND CEO, AGILENT TECHNOLOGIES INC: Thanks, Alicia, and
hello, everyone. Agilent's Q1 orders of $1.62 billion were flat versus last
year. Q1 revenues of $1.64 billion were up 7% year-over-year. Non-GAAP EPS was
$0.69 per share, and operating margin was 19%."""

这是我的代码:

import re

# First approach:
r = re.compile(r"^([^a-z:]+?):([\s\S]+?)", flags=re.MULTILINE)
re.findall(r, example)

# Second approach:
r = re.compile(r"^([^a-z:]+?):([\s\S]+)", flags=re.MULTILINE)
re.findall(r, example)

第一种(非贪婪)方法的问题是它不能捕获说话者的全文。

第二种(贪婪)方法的问题是下一个说话者出现时它不会停止。

编辑:附加信息

  • 文本组也可以包含双点。在某些情况下,在一行的第一个单词之后立即出现双点,例如“例如:...”
  • 扬声器组还可以覆盖多条线路,例如当公司名称和职位描述很长时

【问题讨论】:

  • 直到你明确text 应该在哪里停止(通过定义规则),这是不可能的。您不希望我们为您猜测。
  • 试试r = re.compile(r"^([^a-z\n:]+):(.*?)(?=\n[^a-z\n:]+:|\Z)", re.MULTILINE | re.DOTALL),见this demo
  • 如果你打算使用上面推荐的正则表达式,你最好不要尝试这个^\s*([^:]+):(.*(?:[\r\n]+[^:\r\n]+$)*)
  • @usr2564301 是的,这是可能的。我将用更多信息来扩展我的问题。
  • @revo 为什么你的方法更好?在性能方面?确实对我的分析至关重要,因为数据集非常大。

标签: python regex findall


【解决方案1】:

您可以在不使用 [\s\S]+ 的情况下进行匹配,因为这将匹配任何字符,包括换行符。

对于第二个捕获组,您可以匹配 .*,然后使用带有负前瞻的重复组,只要以下行不以 (?:(?!\n[^a-z\r\n]+:) 开头,它将匹配

^([^a-z\r\n]+):(.*(?:(?!\n[^a-z\r\n]+:)[\r\n].*)*)

Regex demo | Python demo

【讨论】:

  • 也有扬声器组覆盖两行的情况,例如:当职位描述或公司名称很长时。您是否有任何想法将此类情况合并到您的正则表达式中?
  • 这很好,但仍有一种情况会导致您的方法失败。查看第 51 场比赛的演讲者组以获取完整文档regex101.com/r/lO6Qvb/1。你觉得有申请的可能吗?也许在每个扬声器组之前都有\n\n
  • 不匹配 ' 在开头修复它吗? regex101.com/r/ZrIQJK/1
  • 不幸的是,它不适用于导致相同问题的类似星座。但修复更容易。我刚刚在您的正则表达式的前瞻中添加了另一个\n。现在它正在工作:-) 请参阅regex101.com/r/lO6Qvb/2
  • 我不确定 OP 是否真的在寻找这个正则表达式,但我从他们提供的链接中看到的 LANGUAGE: ENGLISH 最后错过了。
猜你喜欢
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多