inception-project / external-recommender-spacy

External recommender example for the INCEpTION annotation platform using spacy
Apache License 2.0
22 stars 3 forks source link

Any example of sentence based annotation ? #7

Open thak123 opened 4 years ago

thak123 commented 4 years ago

I am trying to capture quotes using spacy and textcy. The quotes are getting extracte But I am not able to put it back on the UI

def predict_quote(prediction_request: PredictionRequest) -> PredictionResponse:
    # Load the CAS and type system from the request
    typesystem = load_typesystem(prediction_request.typeSystem)
    cas = load_cas_from_xmi(prediction_request.document.xmi, typesystem=typesystem)
    AnnotationType = typesystem.get_type(prediction_request.layer)

    # Extract the tokens from the CAS and create a spacy doc from it
    sentences = list(cas.select(SENTENCE_TYPE))
    print("sentences:",sentences)

    words = [cas.get_covered_text(token) for token in sentences]
    print(words)

    doc =nlp("".join(words))
    print(doc.sents)

 # Find the quote entities
    quote_entities = direct_quotations(doc)
    print(list(quote_entities))

    # For every entity returned by spacy, create an annotation in the CAS
    for ent in (quote_entities):
        for i in ent:
            fields = {'begin': tokens[i.start].begin,
                      'end': tokens[i.end - 1].end,
                      IS_PREDICTION: True,
                      prediction_request.feature: "speaker"}
            annotation = AnnotationType(**fields)
            cas.add_annotation(annotation)
            break

    xmi = cas.to_xmi()
    return PredictionResponse(xmi)
jcklie commented 4 years ago

This repository is deprecated, the new version is in https://github.com/inception-project/inception-external-recommender . There is also an example for sentence classification.