From 15129c71af0a5aea630ef858b8a5830135741fc4 Mon Sep 17 00:00:00 2001
From: Tomasz Bartosiak <tomasz.bartosiak@gmail.com>
Date: Thu, 16 Nov 2023 16:12:08 +0100
Subject: [PATCH] Added structured representation of syntax

---
 common/management/commands/valunifier_tei.py | 623 ++++++++++++++++++-
 1 file changed, 601 insertions(+), 22 deletions(-)

diff --git a/common/management/commands/valunifier_tei.py b/common/management/commands/valunifier_tei.py
index bca4ac1..4c807cc 100644
--- a/common/management/commands/valunifier_tei.py
+++ b/common/management/commands/valunifier_tei.py
@@ -450,7 +450,7 @@ def schema_textual_representation(subentry, schema):
     if subentry.inherent_sie.name == 'true':
         result += ' się:'
     else:
-        rerult += ':'
+        result += ':'
     
     opinion = schema.opinion.key
     if opinion == 'vul':
@@ -476,7 +476,7 @@ def schema_textual_representation(subentry, schema):
     if subentry.predicativity.name == 'true':
         result += ' pred:'
     else:
-        rerult += ':'
+        result += ':'
 
     if subentry.aspect is not None:
         result += ' ' + subentry.aspect.name + ':'
@@ -498,15 +498,6 @@ def write_schema(parent, subentry, subentry_id, schema, schema_id):
     schema_fs.attrib[etree.QName(XML_NAMESPACE, 'id')] = schema_xml_id
     schema_fs.attrib['type'] = 'schema'
 
-    # textual representation @TODO -- not present in the database
-    # text_rep_f_elem = etree.SubElement(schema_fs_elem, 'f')
-    # text_rep_f_elem.attrib['name'] = 'text_rep'
-    # text_rep_string = etree.SubElement(text_rep_f_elem, 'string')
-    # text_rep = schema.get_position_spaced_text_rep()
-    # if schema.characteristics.filter(type=u'ZWROTNOŚĆ', value__value=u'się').exists():
-    #     text_rep = ' ' + text_rep
-    # text_rep_string.text = lemma.entry_obj.name + text_rep.replace(':',': ')
-
     # schema opinion
     schema_opinion = schema.opinion.key
     if schema.opinion.key is None:
@@ -547,20 +538,22 @@ def write_schema(parent, subentry, subentry_id, schema, schema_id):
     # positions
     write_positions(schema_fs, subentry_id, schema, schema_id)
 
-def write_positions(parent, subentry_id, schema, schema_id):
+def write_positions(parent, subentry_id, schema, schema_id, fake_id=False):
     positions = schema.positions.all()
     positions_f = etree.SubElement(parent, 'f')
     positions_f.attrib['name'] = 'positions'
     vColl = etree.SubElement(positions_f, 'vColl')
     vColl.attrib['org'] = 'set'
     for position in positions:
-        write_position(vColl, subentry_id, schema_id, position)
+        write_position(vColl, subentry_id, schema_id, position, fake_id)
         
-def write_position(parent, subentry_id, schema_id, position):
-    position_xml_id = u'unif_%d.%d.%d-psn' %(subentry_id, schema_id, position.id)
+def write_position(parent, subentry_id, schema_id, position, fake_id):
+    if not fake_id:
+        position_xml_id = u'unif_%d.%d.%d-psn' %(subentry_id, schema_id, position.id)
     position_fs = etree.SubElement(parent, 'fs')
     position_fs.attrib['type'] = 'position'
-    position_fs.attrib[etree.QName(XML_NAMESPACE, 'id')] = position_xml_id
+    if not fake_id:
+        position_fs.attrib[etree.QName(XML_NAMESPACE, 'id')] = position_xml_id
 
     text_rep_f = etree.SubElement(position_fs, 'f')
     text_rep_f.attrib['name'] = 'textual_representation'
@@ -571,7 +564,7 @@ def write_position(parent, subentry_id, schema_id, position):
     
     write_control(position_fs, position)
     
-    write_phrases(position_fs, subentry_id, schema_id, position)
+    write_phrases(position_fs, subentry_id, schema_id, position, fake_id)
 
 def write_function(parent, position):
     function = position.function
@@ -598,24 +591,28 @@ def write_control(parent, position):
             pred_control_symbol = etree.SubElement(vColl, 'symbol')
             pred_control_symbol.attrib['value'] = control
 
-def write_phrases(parent, subentry_id, schema_id, position):
+def write_phrases(parent, subentry_id, schema_id, position, fake_id):
     phrases = position.phrase_types.all()
     phrases_f = etree.SubElement(parent, 'f')
     phrases_f.attrib['name'] = 'phrases'
     vColl = etree.SubElement(phrases_f, 'vColl')
     vColl.attrib['org'] = 'set'
     for phrase in phrases:
