사용자 지정 Sentencizer 오류가있는 SpaCy 모델을 디스크에 저장

Nov 27 2020

비슷한 질문이 있다는 것을 알고 있습니다.

Spacy Custom 문장 분할

SpaCy에서 사용자 지정 문장 경계 감지

하지만 제 상황은 조금 다릅니다. 나는 spacy Sentencizer ()에서 다음과 같이 상속하고 싶습니다.

from spacy.pipeline import Sentencizer

class MySentencizer(Sentencizer):
    def __init__(self):
        self.tok = create_mySentencizer() # returning the sentences

    def __call__(self, *args, **kwargs):
        doc = args[0]
        for tok in doc:
            # do set the boundaries with tok.is_sent_start 
        return doc

doc = nlp("Text and so on. Another sentence.")모델을 업데이트 한 후 호출하면 분할조차도 잘 작동합니다 .

  nlp = spacy.load("some_model")
  sentencizer = MySentencizer()
  nlp.add_pipe(sentencizer, before="parser")
  # update model 

훈련 된 모델을 저장하고 싶을 때 :

nlp.to_disk("path/to/my/model")

다음과 같은 오류가 발생합니다.

AttributeError: 'MySentencizer' object has no attribute 'punct_chars'

반대로 nlp.add_pipe (nlp.create_pipe ( 'sentencizer'))를 사용하면 오류가 발생하지 않습니다. punct_chars 속성을 설정해야하는 시점이 궁금합니다. 수퍼 클래스에서 상속 되었어야하나요?

클래스에서 Sentencizer를 교체하고 첫 번째 게시물에 따라 객체를 수행하면 작동하지만 punct_chars와 같은 중요한 정보가 손실 될 수 있습니다.

미리 도움을 주셔서 감사합니다.

크리스

답변

1 SergeyBushmanov Nov 29 2020 at 20:44

다음을 수행해야합니다 (참고 super(MySentencizer, self).__init__()).

import spacy
from spacy.pipeline import Sentencizer

class MySentencizer(Sentencizer):
    def __init__(self):
        super(MySentencizer, self).__init__() 

    def __call__(self, *args, **kwargs):
        doc = args[0]
        for tok in doc:
            tok.is_sent_start = True if tok.orth == "." else False
        return doc

nlp = spacy.load("en_core_web_md")
sentencizer = MySentencizer()
nlp.add_pipe(sentencizer, before="parser")

nlp.to_disk("model")