from luscenje_struktur.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) prev_match_id = None stmt = """SELECT match_id, component_id, word_lemma, word_text, word_msd, word_id FROM ColocationMatches JOIN Matches ON Matches.match_id=ColocationMatches.mid_match_id WHERE mid_colocation_id=? ORDER BY match_id""" for row in db.execute(stmt, (colocation_id,)): match_id, component_id, word_lemma, word_text, word_msd, word_id = row if match_id != prev_match_id: result.matches.append({}) prev_match_id = match_id result.matches[-1][str(component_id)] = Word(word_lemma, word_msd, word_id, word_text, False) for component_id, text, msd in db.execute("SELECT component_id, text, msd FROM Representations WHERE colocation_id=?", (colocation_id,)): result.representations[str(component_id)] = (text, msd) 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)