-        write_phrase(vColl, subentry_id, schema_id, position, phrase)
+        write_phrase(vColl, subentry_id, schema_id, position, phrase, fake_id)
     
-def write_phrase(parent, subentry_id, schema_id, position, phrase):
-    phrase_xml_id = u'unif_%d.%d.%d.%d-phr' %(subentry_id, schema_id, position.id, phrase.id)
+def write_phrase(parent, subentry_id, schema_id, position, phrase, fake_id):
+    if not fake_id:
+        phrase_xml_id = u'unif_%d.%d.%d.%d-phr' %(subentry_id, schema_id, position.id, phrase.id)
     phrase_fs = etree.SubElement(parent, 'fs')
-    phrase_fs.attrib[etree.QName(XML_NAMESPACE, 'id')] = phrase_xml_id
+    if not fake_id:
+        phrase_fs.attrib[etree.QName(XML_NAMESPACE, 'id')] = phrase_xml_id
     phrase_fs.attrib['type'] = phrase.main_type.name
     # @TODO -- currently no expansions file
     # if phrase.realizations.exists() and write_expansions_id:
     #     write_expansions_link(phrase_fs_elem, phrase)
     write_phrase_textrep(phrase_fs, phrase)
+
+    write_phrase_structure(phrase_fs, phrase)
             
 def write_phrase_textrep(parent, phrase):
     text_rep = phrase.text_rep
@@ -623,4 +620,586 @@ def write_phrase_textrep(parent, phrase):
     textrep_f.attrib['name'] = 'textual_representation'
     textrep_string = etree.SubElement(textrep_f, 'string')
     textrep_string.text = text_rep
