用其他词拆分实体的命名实体识别

问题描述 投票:0回答:1

我有一份法律文件,我想在其中自动识别对另一份法律文件的引用。

文档的结构类似于这个虚拟示例:

非常重要的第 12/34/56 号条约第 85 条第 6 款 明确指出您应该研究另一个重要的第 56/78 号条约,特别是当涉及到第 1 条字母 a第 2 条.

我的目标是识别对其他文档的引用。起初我从正则表达式开始,效果相对较好。但是,如上例所示,我有时需要根据上下文来识别信息。因此,我目前正在考虑使用 spacy 通过(嵌套的)命名实体识别来解决这个问题。

现在问题来了:如前所述,我想识别指向同一文档的其他段落或其他文档的段落的链接。在上面的例子中,只有外部文档的链接,即这3个:

  1. 非常重要的条约第12/34/56号第85条第6款
  2. 第一条第56/78号的另一项重要条约
  3. 第 56/78 号第 2 条另一项重要条约

通常在使用 spacy 为 NER 标记数据时,您使用开始索引和结束索引以及您要识别的相应实体类型来标记实体。然而,在这个例子中,我想要提取的链接实际上会被分成多个部分,所以它实际上需要像

start:ignore_start+_ignore_end:end
.

这样的东西

我想过将实体标记为

article
paragraph
letter
document_title
。但是,那样我就无法将信息重新组合在一起。

我偶然发现了嵌套命名实体识别,但我不确定它是否真的对我有帮助。您认为必须如何标记数据才能解决这个问题?

python regex nested spacy named-entity-recognition
1个回答
0
投票

不清楚您到底需要什么作为结果:最终文本或字典/变量集(以及,您是否需要

Letter
在输出中)。虽然,请考虑一下:

import re

def documents_identification(input_str):

    regex = r"(Article\s+(?P<article>\d+)(\s*|,|$))?(Letter\s+(?P<letter>[a-z]+)(\s*|,|$))?(Paragraph\s+(?P<paragraph>\d+)(\s*|,|$))?(?P<document_title>(?<=\s)((Another|of|Very|Important|Treaty)\s+)+No\.\s+[\d/]+(?:\s*|,|$))?"
    regex_flags = re.I + re.M + re.S

    # input_str += ' ' # sometimes simplier than "(?:\s*|,|$)" etc. at the end

    article = letter = paragraph = document_title = result = last = ''
    article_paragraph_used = False

    for m in re.finditer(regex, input_str, regex_flags):
        if (m.group('article')):
            article = "Article " + m.group('article') + ' '
            letter = ''
        if (m.group('letter')):
            letter = "Letter " + m.group('letter') + ' '
        if (m.group('paragraph')):
            paragraph = "Paragraph " + m.group('paragraph') + ' '
        if (m.group('document_title')):
            if (document_title and article_paragraph_used):
                article = letter = paragraph = ''
            document_title = m.group('document_title')
        
        new = article + letter + paragraph + document_title
        if (new!=last and document_title and article):
            article_paragraph_used = True
            last = new
            result += new + '\n'
    
    return result

str = 'Article 85 Paragraph 6 of Very Important Treaty No. 12/34/56 explicitly states you should do your research on Another Important Treaty No. 56/78, especially when it comes to Article 1 Letter a and Article 2'

str = documents_identification(str)

print(str)

结果:

Article 85 Paragraph 6 of Very Important Treaty No. 12/34/56
Article 1 Letter a Another Important Treaty No. 56/78
Article 2 Another Important Treaty No. 56/78
© www.soinside.com 2019 - 2024. All rights reserved.