dkpro / dkpro-cassis

UIMA CAS processing library written in Python
https://pypi.org/project/dkpro-cassis/
Apache License 2.0
84 stars 22 forks source link

while serializing CAS to xmi: AttributeError: 'str' object has no attribute 'elements' #263

Closed priyankanirmale closed 1 year ago

priyankanirmale commented 1 year ago

Below error came while converting CAS added annotation data to xmi format using to_xmi() function. Error as below ,

File "C:\working\inception-external-recommender-master\inception-external-recommender-master\inception\lib\site-packages\cassis\cas.py", line 605, in to_xmi return self._serialize(CasXmiSerializer(), path, pretty_print=pretty_print) File "C:\working\inception-external-recommender-master\inception-external-recommender-master\inception\lib\site-packages\cassis\cas.py", line 649, in _serialize return serializer.serialize(None, self, kwargs) File "C:\working\inception-external-recommender-master\inception-external-recommender-master\inception\lib\site-packages\cassis\xmi.py", line 496, in serialize for fs in sorted(cas._find_all_fs(), key=lambda a: a.xmiID): File "C:\working\inception-external-recommender-master\inception-external-recommender-master\inception\lib\site-packages\cassis\cas.py", line 748, in _find_all_fs if feature.rangeType.name == TYPE_NAME_FS_ARRAY and feature_value.elements: AttributeError: 'str' object has no attribute 'elements'**

Steps to reproduce the behavior:

  1. Add annotation of data , convert annotation data to cassis to xmi format using to_xmi() function.
  2. Error is coming while call to_xmi() function for conversion of added annotation to xmi format.

Expected behavior to_xmi() function should Serialize the cas.add_annotation() data without throwing error.

Error message File "C:\working\inception-external-recommender-master\inception-external-recommender-master\inception\lib\site-packages\cassis\cas.py", line 748, in _find_all_fs if feature.rangeType.name == TYPE_NAME_FS_ARRAY and feature_value.elements: AttributeError: 'str' object has no attribute 'elements'

Please complete the following information:

reckart commented 1 year ago

Please see https://github.com/dkpro/dkpro-cassis#array-support

If that documentation does not solve your problem, can you provide a short script that reproduces the problem please?

priyankanirmale commented 1 year ago

Github code : [https://github.com/inception-project/inception-external-recommender]

Please refer above github code. I'm using this github code for implementation of inception annotation data.
In "ariadne/contrib/server.py" file, inside the Server() class there is function called _predict() which has this line, "result = jsonify(document=req.cas.to_xmi())" The error is coming at this line basically when to_xmi() function call get triggered.

reckart commented 1 year ago

I know that code and so far as I know, it does not use FSArray/multi-valued features.

Please provide a piece of code that does use FSArray/multi-valued features in the way that you do so I can see if you use them correctly.

reckart commented 1 year ago

Let's ask differently:

priyankanirmale commented 1 year ago

Here is code that does use FSArray/multi-valued features internally ,

import requests
from cassis import *
from cassis.typesystem import FeatureStructure

from bottle import Bottle, request, BaseRequest, response

app = Bottle()

IS_PREDICTION = "inception_internal_predicted"

def create_prediction(cas: Cas, layer: str, feature: str, begin: int, end: int, label: str) -> FeatureStructure:
    AnnotationType = cas.typesystem.get_type(layer)

    fields = {"begin": begin, "end": end, IS_PREDICTION: True, feature: label}
    prediction = AnnotationType(**fields)
    return prediction

def parse_prediction_request(json_object: JsonDict) -> PredictionRequest:
    metadata = json_object["metadata"]
    document = json_object["document"]
    layer = metadata["layer"]
    feature = metadata["feature"]
    project_id = metadata["projectId"]

    typesystem = load_typesystem(json_object["typeSystem"])
    cas = load_cas_from_xmi(document["xmi"], typesystem=typesystem)
    document_id = document["documentId"]
    user_id = document["userId"]

@app.post('/annotation/predict')
def get_annotation():
    # here request is post request body which contains xmi document data.
    json_data = request.get_json()

    layer = "webanno.custom.AttributeLayer"
    req = parse_prediction_request(json_data))

    value = "USA"
    key = "Country"
    if value in req.cas.sofa_string:
        begin = req.cas.sofa_string.index(value)
        end = begin + len(value) - 1
        stem = key
        prediction = create_prediction(req.cas, req.layer, req.feature, begin, end, stem)
        req.cas.add(prediction)

    result = {"document":req.cas.to_xmi()}
    return result

def main():
    app.run(host='0.0.0.0', server="paste", port="8081", debug=True)

if __name__ == '__main__':
    main()
priyankanirmale commented 1 year ago

Using dkpro-cassis==0.7.2 version

reckart commented 1 year ago

Looking at this code, it appears to me that no FSArray is added here - so I assume it must be already in the data that you receive from the remote side - in this case apparently from INCEpTION?

We have recently fixed a bug related to empty arrays here: https://github.com/dkpro/dkpro-cassis/issues/258

If you are using cassis 0.7.2, then that bug should not affect you anymore. Maybe you have stumbled over a variant.

It would be great if you could try producing a small test file which triggers the problem. For example, upload a dummy text document into your project in INCEpTION, try annotating it in a similar way as the document that is giving you problems until you can trigger the error - remove all unnecessary annotations so the example is minimal, dump this XMI to a file (maybe best export it directly from INCEpTION) and provide it so we cant try reproducing and fixing this issue. Would that work?

reckart commented 1 year ago

Closing issue as there as no further feedback.