+
+def write_phrase_structure(parent, phrase):
+    phrase_type = phrase.main_type
+    phrase_type_fs = etree.SubElement(parent, 'fs')
+    phrase_type_fs.attrib['type'] = phrase_type.name
+    attributes = phrase.attributes
+    
+    if phrase_type.name == 'np':
+        write_np_attributes(phrase_type_fs, attributes.npattributes)
+    elif phrase_type.name == 'adjp':
+        write_adjp_attributes(phrase_type_fs, attributes.adjpattributes)
+    elif phrase_type.name == 'possp':
+        write_possp_attributes(phrase_type_fs, attributes.emptyattributes)
+    elif phrase_type.name == 'compar':
+        write_compar_attributes(phrase_type_fs, attributes.comparattributes)
+    elif phrase_type.name == 'prepnp':
+        write_prepnp_attributes(phrase_type_fs, attributes.prepnpattributes)
+    elif phrase_type.name == 'prepadjp':
+        write_prepadjp_attributes(phrase_type_fs, attributes.prepadjpattributes)
+    elif phrase_type.name == 'comprepnp':
+        write_comprepnp_attributes(phrase_type_fs, attributes.comprepnpattributes)
+    elif phrase_type.name == 'cp':
+        write_cp_attributes(phrase_type_fs, attributes.cpattributes)
+    elif phrase_type.name == 'ncp':
+        write_ncp_attributes(phrase_type_fs, attributes.ncpattributes)
+    elif phrase_type.name == 'prepncp':
+        write_prepncp_attributes(phrase_type_fs, attributes.prepncpattributes)
+    elif phrase_type.name == 'nonch':
+        write_nonch_attributes(phrase_type_fs, attributes.emptyattributes)
+    elif phrase_type.name == 'infp':
+        write_infp_attributes(phrase_type_fs, attributes.infpattributes)
+    elif phrase_type.name == 'advp':
+        try: # due to database error
+            a = attributes.advpattributes
+        except:
+            a = attributes.xpattributes
+        write_advp_attributes(phrase_type_fs, a)
+    elif phrase_type.name == 'xp':
+        write_xp_attributes(phrase_type_fs, attributes.xpattributes)
+    elif phrase_type.name == 'E':
+        write_e_attributes(phrase_type_fs, attributes.emptyattributes)
+    elif phrase_type.name == 'or':
+        write_or_attributes(phrase_type_fs, attributes.emptyattributes)
+    elif phrase_type.name == 'refl':
+        write_refl_attributes(phrase_type_fs, attributes.emptyattributes)
+    elif phrase_type.name == 'recip':
+        write_recip_attributes(phrase_type_fs, attributes.emptyattributes)
+    elif phrase_type.name == 'distrp':
+        write_distrp_attributes(phrase_type_fs, attributes.emptyattributes)
+    elif phrase_type.name == 'nump':
+        write_nump_attributes(phrase_type_fs, attributes.numpattributes)
+    elif phrase_type.name == 'prepnump':
+        write_prepnump_attributes(phrase_type_fs, attributes.prepnumpattributes)
+    elif phrase_type.name == 'pactp':
+        write_pactp_attributes(phrase_type_fs, attributes.pactpattributes)
+    elif phrase_type.name == 'preppactp':
+        write_preppactp_attributes(phrase_type_fs, attributes.preppactpattributes)
+    elif phrase_type.name == 'ppasp':
+        write_ppasp_attributes(phrase_type_fs, attributes.ppaspattributes)
+    elif phrase_type.name == 'prepppasp':
+        write_prepppasp_attributes(phrase_type_fs, attributes.prepppaspattributes)
+    elif phrase_type.name == 'gerp':
+        write_gerp_attributes(phrase_type_fs, attributes.gerpattributes)
+    elif phrase_type.name == 'prepgerp':
+        write_prepgerp_attributes(phrase_type_fs, attributes.prepgerpattributes)
+    elif phrase_type.name == 'qub':
+        write_qub_attributes(phrase_type_fs, attributes.emptyattributes)
+    elif phrase_type.name == 'lex':
+        write_lex(phrase_type_fs, phrase)
+    elif phrase_type.name == 'fixed':
+        write_fixed_attributes(phrase_type_fs, attributes.fixedattributes)
+    else:
+        print(phrase_type.name)
+        raise PhraseTypeError()
+
+def write_np_attributes(parent, attributes):
+    case_f = etree.SubElement(parent, 'f')
+    case_f.attrib['name'] = 'case'
+    case_symbol = etree.SubElement(case_f,'symbol')
+    case_symbol.attrib['value'] = attributes.case.name
+
+def write_adjp_attributes(parent, attributes):
+    case_f = etree.SubElement(parent, 'f')
+    case_f.attrib['name'] = 'case'
+    case_symbol = etree.SubElement(case_f,'symbol')
+    case_symbol.attrib['value'] = attributes.case.name
+
+def write_possp_attributes(parent, attributes):
+    # no expansions file
+    pass
+
+def write_compar_attributes(parent, attributes):
+    cat_f = etree.SubElement(parent, 'f')
+    cat_f.attrib['name'] = 'compar_category'
+    cat_symbol = etree.SubElement(cat_f,'symbol')
+    cat_symbol.attrib['value'] = attributes.comparcat.name
+
+def write_prepnp_attributes(parent, attributes):
+    prep_f = etree.SubElement(parent, 'f')
+    prep_f.attrib['name'] = 'preposition'
+    prep_symbol = etree.SubElement(prep_f,'symbol')
+    prep_symbol.attrib['value'] = attributes.prep.name
+    case_f = etree.SubElement(parent, 'f')
+    case_f.attrib['name'] = 'case'
+    case_symbol = etree.SubElement(case_f,'symbol')
+    case_symbol.attrib['value'] = attributes.case.name
+
+def write_prepadjp_attributes(parent, attributes):
+    prep_f = etree.SubElement(parent, 'f')
+    prep_f.attrib['name'] = 'preposition'
+    prep_symbol = etree.SubElement(prep_f,'symbol')
+    prep_symbol.attrib['value'] = attributes.prep.name
+    case_f = etree.SubElement(parent, 'f')
+    case_f.attrib['name'] = 'case'
+    case_symbol = etree.SubElement(case_f,'symbol')
+    case_symbol.attrib['value'] = attributes.case.name
+
+def write_comprepnp_attributes(parent, attributes):
+    # expansions file does not exist
+    prep_f = etree.SubElement(parent, 'f')
+    prep_f.attrib['name'] = 'complex_preposition'
+    prep_string = etree.SubElement(prep_f,'string')
+    prep_string.text = attributes.comprep.name
+    
+def write_cp_attributes(parent, attributes):
+    type_f = etree.SubElement(parent, 'f')
+    type_f.attrib['name'] = 'type'
+    typedef_fs = etree.SubElement(type_f, 'fs')
+    typedef_fs.attrib['type'] = 'type_def'
+    conj_f = etree.SubElement(typedef_fs, 'f')
+    conj_f.attrib['name'] = 'conjunction'
+    conj_symbol = etree.SubElement(conj_f, 'symbol')
+    conj_symbol.attrib['value'] = attributes.cptype.name
+
+    if len(attributes.cpreals.all()) > 0:
+        constr_f = etree.SubElement(typedef_fs, 'f')
+        constr_f.attrib['name'] = 'constraints'
+        vColl = etree.SubElement(constr_f, 'vColl')
+        vColl.attrib['org'] = 'set'
+        for constraint in attributes.cpreals.all():
+            constraint_symbol = etree.SubElement(parent, 'symbol')
+            constraint_symbol.attrib['value'] = constraint.name
+
+def write_ncp_attributes(parent, attributes):
+    case_f = etree.SubElement(parent, 'f')
+    case_f.attrib['name'] = 'case'
+    case_symbol = etree.SubElement(case_f,'symbol')
+    case_symbol.attrib['value'] = attributes.case.name    
+    type_f = etree.SubElement(parent, 'f')
+    type_f.attrib['name'] = 'type'
+    typedef_fs = etree.SubElement(type_f, 'fs')
+    typedef_fs.attrib['type'] = 'type_def'
+    conj_f = etree.SubElement(typedef_fs, 'f')
+    conj_f.attrib['name'] = 'conjunction'
+    conj_symbol = etree.SubElement(conj_f, 'symbol')
+    conj_symbol.attrib['value'] = attributes.cptype.name
+
+    if len(attributes.cpreals.all()) > 0:
+        constr_f = etree.SubElement(typedef_fs, 'f')
+        constr_f.attrib['name'] = 'constraints'
+        vColl = etree.SubElement(constr_f, 'vColl')
+        vColl.attrib['org'] = 'set'
+        for constraint in attributes.cpreals.all():
+            constraint_symbol = etree.SubElement(parent, 'symbol')
+            constraint_symbol.attrib['value'] = constraint.name
+
+def write_prepncp_attributes(parent, attributes):
+    prep_f = etree.SubElement(parent, 'f')
+    prep_f.attrib['name'] = 'preposition'
+    prep_symbol = etree.SubElement(prep_f,'symbol')
+    prep_symbol.attrib['value'] = attributes.prep.name
+    case_f = etree.SubElement(parent, 'f')
+    case_f.attrib['name'] = 'case'
+    case_symbol = etree.SubElement(case_f,'symbol')
+    case_symbol.attrib['value'] = attributes.case.name    
+    type_f = etree.SubElement(parent, 'f')
+    type_f.attrib['name'] = 'type'
+    typedef_fs = etree.SubElement(type_f, 'fs')
+    typedef_fs.attrib['type'] = 'type_def'
+    conj_f = etree.SubElement(typedef_fs, 'f')
+    conj_f.attrib['name'] = 'conjunction'
+    conj_symbol = etree.SubElement(conj_f, 'symbol')
+    conj_symbol.attrib['value'] = attributes.cptype.name
+
+    if len(attributes.cpreals.all()) > 0:
+        constr_f = etree.SubElement(typedef_fs, 'f')
+        constr_f.attrib['name'] = 'constraints'
+        vColl = etree.SubElement(constr_f, 'vColl')
+        vColl.attrib['org'] = 'set'
+        for constraint in attributes.cpreals.all():
+            constraint_symbol = etree.SubElement(parent, 'symbol')
+            constraint_symbol.attrib['value'] = constraint.name
+
+def write_nonch_attributes(parent, attributes):
+    pass
+
+def write_infp_attributes(parent, attributes):
+    asp_f = etree.SubElement(parent, 'f')
+    asp_f.attrib['name'] = 'aspect'
+    asp_symbol = etree.SubElement(asp_f,'symbol')
+    asp_symbol.attrib['value'] = attributes.aspect.name
+
+def write_advp_attributes(parent, attributes):
+    cat_f = etree.SubElement(parent, 'f')
+    cat_f.attrib['name'] = 'category'
+    cat_symbol = etree.SubElement(cat_f,'symbol')
+    cat_symbol.attrib['value'] = attributes.advcat.name
+
+def write_xp_attributes(parent, attributes):
+    # no expansions file
+    cat_f = etree.SubElement(parent, 'f')
+    cat_f.attrib['name'] = 'category'
+    catdef_fs = etree.SubElement(cat_f, 'fs')
+    catdef_fs.attrib['type'] = 'category_def'
+    name_f = etree.SubElement(catdef_fs, 'f')
+    name_f.attrib['name'] = 'name'
+    name_symbol = etree.SubElement(name_f, 'symbol')
+    name_symbol.attrib['value'] = attributes.advcat.name
+
+    if len(attributes.reals.all()) > 0:
+        constr_f = etree.SubElement(catdef_fs, 'f')
+        constr_f.attrib['name'] = 'constraints'
+        vColl = etree.SubElement(constr_f, 'vColl')
+        vColl.attrib['org'] = 'set'
+        for constraint in attributes.reals.all():
+            write_phrase_structure(vColl, constraint)
+
+def write_e_attributes(parent, attributes):
+    pass
+
+def write_or_attributes(parent, attributes):
+    pass
+
+def write_refl_attributes(parent, attributes):
+    pass
+
+def write_recip_attributes(parent, attributes):
+    pass
+
+def write_distrp_attributes(parent, attributes):
+    # no expansions file
+    pass
+
+def write_nump_attributes(parent, attributes):
+    case_f = etree.SubElement(parent, 'f')
+    case_f.attrib['name'] = 'case'
+    case_symbol = etree.SubElement(case_f,'symbol')
+    case_symbol.attrib['value'] = attributes.case.name
+    
+def write_prepnump_attributes(parent, attributes):
+    prep_f = etree.SubElement(parent, 'f')
+    prep_f.attrib['name'] = 'preposition'
+    prep_symbol = etree.SubElement(prep_f,'symbol')
+    prep_symbol.attrib['value'] = attributes.prep.name
+    case_f = etree.SubElement(parent, 'f')
+    case_f.attrib['name'] = 'case'
+    case_symbol = etree.SubElement(case_f,'symbol')
+    case_symbol.attrib['value'] = attributes.case.name
+
+def write_pactp_attributes(parent, attributes):
+    case_f = etree.SubElement(parent, 'f')
+    case_f.attrib['name'] = 'case'
+    case_symbol = etree.SubElement(case_f,'symbol')
+    case_symbol.attrib['value'] = attributes.case.name
+
+def write_preppactp_attributes(parent, attributes):
+    prep_f = etree.SubElement(parent, 'f')
+    prep_f.attrib['name'] = 'preposition'
+    prep_symbol = etree.SubElement(prep_f,'symbol')
+    prep_symbol.attrib['value'] = attributes.prep.name
+    case_f = etree.SubElement(parent, 'f')
+    case_f.attrib['name'] = 'case'
+    case_symbol = etree.SubElement(case_f,'symbol')
+    case_symbol.attrib['value'] = attributes.case.name
+
+def write_ppasp_attributes(parent, attributes):
+    case_f = etree.SubElement(parent, 'f')
+    case_f.attrib['name'] = 'case'
+    case_symbol = etree.SubElement(case_f,'symbol')
+    case_symbol.attrib['value'] = attributes.case.name
+    
+def write_prepppasp_attributes(parent, attributes):
+    prep_f = etree.SubElement(parent, 'f')
+    prep_f.attrib['name'] = 'preposition'
+    prep_symbol = etree.SubElement(prep_f,'symbol')
+    prep_symbol.attrib['value'] = attributes.prep.name
+    case_f = etree.SubElement(parent, 'f')
+    case_f.attrib['name'] = 'case'
+    case_symbol = etree.SubElement(case_f,'symbol')
+    case_symbol.attrib['value'] = attributes.case.name
+
+def write_gerp_attributes(parent, attributes):
+    case_f = etree.SubElement(parent, 'f')
+    case_f.attrib['name'] = 'case'
+    case_symbol = etree.SubElement(case_f,'symbol')
+    case_symbol.attrib['value'] = attributes.case.name
+
+def write_prepgerp_attributes(parent, attributes):
+    prep_f = etree.SubElement(parent, 'f')
+    prep_f.attrib['name'] = 'preposition'
+    prep_symbol = etree.SubElement(prep_f,'symbol')
+    prep_symbol.attrib['value'] = attributes.prep.name
+    case_f = etree.SubElement(parent, 'f')
+    case_f.attrib['name'] = 'case'
+    case_symbol = etree.SubElement(case_f,'symbol')
+    case_symbol.attrib['value'] = attributes.case.name
+
+def write_qub_attributes(parent, attributes):
+    pass
+
+def write_fixed_attributes(parent, attributes):
+    arg_f = etree.SubElement(parent, 'f')
+    arg_f.attrib['name'] = 'argument'
+    write_phrase_structure(arg_f, attributes.phrase)
+    fixed_f = etree.SubElement(parent, 'f')
+    fixed_f.attrib['name'] = 'string'
+    fixed_string = etree.SubElement(fixed_f, 'string')
+    fixed_string.text = attributes.text
+
+def write_lex(parent, phrase_type):
+    write_lex_argument(parent, phrase_type)
+    write_lex_parameters(parent, phrase_type)
+    write_lex_lemma(parent, phrase_type)
+    write_lex_modification(parent, phrase_type)
+    
+def write_lex_argument(parent, phrase_type):
+    arg_f = etree.SubElement(parent, 'f')
+    arg_f.attrib['name'] = 'argument'
+    write_phrase_structure(arg_f, phrase_type.lexicalized_phrase)
+    
+def write_lex_parameters(parent, phrase_type):
+    if phrase_type.lexicalized_phrase.main_type.name == 'np':
+        write_lexnp_attributes(parent, phrase_type.attributes.lexphrasetypeattributes.lexnpattributes)
+    elif phrase_type.lexicalized_phrase.main_type.name == 'adjp':
+        write_lexadjp_attributes(parent, phrase_type.attributes.lexphrasetypeattributes.lexadjpattributes)
+    elif phrase_type.lexicalized_phrase.main_type.name == 'possp':
+        pass
+    elif phrase_type.lexicalized_phrase.main_type.name == 'compar':
+        write_lexcompar_attributes(parent, phrase_type.attributes.lexphrasetypeattributes.lexcomparattributes)
+    elif phrase_type.lexicalized_phrase.main_type.name == 'prepnp':
+        write_lexprepnp_attributes(parent, phrase_type.attributes.lexphrasetypeattributes.lexprepnpattributes)
+    elif phrase_type.lexicalized_phrase.main_type.name == 'prepadjp':
+        write_lexprepadjp_attributes(parent, phrase_type.attributes.lexphrasetypeattributes.lexprepadjpattributes)
+    elif phrase_type.lexicalized_phrase.main_type.name == 'comprepnp':
+        pass
+    elif phrase_type.lexicalized_phrase.main_type.name == 'cp':
+        write_lexcp_attributes(parent, phrase_type.attributes.lexphrasetypeattributes.lexcpattributes)
+    elif phrase_type.lexicalized_phrase.main_type.name == 'ncp':
+        write_lexncp_attributes(parent, phrase_type.attributes.lexphrasetypeattributes.lexncpattributes)
+    elif phrase_type.lexicalized_phrase.main_type.name == 'prepncp':
+        # not present in the database
+        write_lexprepncp_attributes(parent, phrase_type.attributes.lexphrasetypeattributes.lexprepncpattributes)
+    elif phrase_type.lexicalized_phrase.main_type.name == 'nonch':
+        pass
+    elif phrase_type.lexicalized_phrase.main_type.name == 'infp':
+        write_lexinfp_attributes(parent, phrase_type.attributes.lexphrasetypeattributes.lexinfpattributes)
+    elif phrase_type.lexicalized_phrase.main_type.name == 'advp':
+        write_lexadvp_attributes(parent, phrase_type.attributes.lexphrasetypeattributes.lexadvpattributes)
+    elif phrase_type.lexicalized_phrase.main_type.name == 'xp':
+        write_lex_parameters(parent, phrase_type.attributes.lexphrasetypeattributes.lexxpattributes.lex)
+    elif phrase_type.lexicalized_phrase.main_type.name == 'E':
+        pass
+    elif phrase_type.lexicalized_phrase.main_type.name == 'or':
+        pass
+    elif phrase_type.lexicalized_phrase.main_type.name == 'refl':
+        pass
+    elif phrase_type.lexicalized_phrase.main_type.name == 'recip':
+        pass
+    elif phrase_type.lexicalized_phrase.main_type.name == 'distrp':
+        pass
+    elif phrase_type.lexicalized_phrase.main_type.name == 'nump':
+        pass
+    elif phrase_type.lexicalized_phrase.main_type.name == 'prepnump':
+        pass
+    elif phrase_type.lexicalized_phrase.main_type.name == 'pactp':
+        write_lexpactp_attributes(parent, phrase_type.attributes.lexphrasetypeattributes.lexpactpattributes)
+    elif phrase_type.lexicalized_phrase.main_type.name == 'preppactp':
+        # not present in database
+        write_lexpreppactp_attributes(parent, phrase_type.attributes.lexphrasetypeattributes.lexpreppactpattributes)
+    elif phrase_type.lexicalized_phrase.main_type.name == 'ppasp':
+        write_lexppasp_attributes(parent, phrase_type.attributes.lexphrasetypeattributes.lexppaspattributes)
+    elif phrase_type.lexicalized_phrase.main_type.name == 'prepppasp':
+        write_lexprepppasp_attributes(parent, phrase_type.attributes.lexphrasetypeattributes.lexprepppaspattributes)
+    elif phrase_type.lexicalized_phrase.main_type.name == 'gerp':
+        # not present in database
+        write_lexgerp_attributes(parent, phrase_type.attributes.lexphrasetypeattributes.lexgerpattributes)
+    elif phrase_type.lexicalized_phrase.main_type.name == 'prepgerp':
+        write_lexprepgerp_attributes(parent, phrase_type.attributes.lexphrasetypeattributes.lexprepgerpattributes)
+    elif phrase_type.lexicalized_phrase.main_type.name == 'qub':
+        pass
+    elif phrase_type.lexicalized_phrase.main_type.name == 'lex':
+        pass
+    elif phrase_type.lexicalized_phrase.main_type.name == 'fixed':
+        pass
+    else:
+        print(phrase_type.name)
+        raise LexicalisedPhraseTypeError()
+    
+def write_lex_lemma(parent, phrase_type):
+    if phrase_type.lexicalized_phrase.main_type.name == 'compar':
+        return
+    pt = phrase_type
+    if phrase_type.lexicalized_phrase.main_type.name == 'xp':
+        pt = phrase_type.attributes.lexphrasetypeattributes.lexxpattributes.lex
+    print(phrase_type.id)
+    lemma_f = etree.SubElement(parent, 'f')
+    lemma_f.attrib['name'] = 'lemma'
+    lemma_fs = etree.SubElement(lemma_f, 'fs')
+    lemma_fs.attrib['type'] = 'lemma_def'
+    
+    select_f = etree.SubElement(lemma_fs, 'f')
+    select_f.attrib['name'] = 'selection_mode'
+    select_symbol = etree.SubElement(select_f, 'symbol')
+    select_symbol.attrib['value'] = pt.lemma_operator.name
+    
+    cooc_f = etree.SubElement(lemma_fs, 'f')
+    cooc_f.attrib['name'] = 'selection_mode'
+    cooc_symbol = etree.SubElement(cooc_f, 'symbol')
+    cooc_symbol.attrib['value'] = pt.lemma_cooccur.name
+
+    lemmas_f = etree.SubElement(lemma_fs, 'f')
+    lemmas_f.attrib['name'] = 'lemmas'
+    vColl = etree.SubElement(lemmas_f, 'vColl')
+    vColl.attrib['org'] = 'set'
+    for lemma in pt.lemmata.all():
+        lemma_string = etree.SubElement(vColl, 'string')
+        lemma_string.text = lemma.name
+
+def write_lex_modification(parent, phrase_type):
+    if phrase_type.lexicalized_phrase.main_type.name == 'compar':
+        return
+    pt = phrase_type
+    if phrase_type.lexicalized_phrase.main_type.name == 'xp':
+        pt = phrase_type.attributes.lexphrasetypeattributes.lexxpattributes.lex
+    mod_f = etree.SubElement(parent, 'f')
+    mod_f.attrib['name'] = 'modification'
+    mod_fs = etree.SubElement(mod_f, 'fs')
+    mod_fs.attrib['type'] = 'modification_def'
+    type_f = etree.SubElement(mod_fs, 'f')
+    type_f.attrib['name']  = 'type'
+    type_symbol = etree.SubElement(type_f, 'symbol')
+    type_symbol.attrib['value'] = pt.modification.mod_type.name
+
+    if len(pt.modification.positions.all()) > 0:
+        positions = pt.modification.positions.all()
+        positions_f = etree.SubElement(parent, 'f')
+        positions_f.attrib['name'] = 'positions'
+        vColl = etree.SubElement(positions_f, 'vColl')
+        vColl.attrib['org'] = 'set'
+        for position in positions:
+            write_position(vColl, None, None, position, True)
+
+def write_lexnp_attributes(parent, attributes):
+    num_f = etree.SubElement(parent, 'f')
+    num_f.attrib['name'] = 'number'
+    num_symbol = etree.SubElement(num_f, 'symbol')
+    num_symbol.attrib['value'] = attributes.num.name
+
+def write_lexadjp_attributes(parent, attributes):
+    num_f = etree.SubElement(parent, 'f')
+    num_f.attrib['name'] = 'number'
+    num_symbol = etree.SubElement(num_f, 'symbol')
+    num_symbol.attrib['value'] = attributes.num.name
+    gen_f = etree.SubElement(parent, 'f')
+    gen_f.attrib['name'] = 'gender'
+    gen_symbol = etree.SubElement(gen_f, 'symbol')
+    gen_symbol.attrib['value'] = attributes.gend.name
+    deg_f = etree.SubElement(parent, 'f')
+    deg_f.attrib['name'] = 'degree'
+    deg_symbol = etree.SubElement(deg_f, 'symbol')
+    deg_symbol.attrib['value'] = attributes.deg.name
+
+def write_lexcompar_attributes(parent, attributes):
+    arg_f = etree.SubElement(parent, 'f')
+    arg_f.attrib['argument'] = 'arguments'
+    vColl = etree.SubElement(arg_f, 'vColl')
+    vColl.attrib['org'] = 'set'
+    for lex in attributes.lexes.all():
+        write_phrase_structure(vColl, lex)
     
