#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
import codecs
import re
class LMF:
def __init__(self, lang):
self.lang = lang
self.lexical_entries = []
self._lexical_entries_set = set()
self._le_senses = set()
self.useNamespace = False
self.semantic_predicates = []
def add_lexical_entry(self, lexical_entry):
self.lexical_entries.append(lexical_entry)
self._lexical_entries_set.add(".".join([lexical_entry._pos, lexical_entry._wf])) # Ett fulhack for att speeda upp det lite (ersatt med nagot battre i framtiden)
def add_semantic_predicate(self, semantic_predicate):
self.semantic_predicates.append(semantic_predicate)
def __str__(self):
return "\n".join([
'',
'',
'',
'',
' ',
'',
'',
' ' % self.lang,
"\n".join([str(e) for e in self.lexical_entries]),
"\n".join([str(s) for s in self.semantic_predicates]),
'',
''])
class LexicalEntry:
def __init__(self):
self.features = []
self.lemma = None
self.wordforms = []
self.senses = []
self.saldolinks = []
self._pos = ""
self._wf = ""
self.idattr = ""
self.components = []
def add_sense(self, sense):
self.senses.append(sense)
def add_feature(self, feature):
self.features.append(feature)
def add_feature_unique(self, feature):
for existing_feature in self.features:
if(existing_feature.att == feature.att and existing_feature.val == feature.val):
return
self.add_feature(feature)
def get_components(self):
if self.components:
return '\n'.join(['',
'\n'.join([str(c) for c in self.components]),
''])
else:
return ''
def add_component(self, component):
self.components.append(component)
def add_wordform(self, wordform):
self.wordforms.append(wordform)
def add_saldoLink(self, saldoLink):
self.saldolinks.append(saldoLink)
def __str__(self):
le_string = ''
if(self.idattr):
le_string = '' % (self.idattr)
return "\n".join([
le_string,
'\n'.join([str(f) for f in self.features]),
self.get_components(),
str(self.lemma),
'\n'.join([str(w) for w in self.wordforms]),
'\n'.join([str(s) for s in self.senses]),
'\n'.join([str(f) for f in self.saldolinks]),
''])
class Component:
def __init__(self, ref):
self.ref = ref
def __str__(self):
return '' % (self.ref)
class SaldoLink:
def __init__(self, saldo_id):
self.saldo_id = saldo_id
def __str__(self):
return '' % (self.saldo_id)
"""
class Lemma:
def __init__(self):
self.features = [] # now including writtenForm and partOfSpeech!
def add_feature(self, feature):
self.features.append(feature)
def add_feature_unique(self, feature):
for existing_feature in self.features:
if(existing_feature.att == feature.att and existing_feature.val == feature.val):
return
self.add_feature(feature)
def __str__(self):
if self.features:
return "\n".join(['\n',
'\n'.join([str(f) for f in self.features]),
'\n'])
else:
return ''
"""
class Lemma:
def __init__(self):
self.form_representations = []
self.features = [] # now including writtenForm and partOfSpeech!
def add_feature(self, feature):
self.features.append(feature)
def add_feature_unique(self, feature):
for existing_feature in self.features:
if(existing_feature.att == feature.att and existing_feature.val == feature.val):
return
self.add_feature(feature)
def add_form_representation(self, form_representation):
self.form_representations.append(form_representation)
def __str__(self):
if self.features or self.form_representations:
return "\n".join(['', '\n'.join(str(fr) for fr in self.form_representations),''])
#return "\n".join(['\n',
# '\n'.join([str(f) for f in self.features]),
# '\n'])
else:
return ''
class WordForm:
def __init__(self):
self.features = []
self.form_representations = []
def add_feature(self, feature):
self.features.append(feature)
def add_form_representation(self, form_representation):
self.form_representations.append(form_representation)
def __str__(self):
return "\n".join(['',
'\n'.join(str(fr) for fr in self.form_representations),
'\n'.join([str(f) for f in self.features]),
''])
class FormRepresentation:
def __init__(self):
self.features = []
def add_feature(self, feature):
self.features.append(feature)
def add_feature_unique(self, feature):
for existing_feature in self.features:
if(existing_feature.att == feature.att and existing_feature.val == feature.val):
return
self.add_feature(feature)
def __str__(self):
if self.features:
return "\n".join(['','\n'.join([str(f) for f in self.features]),''])
else:
return ''
class Feature:
def __init__(self, att, val):
self.att = att
self.val = val
def __str__(self):
return '' % (self.att, escape(self.val))
class Sense:
def __init__(self, sense):
self.sense = sense
self.relations = []
self.predicative_representations = []
self.sense_examples = []
self.features = []
def add_sense_relation(self, sense_relation):
self.relations.append(sense_relation)
def add_predicative_representation(self, predicative_representation):
self.predicative_representations.append(predicative_representation)
def add_sense_example(self, sense_example):
self.sense_examples.append(sense_example)
def add_feature(self, feature):
self.features.append(feature)
def __str__(self):
if not self.relations and not self.predicative_representations and not self.sense_examples and not self.features:
return '' % (self.sense)
else:
return "\n".join(['' % (self.sense),
'\n'.join([str(f) for f in self.features]),
"\n".join([str(pre) for pre in self.predicative_representations]),
"\n".join([str(rel) for rel in self.relations]),
"\n".join([str(ex) for ex in self.sense_examples]),
''
])
class SenseRelation:
def __init__(self, target, relation_types):
self.target = target
self.relation_types = relation_types
self.features = []
def add_feature(self, feature):
self.features.append(feature)
def __str__(self):
return "\n".join(['' % (self.target),
'\n'.join(['' % t for t in self.relation_types]),
'\n'.join([str(f) for f in self.features]),
''
])
class SenseExample:
def __init__(self, example):
self.example = example
def __str__(self):
return "\n".join([
'',
'' % (self.example),
''
])
class SemanticPredicate:
def __init__(self, id, domain, semantic_types):
self.id = id
#self.domain = domain
self.semantic_types = semantic_types
self.semantic_arguments = []
self.features = []
if domain != None and domain != "":
self.add_feature(Feature("domain", domain))
def add_semantic_argument(self, argument):
self.semantic_arguments.append(argument)
def add_feature(self, feature):
self.features.append(feature)
def generateFeatures(self, att, vals):
for val in vals:
self.add_feature(Feature(att, val.strip()))
def __str__(self):
extras = ""
for st in self.semantic_types:
extras += ''
return "\n".join([
'' % (self.id),
"\n".join(['\n' % (st) for st in self.semantic_types]),
"\n".join([str(fe) for fe in self.features]),
"\n".join([str(sa) for sa in self.semantic_arguments]),
''
])
class SemanticArgument:
def __init__(self, semantic_role, core_type):
self.semantic_role = semantic_role
self.core_type = core_type
def __str__(self):
return '' % (self.semantic_role, self.core_type)
class PredicativeRepresentation:
def __init__(self, idref):
self.idref = idref
def __str__(self):
return '' % (self.idref, self.idref)
# HELPER FUNCTIONS -------------------------------------------------------------------------------------
def escape(s):
s = s.replace('&', '&')
s = s.replace("'", ''')
s = s.replace('<', '<')
s = s.replace('>', '>')
return s.replace('"', '"')