def test_findTypeDescFeatureByName(self): castypesytem = TypeSystemParser() castypesytem.setTypeAsFile('tests/testing_data/typesystem.xml') fd = castypesytem.findTypeDescFeatureByName( 'language', 'de.tudarmstadt.ukp.dkpro.core.api.metadata.type.DocumentMetaData') self.assertEqual( castypesytem.getElementText(castypesytem.getFeatureDescName(fd)), 'language') with self.assertRaises(AttributeError): castypesytem.getElementText(None)
def test_getTypeDescriptionFeaturesAsString(self): castypesytem = TypeSystemParser() castypesytem.setTypeAsFile('tests/testing_data/typesystem.xml') "print(castypesytem.getTypeDescriptionFeaturesAsString(castypesytem.getTypeDescElements()[1]))" self.assertEqual( castypesytem.getElementTag( castypesytem.getTypeDescriptionFeatures( castypesytem.getTypeDescElements()[1])), '{http://uima.apache.org/resourceSpecifier}features') self.assertEqual( castypesytem.getElementText( castypesytem.getTypeDescriptionFeatures( castypesytem.getTypeDescElements()[1])), '\n ') self.assertEqual( castypesytem.getElementNamespace( castypesytem.getTypeDescriptionFeatures( castypesytem.getTypeDescElements()[1])), 'http://uima.apache.org/resourceSpecifier') self.assertEqual( castypesytem.getElementLocalname( castypesytem.getTypeDescriptionFeatures( castypesytem.getTypeDescElements()[1])), 'features') """ castypesytem1 = TypeSystemParser() atypeDesc= castypesytem.getTypeDescElements()[1] features = castypesytem1.getTypeDescriptionFeatures(atypeDesc) castypesytem2 = TypeSystemParser() """ "print(castypesytem2.getTypeDescriptionFeaturesAsString(atypeDesc))"
def test_getTypeDescriptionName(self): castypesytem = TypeSystemParser() castypesytem.setTypeAsFile('tests/testing_data/typesystem.xml') "print(castypesytem.getTypeDescriptionName(castypesytem.getTypeDescElements()[0]))" self.assertEqual( castypesytem.getElementText( castypesytem.getTypeDescriptionName( castypesytem.getTypeDescElements()[0])), 'uima.tcas.DocumentAnnotation')
def test_findTypeDescElementByName(self): castypesytem = TypeSystemParser() castypesytem.setTypeAsFile('tests/testing_data/typesystem.xml') docmetadata = castypesytem.findTypeDescElementByName( 'de.tudarmstadt.ukp.dkpro.core.api.metadata.type.DocumentMetaData') self.assertEqual( castypesytem.getElementText( castypesytem.getTypeDescriptionName(docmetadata)), 'de.tudarmstadt.ukp.dkpro.core.api.metadata.type.DocumentMetaData')
def test_getTypeDescriptionSuperType(self): castypesytem = TypeSystemParser() castypesytem.setTypeAsFile('tests/testing_data/typesystem.xml') self.assertEqual( castypesytem.getElementTag( castypesytem.getTypeDescriptionSuperType( castypesytem.getTypeDescElements()[0])), '{http://uima.apache.org/resourceSpecifier}supertypeName') self.assertEqual( castypesytem.getElementText( castypesytem.getTypeDescriptionSuperType( castypesytem.getTypeDescElements()[0])), 'uima.tcas.Annotation')
def test_getTypeDesc(self): castypesytem = TypeSystemParser() castypesytem.setTypeAsFile('tests/testing_data/typesystem.xml') "print(castypesytem.getTypeDescElements())" "print(castypesytem.getTypeDescElements()[0])" self.assertEqual( castypesytem.getTypeDescElementsAsStingList()[0], '<typeDescription xmlns="http://uima.apache.org/resourceSpecifier"><name>uima.tcas.DocumentAnnotation</name><description/><supertypeName>uima.tcas.Annotation</supertypeName><features><featureDescription><name>language</name><description/><rangeTypeName>uima.cas.String</rangeTypeName></featureDescription></features></typeDescription>' ) self.assertEqual( castypesytem.getElementTag(castypesytem.getTypeDescElements()[0]), '{http://uima.apache.org/resourceSpecifier}typeDescription') self.assertEqual( castypesytem.getElementNamespace( castypesytem.getTypeDescElements()[0]), 'http://uima.apache.org/resourceSpecifier') self.assertEqual( castypesytem.getElementLocalname( castypesytem.getTypeDescElements()[0]), 'typeDescription') self.assertEqual( castypesytem.getElementText(castypesytem.getTypeDescElements()[0]), '\n ')
def readTypeSystem(self, typesystempath): #create TypeSystem object typesystem = TypeSystem() #create TypeSystemParser object typesytemParser = TypeSystemParser() # set the type system path to TypeSystemParser typesytemParser.setTypeAsFile(typesystempath) # populate TypeSystem name space attribute to contain the base name space typesystem.namespace = typesytemParser.getTypeSystemDescNamespace() # iterate type description elements returned by the parser and create a typeDescription object for TypeSystem for tdelement in typesytemParser.getTypeDescElements(): # create type description object TDname = typesytemParser.getElementText( typesytemParser.getTypeDescriptionName(tdelement)) TDdesc = typesytemParser.getElementText( typesytemParser.getTypeDescriptionDescription(tdelement)) typeDesc = TypeDescription(TDname, TDdesc) typeDesc.superType = typesytemParser.getElementText( typesytemParser.getTypeDescriptionSuperType(tdelement)) #get the features element from parser features = typesytemParser.getTypeDescriptionFeatures(tdelement) if not features == None: #get the feature description element from the parser and create features object for typeDescription for felement in typesytemParser.getFeatureDescElements( features): fdomain = TDname fname = typesytemParser.getElementText( typesytemParser.getFeatureDescName(felement)) feature = Feature(fdomain, fname) feature.description = typesytemParser.getElementText( typesytemParser.getFeatureDescDescription(felement)) rangeVal = typesytemParser.getFeatureDescRangeType( felement) elementVal = typesytemParser.getFeatureDescElementType( felement) if (not rangeVal == None): rangeValStr = typesytemParser.getElementText(rangeVal) if (rangeValStr == CAS_Type.TYPE_NAME_FSARRAY and elementVal == None): raise TypeError( fname, 'has only rangeType as FSArray, no element type found' ) return #if range type is primitive CAS type if (rangeValStr == CAS_Type.TYPE_NAME_BOOLEAN or rangeValStr == CAS_Type.TYPE_NAME_FLOAT or rangeValStr == CAS_Type.TYPE_NAME_DOUBLE or rangeValStr == CAS_Type.TYPE_NAME_INTEGER or rangeValStr == CAS_Type.TYPE_NAME_LONG or rangeValStr == CAS_Type.TYPE_NAME_STRING): #set the range type from type system of the feature feature.rangeType = rangeValStr else: feature.rangeType = TypeDescription(rangeValStr) if (not elementVal == None): elementValStr = typesytemParser.getElementText( elementVal) #if element type is primitive CAS type if (elementValStr == CAS_Type.TYPE_NAME_BOOLEAN or elementValStr == CAS_Type.TYPE_NAME_FLOAT or elementValStr == CAS_Type.TYPE_NAME_DOUBLE or elementValStr == CAS_Type.TYPE_NAME_INTEGER or elementValStr == CAS_Type.TYPE_NAME_LONG or elementValStr == CAS_Type.TYPE_NAME_STRING): #set the range type from type system of the feature feature.elementType = elementValStr else: feature.elementType = TypeDescription( elementValStr) #add the feature to the TypeDescription object typeDesc.addFeature(feature) #add the TypeDescription to the TypeSystem object typesystem.addTypeDesc(typeDesc) #add inbuilt uima.cas.AnnotationBase type with sofa feature typeDescABase = TypeDescription('uima.cas.AnnotationBase', '') typeDescABase.superType = 'uima.cas.TOP' featureSofa = Feature('uima.cas.AnnotationBase', 'sofa') featureSofa.description = '' featureSofa.elementType = TypeDescription('uima.cas.Sofa') typeDescABase.addFeature(featureSofa) typesystem.addTypeDesc(typeDescABase) #add inbuilt uima.tcas.Annotation type with begin and end feature typeDescABase = TypeDescription('uima.tcas.Annotation', '') typeDescABase.superType = 'uima.cas.AnnotationBase' #add begin feature to inbuilt uima.tcas.Annotation featureSofa = Feature('uima.tcas.Annotation', 'begin') featureSofa.description = '' featureSofa.elementType = CAS_Type.TYPE_NAME_INTEGER typeDescABase.addFeature(featureSofa) #add end feature to inbuilt uima.tcas.Annotation featureSofa = Feature('uima.tcas.Annotation', 'end') featureSofa.description = '' featureSofa.elementType = CAS_Type.TYPE_NAME_INTEGER typeDescABase.addFeature(featureSofa) #add inbuilt uima.tcas.Annotation type to the list of type descriptions typesystem.addTypeDesc(typeDescABase) #add inbuilt FSArray type typeDescFsArray = TypeDescription('uima.cas.FSArray', '') typeDescFsArray.superType = 'uima.cas.TOP' typesystem.addTypeDesc(typeDescFsArray) #add inbuilt sofa type typeDescSofa = TypeDescription('uima.cas.Sofa', '') typeDescSofa.superType = 'uima.cas.TOP' #add sofaNum feature to inbuilt sofa type featureSofaNum = Feature('uima.cas.Sofa', 'sofaNum') featureSofaNum.description = '' featureSofaNum.elementType = CAS_Type.TYPE_NAME_INTEGER typeDescSofa.addFeature(featureSofaNum) #add sofaID feature to inbuilt sofa type featureSofaID = Feature('uima.cas.Sofa', 'sofaID') featureSofaID.description = '' featureSofaID.elementType = CAS_Type.TYPE_NAME_STRING typeDescSofa.addFeature(featureSofaID) #add mimeType feature to inbuilt sofa type featureMimeType = Feature('uima.cas.Sofa', 'mimeType') featureMimeType.description = '' featureMimeType.elementType = CAS_Type.TYPE_NAME_STRING typeDescSofa.addFeature(featureMimeType) #add sofaString feature to inbuilt sofa type featureSofaString = Feature('uima.cas.Sofa', 'sofaString') featureSofaString.description = '' featureSofaString.elementType = CAS_Type.TYPE_NAME_STRING typeDescSofa.addFeature(featureSofaString) #add inbuilt sofa type to the list of type descriptions typesystem.addTypeDesc(typeDescSofa) return typesystem