+def write_lexprepnp_attributes(parent, attributes):
+    num_f = etree.SubElement(parent, 'f')
+    num_f.attrib['name'] = 'number'
+    num_symbol = etree.SubElement(num_f, 'symbol')
+    num_symbol.attrib['value'] = attributes.num.name
+
+def write_lexprepadjp_attributes(parent, attributes):
+    num_f = etree.SubElement(parent, 'f')
+    num_f.attrib['name'] = 'number'
+    num_symbol = etree.SubElement(num_f, 'symbol')
+    num_symbol.attrib['value'] = attributes.num.name
+    gen_f = etree.SubElement(parent, 'f')
+    gen_f.attrib['name'] = 'gender'
+    gen_symbol = etree.SubElement(gen_f, 'symbol')
+    gen_symbol.attrib['value'] = attributes.gend.name
+    deg_f = etree.SubElement(parent, 'f')
+    deg_f.attrib['name'] = 'degree'
+    deg_symbol = etree.SubElement(deg_f, 'symbol')
+    deg_symbol.attrib['value'] = attributes.deg.name
+
+def write_lexcp_attributes(parent, attributes):
+    neg_f = etree.SubElement(parent, 'f')
+    neg_f.attrib['name'] = 'negativity'
+    neg_symbol = etree.SubElement(neg_f, 'symbol')
+    neg_symbol.attrib['value'] = attributes.num.name
+    inh_f = etree.SubElement(parent, 'f')
+    inh_f.attrib['name'] = 'inherent_sie'
+    inh_symbol = etree.SubElement(inh_f, 'symbol')
+    inh_symbol.attrib['value'] = attributes.inhsie.name
+
+def write_lexncp_attributes(parent, attributes):
+    neg_f = etree.SubElement(parent, 'f')
+    neg_f.attrib['name'] = 'negativity'
+    neg_symbol = etree.SubElement(neg_f, 'symbol')
+    neg_symbol.attrib['value'] = attributes.num.name
+    inh_f = etree.SubElement(parent, 'f')
+    inh_f.attrib['name'] = 'inherent_sie'
+    inh_symbol = etree.SubElement(inh_f, 'symbol')
+    inh_symbol.attrib['value'] = attributes.inhsie.name
+
+def write_lexprepncp_attributes(parent, attributes):
+    # not present in database
+    pass
+
+def write_lexinfp_attributes(parent, attributes):
+    neg_f = etree.SubElement(parent, 'f')
+    neg_f.attrib['name'] = 'negativity'
+    neg_symbol = etree.SubElement(neg_f, 'symbol')
+    neg_symbol.attrib['value'] = attributes.num.name
+    inh_f = etree.SubElement(parent, 'f')
+    inh_f.attrib['name'] = 'inherent_sie'
+    inh_symbol = etree.SubElement(inh_f, 'symbol')
+    inh_symbol.attrib['value'] = attributes.inhsie.name
+
+def write_lexadvp_attributes(parent, attributes):
+    deg_f = etree.SubElement(parent, 'f')
+    deg_f.attrib['name'] = 'degree'
+    deg_symbol = etree.SubElement(deg_f, 'symbol')
+    deg_symbol.attrib['value'] = attributes.deg.name
+
+def write_lexpactp_attributes(parent, attributes):
+    num_f = etree.SubElement(parent, 'f')
+    num_f.attrib['name'] = 'number'
+    num_symbol = etree.SubElement(num_f, 'symbol')
+    num_symbol.attrib['value'] = attributes.num.name
+    gen_f = etree.SubElement(parent, 'f')
+    gen_f.attrib['name'] = 'gender'
+    gen_symbol = etree.SubElement(gen_f, 'symbol')
+    gen_symbol.attrib['value'] = attributes.gend.name
+
+def write_lexpreppactp_attributes(parent, attributes):
+    # not present in database
+    pass
+
+def write_lexppasp_attributes(parent, attributes):
+    num_f = etree.SubElement(parent, 'f')
+    num_f.attrib['name'] = 'number'
+    num_symbol = etree.SubElement(num_f, 'symbol')
+    num_symbol.attrib['value'] = attributes.num.name
+    gen_f = etree.SubElement(parent, 'f')
+    gen_f.attrib['name'] = 'gender'
+    gen_symbol = etree.SubElement(gen_f, 'symbol')
+    gen_symbol.attrib['value'] = attributes.gend.name
+
+def write_lexprepppasp_attributes(parent, attributes):
+    num_f = etree.SubElement(parent, 'f')
+    num_f.attrib['name'] = 'number'
+    num_symbol = etree.SubElement(num_f, 'symbol')
+    num_symbol.attrib['value'] = attributes.num.name
+    gen_f = etree.SubElement(parent, 'f')
+    gen_f.attrib['name'] = 'gender'
+    gen_symbol = etree.SubElement(gen_f, 'symbol')
+    gen_symbol.attrib['value'] = attributes.gend.name
+
+def write_lexgerp_attributes(parent, attributes):
+    # not present in database
+    pass
+
+def write_lexprepgerp_attributes(parent, attributes):
+    num_f = etree.SubElement(parent, 'f')
+    num_f.attrib['name'] = 'number'
+    num_symbol = etree.SubElement(num_f, 'symbol')
+    num_symbol.attrib['value'] = attributes.num.name
-- 
GitLab