AddingSpanCategorizertoSpacyattheendofpipelineisnotworking
创始人
2024-07-26 20:01:16
0

在spaCy的管道末尾添加Span Categorizer是没有作用的。这是因为在pipe的最后一步,最后一个组件返回的不是一个Doc或一个Span,而是一个单词标记(Token)。因此,它不可能创建一个覆盖整个文档的Span对象。

有两种方法可以解决这个问题:

  1. 创建一个定制的组件,继承spancat.base.SpanCategorizer并插入到管道的适当位置。这个组件将会保留或创建新的Span对象以进行分类。以下是一个例子:
import spacy
from spacy import displacy
from spacy.pipeline import Pipe
from spacy.lang.en import English

class MySpanCategorizer(Pipe):
    def __init__(self, nlp, labels=[]):
        self.labels = labels
        self.model = None
        self.nlp = nlp
        self.batch_size = 1
  
    def add_label(self, label):
        self.labels.append(label)
  
    def predict(self, docs):
        for doc in docs:
            spans = []
            for start, end in zip(range(len(doc)), range(1, len(doc)+1)):
                span = doc[start:end]
                spans.append(span)
            for span in spans:
                span_cats = {}
                for label in self.labels:
                    # Set score to 0.5 for all labels
                    span_cats[label] = 0.5
                span.set_extension('cats', default={}, force=True)
                span._.cats = span_cats
        return docs


nlp = English()
span_cat = MySpanCategorizer(nlp, ['PERSON', 'ORG'])
nlp.add_pipe(span_cat, last=True)
doc = nlp("George Washington was the first president of the United States.")
displacy.render(doc, style='dep', jupyter=True)
  1. 您可以将Span Categorizer插入到数据的中间,这样您将能够从

相关内容

热门资讯

安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
避免在粘贴双引号时向VS 20... 在粘贴双引号时向VS 2022添加反斜杠的问题通常是由于编辑器的自动转义功能引起的。为了避免这个问题...
Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
omi系统和安卓系统哪个好,揭... OMI系统和安卓系统哪个好?这个问题就像是在问“苹果和橘子哪个更甜”,每个人都有自己的答案。今天,我...
原生ios和安卓系统,原生对比... 亲爱的读者们,你是否曾好奇过,为什么你的iPhone和安卓手机在操作体验上有着天壤之别?今天,就让我...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...
Alertmanager在pr... 首先,在Prometheus配置文件中,确保Alertmanager URL已正确配置。例如:ale...