在 Spacy 中使用 NER 恢复解析器:训练数据具有重叠的实体开始和结束索引。有解决办法吗?

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

示例:

格式:{内容,注释:{标签:“”点[起始索引,最后索引,文本:“”]}}

假设我们有:内容中的文本是:“Pune University”,我想在其中提取“Pune”作为位置,我也想提取“Pune University”作为学院名称。

请建议我解决此问题。

我使用了来自 Github 的训练数据和代码:https://github.com/DataTurks-Engg/Entity-Recognition-In-Resumes-SpaCyhttps://github.com/singhsourabh/Resume-NER。两者都使用相同的数据。

我没想到会出现错误,因为该数据被很多人使用。请告诉我我该怎么办?

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

如果您使用的是 spacy V2,那么您必须修剪跨度,因为 spacy v2 不支持跨度。为此,您可以使用:

def trim_entity_spans(data: list) -> list:
    """Removes leading and trailing white spaces from entity spans.

    Args:
        data (list): The data to be cleaned in spaCy JSON format.

    Returns:
        list: The cleaned data.
    """
    invalid_span_tokens = re.compile(r"\s")

    cleaned_data = []
    for text, annotations in data:
        entities = annotations["entities"]
        # for i in entities:
        #     if "Skills" in i:
        #         entities.remove(i)
        valid_entities = []
        for start, end, label in entities:
            valid_start = start
            valid_end = end
            while valid_start < len(text) and invalid_span_tokens.match(
                text[valid_start]
            ):
                valid_start += 1
            while valid_end > 1 and invalid_span_tokens.match(text[valid_end - 1]):
                valid_end -= 1
            valid_entities.append([valid_start, valid_end, label])
        cleaned_data.append([text, {"entities": valid_entities}])
    return cleaned_data

您可以关注这个笔记本了解更多。 另外,上面的代码仅适用于 spacy==2.1.4 。因此,如果您使用其他版本,如果您没有使用任何版本特定功能或可以重构此代码,请考虑降级。

© www.soinside.com 2019 - 2024. All rights reserved.