util.py 1.2 KB
# -*- coding: utf-8 -*-


def prepare_table(table):
    for row in table:
        for cell in row:
            if type(cell) == dict and 'forms' in cell:
                cell['forms'].sort()
                seen_forms = []
                unique_forms = []
                form_patterns = {}
                for form in cell['forms']:
                    if form[1] not in seen_forms:
                        seen_forms.append(form[1])
                        unique_forms.append(form)
                        form_patterns[form[1]] = set()
                    form_patterns[form[1]].add(form[3])
                cell['forms'] = [
                    {
                        'form': form,
                        'qualifiers': qualifiers,
                        'patterns': form_patterns[form],
                    }
                    for (key, form, qualifiers, pattern) in unique_forms]
            elif type(cell) == dict and 'label' in cell:
                seen_labels = []

                def is_new(label):
                    new = label not in seen_labels
                    seen_labels.append(label)
                    return new

                cell['label'] = filter(is_new, cell['label'])
    return table