如何从一段或一段段落中找到标题案例短语

时间:2009-08-27 20:04:07

标签: python parsing nlp text-parsing

如何解析段落中的句子短语。

例如,从这段经文

柯南道尔说,福尔摩斯的角色受到约瑟夫贝尔博士的启发,多伊尔曾在爱丁堡皇家医院担任职员。与霍姆斯一样,贝尔因从最小的观察中得出大量结论而受到关注。[1]迈克尔·哈里森(Michael Harrison)在Ellery Queen的“神秘杂志”(Ellery Queen's Mystery Magazine)1971年的一篇文章中指出,这个角色的灵感来自于一起谋杀案中的“咨询侦探”温德尔·谢勒(Wendell Scherer),该案件据称于1882年在英格兰引起了大量的报纸关注。

我们需要生成像Conan Doyle,Holmes,Joseph Bell博士,Wendell Scherr等人的东西。

如果可能,我更喜欢Pythonic解决方案

2 个答案:

答案 0 :(得分:5)

这种处理可能非常棘手。这个简单的代码几乎是正确的:

for s in re.finditer(r"([A-Z][a-z]+[. ]+)+([A-Z][a-z]+)?", text):
    print s.group(0)

产生

Conan Doyle
Holmes
Dr. Joseph Bell
Doyle
Edinburgh Royal Infirmary. Like Holmes
Bell
Michael Harrison
Ellery Queen
Mystery Magazine
Wendell Scherer
England

要包括“约瑟夫贝尔博士”,你需要对字符串中的句号保持正确,这允许在“爱丁堡皇家医院。像霍姆斯一样”。

我遇到了类似的问题:Separating Sentences

答案 1 :(得分:2)

“重新”方法很快耗尽了蒸汽。命名实体识别是一个非常复杂的主题,超出了SO答案的范围。如果你认为你有一个很好的方法来解决这个问题,请指出Flann O'Brien又名Myles na cGopaleen,Sukarno,Harry S. Truman,J。Edgar Hoover,JK Rowling,数学家L'Hopital,Joe di Maggio, Algernon Douglas-Montagu-Scott和Hugo Max Graf von und zu LerchenfeldaufKöferingundSchönberg。

更新以下是一种基于“重新”的方法,可以找到更多有效的案例。不过,我仍然认为这不是一个好方法。注:我在我的文本样本中将巴伐利亚计数的名称贬低了。如果有人真的想要使用这样的东西,它们应该在Unicode中工作,并在某个阶段(输入或输出)规范化空白。

import re

text1 = """Conan Doyle said that the character of Holmes was inspired by Dr. Joseph Bell, for whom Doyle had worked as a clerk at the Edinburgh Royal Infirmary. Like Holmes, Bell was noted for drawing large conclusions from the smallest observations.[1] Michael Harrison argued in a 1971 article in Ellery Queen's Mystery Magazine that the character was inspired by Wendell Scherer, a "consulting detective" in a murder case that allegedly received a great deal of newspaper attention in England in 1882."""

text2 = """Flann O'Brien a.k.a. Myles na cGopaleen, I Zingari, Sukarno and Suharto, Harry S. Truman, J. Edgar Hoover, J. K. Rowling, the mathematician L'Hopital, Joe di Maggio, Algernon Douglas-Montagu-Scott, and Hugo Max Graf von und zu Lerchenfeld auf Koefering und Schoenberg."""

pattern1 = r"(?:[A-Z][a-z]+[. ]+)+(?:[A-Z][a-z]+)?"

joiners = r"' - de la du von und zu auf van der na di il el bin binte abu etcetera".split()

pattern2 = r"""(?x)
    (?:
        (?:[ .]|\b%s\b)*
        (?:\b[a-z]*[A-Z][a-z]*\b)?
    )+
    """ % r'\b|\b'.join(joiners)

def get_names(pattern, text):
    for m in re.finditer(pattern, text):
        s = m.group(0).strip(" .'-")
        if s:
            yield s

for t in (text1, text2):
    print "*** text: ", t[:20], "..."
    print "=== Ned B"
    for s in re.finditer(pattern1):
        print repr(s.group(0))
    print "=== John M =="
    for name in get_names(pattern2, t):
        print repr(name)

输出:

C:\junk\so>\python26\python extract_names.py
*** text:  Conan Doyle said tha ...
=== Ned B
'Conan Doyle '
'Holmes '
'Dr. Joseph Bell'
'Doyle '
'Edinburgh Royal Infirmary. Like Holmes'
'Bell '
'Michael Harrison '
'Ellery Queen'
'Mystery Magazine '
'Wendell Scherer'
'England '
=== John M ==
'Conan Doyle'
'Holmes'
'Dr. Joseph Bell'
'Doyle'
'Edinburgh Royal Infirmary. Like Holmes'
'Bell'
'Michael Harrison'
'Ellery Queen'
'Mystery Magazine'
'Wendell Scherer'
'England'
*** text:  Flann O'Brien a.k.a. ...
=== Ned B
'Flann '
'Brien '
'Myles '
'Sukarno '
'Harry '
'Edgar Hoover'
'Joe '
'Algernon Douglas'
'Hugo Max Graf '
'Lerchenfeld '
'Koefering '
'Schoenberg.'
=== John M ==
"Flann O'Brien"
'Myles na cGopaleen'
'I Zingari'
'Sukarno'
'Suharto'
'Harry S. Truman'
'J. Edgar Hoover'
'J. K. Rowling'
"L'Hopital"
'Joe di Maggio'
'Algernon Douglas-Montagu-Scott'
'Hugo Max Graf von und zu Lerchenfeld auf Koefering und Schoenberg'