luscenje_struktur/src/match.py

42 lines
1.4 KiB
Python

from word import Word
class StructureMatch:
def __init__(self, match_id, structure):
self.match_id = str(match_id)
self.structure = structure
self.matches = []
self.representations = {}
@staticmethod
def from_db(db, colocation_id, structure):
result = StructureMatch(colocation_id, structure)
for match_id in db.execute("SELECT mid_match_id FROM ColocationMatches WHERE mid_colocation_id=?", (colocation_id,)):
to_add = {}
for component_id, word_lemma, word_text, word_msd, word_id in db.execute("""
SELECT component_id, word_lemma, word_text, word_msd, word_id
FROM Matches WHERE match_id=?""", match_id):
to_add[str(component_id)] = Word(word_lemma, word_msd, word_id, word_text, False)
result.matches.append(to_add)
for component_id, text in db.execute("SELECT component_id, text FROM Representations WHERE colocation_id=?", (colocation_id,)):
result.representations[str(component_id)] = text
return result
def distinct_forms(self):
dm = set()
keys = list(self.matches[0].keys())
for words in self.matches:
dm.add(" ".join(words[k].text for k in keys))
return len(dm)
def append(self, match):
self.matches.append(match)
def __len__(self):
return len(self.matches)