descriptions.py 29.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
#from .parse_phrase_type import parse_phrase_rep

from itertools import chain, combinations, product

from .polish_strings import *
from .morph_generation import select_form

from importer.Phrase import *

def powerset(iterable):
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))

def powerset_nonempty(iterable):
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(1, len(s) + 1))

# keeps the element order
def uniq_list(iterable):
    u = []
    for element in iterable:
        if element not in u:
            u.append(element)
    return u

def position_prop_description(prop):
    return POSITION_PROP[prop]

def phrase_description2(phrase, position, negativity):
    function = position._function._value if position._function else None
    negativity = negativity.name if negativity else '_'
    desc = phrase_description(phrase, function, negativity)
    print(desc)
    return desc

def phrase_description(phrase, function, negativity, desc_case='nom', inside_lex=False):
    print('******', function, '***', negativity, '***', str(phrase))
    if str(phrase) in (
            'lex(np(str),pl,usta,ratr1({lex(adjp(agr),pl,agr,pos,pełny,ratr1({np(gen)}))}))',
            'lex(xp(mod[np(inst)]),sg,ręka,ratr1({lex(adjp(agr),sg,agr,pos,lekki,natr)}))',
            'lex(adjp(agr),agr,agr,pos,malować,natr)',
            'lex(compar(jak),lex(np(nom),sg,wół,natr),lex(prepnp(na,acc),pl,wrota,ratr1({lex(adjp(agr),agr,agr,pos,malować,natr)})),natr)',
            'lex(compar(jak),lex(ppasp(agr),agr,agr,aff,oszaleć,natr),natr)',
            'lex(np(str),_,uwaga,ratr({adjp(agr)}+{or}))',
            'lex(xp(mod[prepnp(z,gen)]),sg,góry,natr)', 'lex(np(str),_,uwaga,atr({adjp(agr)}+{or}))',
            'lex(cp(int),aff,wleźć,,ratr1(subj{lex(nump(nom),ile,E(n),natr)}))',
            'lex(cp(int),aff,dziękować,,atr)',
            'lex(cp(int),aff,żyć,,ratr1({lex(prepnp(na,loc),sg,świat,ratr1({lex(adjp(agr),agr,agr,pos,jaki,natr)}))}))',
            ):
        return '??? TODO'
    if str(phrase) in ('ncp(part,że)',):
        return 'fraza dziwna TODO'
    if str(phrase).startswith('lex'):
        return lex_phrase_description(phrase, function, negativity, desc_case)
    return make_phrase_description(phrase, function, negativity, desc_case, inside_lex=inside_lex)

def get_phrase_type(lex_phrase):
    ptype = type(lex_phrase)
    if ptype == LexNP:
        return lex_phrase._np
    if ptype == LexNumP:
        return lex_phrase._nump
    if ptype == LexAdjP:
        return lex_phrase._adjp
    if ptype == LexPPasP:
        return lex_phrase._ppasp
    if ptype == LexPActP:
        return lex_phrase._pactp
    if ptype == LexPrepNP:
        return lex_phrase._prepnp
    if ptype == LexPrepGerP:
        return lex_phrase._prepgerp
    if ptype == LexPrepNumP:
        return lex_phrase._prepnump
    if ptype == LexPrepAdjP:
        return lex_phrase._prepadjp
    if ptype == LexPrepPPasP:
        return lex_phrase._prepppasp
    if ptype == LexInfP:
        return lex_phrase._infp
    if ptype == LexCP:
        return lex_phrase._cp
    if ptype == LexNCP:
        return lex_phrase._ncp
    if ptype == LexXP:
        return lex_phrase._xp
    if ptype == LexAdvP:
        return lex_phrase._advp
    if ptype == LexCompar:
        return lex_phrase._compar
    if ptype == LexQub:
        return lex_phrase._qub
    print(ptype)
    1/0

def lex_phrase_description(phrase, function, negativity, desc_case='nom'):
    phrase2 = get_phrase_type(phrase)
    #if type(phrase2) in (CP,):
    #    return 'frazeologizm będący cepem TODO'
    desc = make_phrase_description(phrase2, function, negativity, 'inst', inside_lex=True)
    return 'frazeologizm będący ' + desc + ' postaci' + make_ul(map('<i>{}</i>'.format, uniq_list(make_phraseologisms(phrase, function, negativity))))

def make_phrase_description(phrase, function, negativity, desc_case, inside_lex=False):
    ptype = type(phrase)
    if ptype == NP:
        case = phrase._case._value
        if function == 'subj' and case == 'str':
            case = 'str_subj'
        return make_inflected_string(NP_, desc_case).format(case=CASE_FOR_NP[case])
    if ptype == NumP:
        case = phrase._case._value
        return make_inflected_string(NUMP, desc_case).format(case=CASE_FOR_NP[case])
    if ptype == AdjP:
        return make_inflected_string(ADJP, desc_case).format(case=CASE_FOR_ADJP[phrase._case._value])
    if ptype == PPasP:
        return make_inflected_string(PPASP, desc_case).format(case=CASE_FOR_ADJP[phrase._case._value])
    if ptype == PActP:
        return make_inflected_string(PACTP, desc_case).format(case=CASE_FOR_ADJP[phrase._case._value])
    if ptype == PrepNP:
        prep, case = phrase._prep._value, phrase._prep._case._value
        return make_inflected_string(PREPNP, desc_case).format(prep=prep, case=CASE_FOR_PREPNP[case])
    if ptype == PrepGerP:
        prep, case = phrase._prep._value, phrase._prep._case._value
        return make_inflected_string(PREPGERP, desc_case).format(prep=prep, case=CASE_FOR_PREPNP[case])
    if ptype == PrepNumP:
        prep, case = phrase._prep._value, phrase._prep._case._value
        return make_inflected_string(PREPNUMP, desc_case).format(prep=prep, case=CASE_FOR_PREPNP[case])
    if ptype == PrepAdjP:
        prep, case = phrase._prep._value, phrase._prep._case._value
        return make_inflected_string(PREPADJP, desc_case).format(prep=prep, case=CASE_FOR_PREPNP[case])
    if ptype == PrepPPasP:
        prep, case = phrase._prep._value, phrase._prep._case._value
        return make_inflected_string(PREPPPASP, desc_case).format(prep=prep, case=CASE_FOR_PREPNP[case])
    if ptype == ComPrepNP:
        return make_inflected_string(COMPREPNP, desc_case).format(prep=phrase._prep)
    if ptype == InfP:
        aspect = phrase._aspect._value
        return make_inflected_string(INFP, desc_case).format(aspect=make_inflected_string(ASPECT[aspect], desc_case))
    if ptype == CP:
        # TODO realisations? – not stored in DB
        typ = phrase._type._value
        return make_inflected_string(CP_, desc_case).format(
            typ=make_inflected_string(CP_TYPE.get(typ, CP_CONJ), desc_case).format(conj=typ))
    if ptype == NCP:
        # TODO realisations? – not stored in DB
        case, typ = phrase._case._value, phrase._type._value
        to = TO[case]
        s = NCP_TYPE.get(typ, NCP_CONJ).format(to=to, conj=typ)
        return make_inflected_string(NCP_, desc_case) + s
    if ptype == PrepNCP:
        # TODO realisations? – not stored in DB
        prep, case, typ = phrase._prep._value, phrase._prep._case._value, phrase._type._value
        to = TO[case]
        s = PREPNCP_TYPE.get(typ, PREPNCP_CONJ).format(prep=prep, to=to, conj=typ)
        # we use NCP here as it’s the same (fraza zdaniowa wprowadzana przez...)
        return make_inflected_string(NCP_, desc_case) + s
    if ptype in (XP, AdvP):
        sem, realisations = phrase._category._value, phrase._category._limitations
        if ptype == AdvP and sem == 'misc':
            return make_inflected_string(ADVP_MISC, desc_case)
        if ptype == AdvP and sem == 'pron':
            return make_inflected_string(ADVP_PRON, desc_case)
        b = (bool(realisations) or inside_lex)
        desc = make_inflected_string(XP_[b] if ptype == XP else ADVP, desc_case)
        ret = '{desc} {sem}'.format(desc=desc, sem=make_inflected_string(XP_SEM[sem], desc_case))
        # don’t describe realisations for fixed/lexicalised phrases
        if realisations and not inside_lex:
            rs = make_ul(map(lambda r: phrase_description(r, function, negativity, 'nom', inside_lex=inside_lex), realisations))
            if len(realisations) == 1:
                ret += ' z dopuszczalną realizacją: ' + rs
            else:
                ret += ' z dopuszczalnymi realizacjami: ' + rs
        return ret
    # TODO !!!
    if ptype == Compar:
        prep = phrase._category._value
        return 'fraza porównawcza z przyimkiem <i>{prep}</i>'.format(prep=prep)
    if ptype == Nonch:
        return make_inflected_string(NONCH, desc_case)
    if ptype == OR:
        return make_inflected_string(OR_, desc_case)
    if ptype == Refl:
        return make_inflected_string(REFL, desc_case)
    if ptype == Recip:
        return make_inflected_string(RECIP, desc_case)
    if ptype == E:
        return make_inflected_string(E_, desc_case)
    if ptype == PossP:
        return make_inflected_string(POSSP, desc_case)
    if ptype == DistrP:
        return make_inflected_string(DISTRP, desc_case)
    if ptype == Fixed:
        assert (desc_case == 'nom')
        phrase, phraseo = phrase._phrase, phrase._text
        return 'frazeologizm w postaci {phrase} zamrożony w postaci <i>{phraseo}</i>'.format(
                phrase=make_phrase_description(phrase, function, negativity, 'gen', inside_lex=True),
                phraseo=phraseo.strip('\''))
    # TODO nie było w dokumentacji
    if ptype == Qub:
        return make_inflected_string(QUB, desc_case)
    print(ptype)
    1/0
    return 'fraza TODO'

def _lemma(lemma):
    # TODO see notes
    l = lemma.strip('\'')
    if l == 'bliźnięta':
        return 'bliźnię'
    return l

NUM_LEMMA = { '2' : 'dwa', '3' : 'trzy', '5' : 'pięć', }

def _num_lemma(lemma):
    return NUM_LEMMA.get(lemma, lemma)

def _pos(lemma, pos):
    if lemma == 'siebie':
        return 'siebie'
    if lemma in ('ja', 'ty', 'my', 'wy'):
        return 'ppron12'
    if lemma == 'on':
        return 'ppron3'
    if lemma == 'oba':
        return 'num'
    if lemma == 'jeden':
        return 'adj'
    return pos

def _num(lemma, num):
    if lemma == 'siebie':
        return ''
    if lemma in ('ja', 'ty') and num == '_':
        return 'sg'
    if lemma in ('oba', 'plecy', 'usta',):
        return 'pl'
    if lemma in ('pół', 'półtora'):
        return 'sg'
    # TODO (?)
    if num == 'agr':
        return 'sg'
    # TODO _ -> sg or _ -> sg and pl?
    return num if num != '_' else ['sg', 'pl']

def _gend(gend):
    if gend == 'agr':
        return 'm1'
    return gend

# TODO is the mapping for no function correct?
# TODO the mapping should be more complex, e.g. most lex(np)s should be in acc (dać kosza etc.),
# but adjps seem to need nom: chrzest bojowy
STR_CASE = {
    'subj' : { '_' : 'nom', 'aff' : 'nom', 'neg' : 'nom' },
    'obj'  : { '_' : 'acc', 'aff' : 'acc', 'neg' : 'gen' },
    None   : { '_' : 'acc', 'aff' : 'acc', 'neg' : 'gen' },
}
AGR_CASE = { 'subj' : 'nom', 'obj' : 'acc', None : 'nom' }
def _case(case, function, negativity='_'):
    if case == 'str':
        return STR_CASE[function][negativity]
    if case == 'agr':
        return AGR_CASE[function]
    # TODO both gen and acc?
    if case == 'part':
        return 'gen'
    # TODO other cases in case of control?
    if case == 'pred':
        return 'inst'
    if case == 'postp':
        return 'dat'
    return case

def _deg(deg):
    # positive degree = positive or no degree at all
    if deg == 'pos':
        return [deg, '']
    if deg == '_':
        return ['pos', 'com', 'sup', '']
    return deg

def _congr(lemma):
    if lemma in ('pół', 'półtora'):
        return 'rec'
    # heuristic: if both congr and rec forms available, prefer congr
    # no congr/rec also possible
    return ['congr', 'rec', '']

def _aff(aff):
    if aff == '_':
        return ['aff', 'neg']
    return aff

NEG = { '_' : '(nie) ', 'aff' : '', 'neg' : 'nie '}
def _neg(neg):
    return NEG[neg]

SIE = { '' : '', 'się' : 'się ', }
def _sie(sie):
    return SIE[sie]

def _feats(lemma, feats, praep=False):
    if lemma == 'on':
        return feats + ['m1', 'akc', 'praep' if praep else 'npraep']
    if lemma in ('ja', 'ty',):
        return feats + ['m1', ['akc', '']]
    if lemma in ('my', 'wy'):
        return feats + ['m1']
    if lemma == 'oba':
        return feats + ['congr', 'ncol']
    return feats
        
def _subst_attrs(lemma, tag):
    feats = tag.split(':')
    if lemma == 'siebie':
        return { 'case' : feats[1] }
    return {'num': feats[1], 'case': feats[2], 'gend' : feats[3]}

def get_gender_for_num(lemma):
    form = get_form(lemma, ['subst', 'sg', 'nom'])
    # 1 or 2 values: ['f'], ['n', 'ncol'], ...
    gend = form[1].split(':')[3:]
    if len(gend) == 2:
        # no col/ncol variant for jeden, wiele itp.
        gend[1] = [gend[1], '']
    else:
        # choose ncol for e.g. czterech/czworo m1
        gend = [gend[0], ['ncol', '']]
    return gend

def get_form(lemma, feats):
    if lemma.startswith('E('):
        return ('', 'subst:pl:nom:{}'.format(lemma.strip('E()')))
    lemma_feats = [f(lemma) if hasattr(f, '__call__') else f for f in feats]
    return select_form(lemma, lemma_feats)

def get_forms(lemma, feats):
    lemma_feats = [f(lemma) if hasattr(f, '__call__') else f for f in feats]
    lemma_feats = [[f] if type(f) == str else f for f in lemma_feats]
    ret = []
    for feats in product(*lemma_feats):
        try:
            ret.append(select_form(lemma, feats))
        except:
            pass
    if ret:
        return ret
    1/0

PRE, POST = 0, 1
def build_phrase(head, dep, head_type, dep_type):
    order = None
    if head_type == NP:
        if dep_type in (AdjP, LexAdjP, LexPPasP, LexPActP, PossP, LexQub, Fixed,):
            order = PRE
        # LexAdvP: nic więcej
        if dep_type in (NP, LexNP, PrepNP, ComPrepNP, LexPrepNP, LexPrepGerP, CP, LexCP, XP, LexXP, LexAdvP,):
            order = POST
    if head_type == NumP:
        if dep_type in (AdjP, LexAdjP, PossP):
            order = PRE
        # XP: w pół drogi ‹dokądś›
        # NP: na dwóch biegunach ‹kogoś/czegoś›
        if dep_type in (NP, XP,):
            order = POST
    if head_type == AdjP:
        if dep_type in (AdvP, LexAdvP, AdjP, LexAdjP, LexQub,):
            order = PRE
        # NP: pełny czegoś
        # Fixed: samo przez się
        if dep_type in (NP, LexNP, PrepNP, LexPrepNP, XP, LexXP, Compar, LexCompar, Fixed):
            order = POST
    if head_type == AdvP:
        if dep_type in (XP, AdvP,):
            order = PRE
        # LexNP: dalej własnego nosa
        # LexPrepNP: prosto w oczy
        # LexCP: tak, że...
        if dep_type in (LexCompar, NP, LexNP, LexPrepNP, LexCP,):
            order = POST
    if head_type == InfP:
        order = POST
    if head_type == Qub:
        if dep_type in (LexQub,):
            return PRE
    if order == PRE:
        return '{} {}'.format(dep, head)
    if order == POST:
        return '{} {}'.format(head, dep)
    else:
        print(head, dep, head_type, dep_type)
        1/0

def combine(phrase, texts):
    assert (len(texts) == len(phrase._words._lemmas))
    if len(texts) == 1:
        return texts[0]
    if phrase._words._selection == 'xor':
        return list(chain.from_iterable(texts))
    else:
        joiner = ' ' if  phrase._words._coocur == 'concat' else ' i/lub '
        return list(chain.from_iterable(map(joiner.join, powerset_nonempty(x)) for x in product(*texts)))

def make_phraseologisms(phrase, function, negativity, attrs={}):
    ptype = type(phrase)
    if ptype in (NP, PrepNP, ComPrepNP):
        # “any ((com)prep)np”
        if ptype == NP:
            case = phrase._case._value
        else:
            case = phrase._prep._case._value if ptype == PrepNP else 'gen'
        CASE = _case(case, function, negativity)
        prep = (phrase._prep._value + ' ') if ptype != NP else ''
        feats = ['subst', CASE]
        forms = [get_form(lemma, feats) for lemma in ('ktoś', 'coś')]
        return ['{}‹{}›'.format(prep, '/'.join(o for o, t in forms))]
    '''
    if ptype in (PrepNP, ComPrepNP):
        # “any (com)prepnp”
        prep = phrase._prep._value
        case = phrase._prep._case._value if ptype == PrepNP else 'gen'
        feats = ['subst', case]
        forms = [get_form(lemma, feats) for lemma in ('ktoś', 'coś')]
        return ['{}{}'.format(prep, '/'.join(o for o, t in forms))]
    '''
    # TODO merge LexNP with LexPrepNP/LexPrepGerP?
    if ptype == LexNP:
        case = phrase._np._case._value
        POS = lambda lemma: _pos(lemma, 'subst')
        NUM = lambda lemma: _num(lemma, phrase._number)
        CASE = _case(case, function, negativity)
        feats = [POS, NUM, CASE]
        nps = []
        for lemma in phrase._words._lemmas:
            lemma = _lemma(lemma)
            for orth, tag in get_forms(lemma, _feats(lemma, feats)):
                mod_attrs = _subst_attrs(lemma, tag)
                nps += make_modified_phrases(phrase, orth, NP, function, negativity, mod_attrs)
        return nps
    if ptype in (LexPrepNP, LexPrepGerP):
        phrase2 = phrase._prepnp if ptype == LexPrepNP else phrase._prepgerp
        prep, case = phrase2._prep._value, phrase2._prep._case._value
        POS = lambda lemma: _pos(lemma, 'subst') if ptype == LexPrepNP else 'ger'
        NUM = lambda lemma: _num(lemma, phrase._number)
        CASE = _case(case, function)
        feats = [POS, NUM, CASE]
        if ptype == LexPrepGerP:
            feats += ['aff']
        nps = []
        for lemma in phrase._words._lemmas:
            lemma = _lemma(lemma)
            for orth, tag in get_forms(lemma, _feats(lemma, feats, praep=True)):
                mod_attrs = _subst_attrs(lemma, tag)
                nps += make_modified_phrases(phrase, orth, NP, function, negativity, mod_attrs)
        return ['{} {}'.format(prep, np) for np in nps]
    if ptype in (LexNumP, LexPrepNumP):
        case = phrase._nump._case._value if ptype == LexNumP else phrase._prepnump._prep._case._value
        prep = (phrase._prepnump._prep._value + ' ') if ptype != LexNumP else ''
        CASE = _case(case, function)
        phrs = []
        words = []
        for word in phrase._words._lemmas:
            if word.startswith('E('):
                if word == 'E(_)':
                    word = 'E(f.m1.m2.m3.n)'
                words += ['E({})'.format(gend) for gend in word.strip('E()').split('.')]
            else:
                words.append(word)
        for num in phrase._nums._lemmas:
            POS = _pos(num, 'num')
            for word in words:
                # wiele wody
                NUM = _num(num, 'pl') if word != 'woda' else 'sg'
                gend = get_gender_for_num(word)
                # gender before congr/rec to avoid empty result due to filtering priority
                feats = [POS, NUM, CASE] + gend + [_congr(num)]
                num_form = get_form(_num_lemma(num), feats)
                congr = num_form[1].split(':')[4]
                word_case = CASE if congr == 'congr' else 'gen'
                word_form = get_form(word, ['subst', NUM, word_case])
                # back to digits if this is the case
                num_form = (num, num_form[1]) if num in NUM_LEMMA else num_form
                phr = '{} {}'.format(num_form[0], word_form[0]) if word_form[0] else num_form[0]
                mod_attrs = { 'num': NUM, 'case': CASE, 'gend' : gend }
                phrs += make_modified_phrases(phrase, phr, NumP, function, negativity, mod_attrs)
        return ['{}{}'.format(prep, phr) for phr in phrs]
    if ptype == AdjP:
        feats = ['adj', attrs['num'], attrs['case'], attrs['gend'], 'pos']
        return ['‹{}›'.format(get_form('jakiś', feats)[0])]
    if ptype in (LexAdjP, LexPPasP, LexPActP):
        if ptype == LexAdjP:
            phrase2 = phrase._adjp
            POS = lambda lemma: _pos(lemma, 'adj')
        elif ptype == LexPPasP:
            phrase2 = phrase._ppasp
            POS = 'ppas'
        else:
            phrase2 = phrase._pactp
            POS = 'pact'
        if phrase._number == 'agr' and 'num' in attrs:
            num = attrs['num']
            assert (num != 'agr')
            NUM = lambda lemma: _num(lemma, num)
        else:
            NUM = lambda lemma: _num(lemma, phrase._number)
        if phrase2._case._value == 'agr' and 'case' in attrs:
            case = attrs['case']
            assert (case != 'agr')
            CASE = _case(case, function)
        else:
            CASE = _case(phrase2._case._value, function)
        if phrase._gender == 'agr' and 'gend' in attrs:
            gend = attrs['gend']
            assert (gend != 'agr')
            GEND = gend
        else:
            GEND = _gend(phrase._gender)
        DEG = _deg(phrase._degree) if ptype == LexAdjP else ''
        feats = [POS, NUM, CASE, GEND, DEG]
        if ptype != LexAdjP:
            feats.append('aff')
        adjps = []
        for lemma in phrase._words._lemmas:
            lemma_adjps = []
            for orth, tag in get_forms(lemma, _feats(lemma, feats)):
                mod_attrs = _subst_attrs(lemma, tag)
                lemma_adjps += make_modified_phrases(phrase, orth, AdjP, function, negativity, mod_attrs)
            adjps.append(lemma_adjps)
        return combine(phrase, adjps)
    if ptype in (LexPrepAdjP, LexPrepPPasP):
        phrase2 = phrase._prepadjp if ptype == LexPrepAdjP else phrase._prepppasp
        prep, case = phrase2._prep._value, phrase2._prep._case._value
        NUM = lambda lemma: _num(lemma, phrase._number)
        GEND = _gend(phrase._gender)
        CASE = _case(case, function)
        DEG =  _deg(phrase._degree) if ptype == LexPrepAdjP else ''
        feats_adjp = ['adjp', CASE]
        feats_adj = ['adj', NUM, CASE, GEND, DEG] if ptype == LexPrepAdjP else ['ppas', NUM, CASE, GEND, DEG, _aff(phrase._negativity)]
        adjps = []
        for lemma in phrase._words._lemmas:
            if case == 'postp':
                assert (ptype == LexPrepAdjP)
                try:
                    orth, tag = get_form(lemma, feats_adjp)
                    mod_attrs = {}
                except:
                    orth, tag = get_form(lemma, feats_adj)
                    mod_attrs = _subst_attrs(lemma, tag)
                adjps = make_modified_phrases(phrase, orth, AdjP, function, negativity, mod_attrs)
            else:
                for orth, tag in get_forms(lemma, feats_adj):
                    mod_attrs = _subst_attrs(lemma, tag)
                    adjps += make_modified_phrases(phrase, orth, AdjP, function, negativity, mod_attrs)
        return ['{} {}'.format(prep, adjp) for adjp in adjps]
    if ptype == LexInfP:
        aspect = phrase._infp._aspect._value
        neg = _neg(phrase._negativity)
        sie = _sie(phrase._inherent_sie)
        POS = 'inf'
        feats = [POS]
        infps = []
        for lemma in phrase._words._lemmas:
            for orth, tag in get_forms(lemma, feats):
                head = '{}{}{}'.format(neg, orth, sie)
                infps += make_modified_phrases(phrase, head, InfP, function, negativity, {})
        return infps
    # TODO realisations? – not stored in DB
    if ptype in (CP, NCP, PrepNCP):
        typ = phrase._type._value
        to = TO[phrase._case._value] if ptype == NCP else ''
        prep = '{} '.format(phrase._prep._value) if ptype == PrepNCP else ''
        text = None
        if typ == 'żeby2':
            text = ', że…' if negativity != 'neg' else ', że/żeby…'
        if typ == 'gdy':
            text = ', gdy…'
        if typ == 'int':
            text = ', kto/co/dlaczego/kiedy/…'
        if typ == 'rel':
            ktory = get_form('który', ['adj', attrs['num'], 'nom', attrs['gend'], 'pos'])[0]
            text = ', {}/co…'.format(ktory)
        if typ in ('jak', 'kiedy', 'że', 'żeby',):
            text = ', {}…'.format(typ)
        if text is not None:
            return [prep + to + text]
        print('===========', typ)
        1/0
    # TODO order, mnie -> mi itp.,...
    if ptype in (LexCP, LexNCP):
        typ = phrase._cp._type._value if ptype == LexCP else phrase._ncp._type._value
        to = '' if ptype == LexCP else '{}, '.format(TO[phrase._ncp._case._value])
        comp = ''
        if typ == 'żeby2':
            comp = 'żeby '
        elif typ == 'gdy':
            comp = 'gdy '
        elif typ not in ('int', 'rel',):
            comp = typ + ' '
        neg = _neg(phrase._negativity)
        sie = _sie(phrase._inherent_sie)
        subj = None
        # dependent like „co”, „na kogo” – should go first
        first = []
        rest = []
        print()
        for position in phrase._modification._dependents:
            print('---')
            assert(len(position._phrases) == 1)
            dep_phrase = position._phrases[0]
            func = position._function._value if position._function else None
            dep_phr = (dep_phrase, make_phraseologisms(dep_phrase, func, phrase._negativity, {}))
            if func == 'subj':
                subj = dep_phr
            words = None
            if type(dep_phrase) == LexNumP:
                words = dep_phrase._nums._lemmas
            elif type(dep_phrase) == LexXP:
                words = dep_phrase._lex._words._lemmas
            elif hasattr(dep_phrase, '_words'):
                words = dep_phrase._words._lemmas
            print(words)
            if words and {'co', 'gdzie', 'ile', 'jak', 'skąd',}.intersection(words):
                first.append(dep_phr)
            elif func != 'subj':
                rest.append(dep_phr)
        print()
        print('--- FIRST:', list(map(str, first)))
        print('--- SUBJ:', subj)
        print('--- REST:', list(map(str, rest)))
        print(typ)
        assert (len(first) == 1 or typ not in ('int',))
        print()
        deps1 = [d[1] for d in first]
        if subj and subj not in first + rest:
            deps1.append(subj[1])
        deps2 = [d[1] for d in rest]
        # TODO: always ter? sg if no subj?
        # TODO separate numbers for subject realisations
        subj_num = 'sg'
        if subj and hasattr(subj[0], '_number'):
            subj_num = _num('', subj[0]._number)
        feats = ['fin', subj_num, 'ter']
        phrs = []
        for lemma in phrase._words._lemmas:
            for dps1 in product(*deps1):
                for dps2 in product(*deps2):
                    verb_form = get_form(lemma, feats)[0]
                    phrs.append('{}{}{} {}{}{} {}'.format(to, comp, ' '.join(dps1), sie, neg, verb_form, ' '.join(dps2)))
        return phrs
    if ptype in (XP, AdvP):
        # TODO realisations?
        return [XP_SEM_PHRASEO[phrase._category._value]]
    if ptype == LexXP:
        return make_phraseologisms(phrase._lex, function, negativity, {})
    if ptype == LexAdvP:
        POS = 'adv'
        DEG =  _deg(phrase._degree)
        feats = [POS, DEG]
        advps = []
        for lemma in phrase._words._lemmas:
            for orth, tag in get_forms(lemma, feats):
                advps += make_modified_phrases(phrase, orth, AdvP, function, negativity, {})
        return advps
    # TODO something more?
    if ptype == Compar:
        return ['{}…'.format(phrase._category._value)]
    if ptype == LexCompar:
        comp = phrase._compar._category._value
        lex_phrs = []
        for lex in phrase._lexes:
            # TODO case (determined by function?) will depend on the control
            phrs = make_phraseologisms(lex, 'subj', negativity, {})
            lex_phrs.append(phrs)
        print(lex_phrs)
        return ['{} {}'.format(comp, ' '.join(phrs)) for phrs in product(*lex_phrs)]
    if ptype == PossP:
        feats = ['adj', attrs['num'], attrs['case'], attrs['gend'], 'pos']
        forms = [get_form(lemma, feats) for lemma in ('mój', 'pański')]
        return ['/'.join(o for o, t in forms) + '/Piotra/…']
    #if ptype == OR:
    if ptype == Fixed:
        return [phrase._text]
    if ptype == LexQub:
        qubs = []
        for orth in phrase._words._lemmas:
            print(orth)
            qubs += make_modified_phrases(phrase, orth, Qub, function, negativity, {})
        return qubs
    print(ptype)
    1/0
    return ['TODO']

def make_modified_phrases(phrase, head, head_type, function, negativity, mod_attrs):
    texts = []
    if phrase._modification is not None and phrase._modification._atr != 'natr':
        for mod_list in make_modifications(phrase._modification, function, negativity, mod_attrs):
            if phrase._modification._atr == 'ratr1':
                for mod_ptype, mod in mod_list:
                    texts.append(build_phrase(head, mod, head_type, mod_ptype))
            elif phrase._modification._atr == 'atr1':
                for mod_ptype, mod in mod_list:
                    texts.append(build_phrase(head, '({})'.format(mod), head_type, mod_ptype))
            elif phrase._modification._atr == 'ratr':
                for mod_list2 in powerset_nonempty(mod_list):
                    p = head
                    for mod_ptype, mod in mod_list2:
                        p = build_phrase(p, '{}'.format(mod), head_type, mod_ptype)
                    texts.append(p)
            elif phrase._modification._atr == 'atr':
                p = head
                for mod_ptype, mod in mod_list:
                    p = build_phrase(p, '({})'.format(mod), head_type, mod_ptype)
                texts.append(p)
            else:
                print(phrase._modification._atr)
                1/0
    else:
        texts.append(head)
    return uniq_list(texts)

# TODO is the ‘first’ heuristic for choosing phrase type enough?
def make_coordinations(mods):
    ptype = mods[0][0]
    ret = [(ptype, ' i/lub '.join(text for _, text in x)) for x in powerset_nonempty(mods)]
    return ret

def make_modifications(modification, function, negativity, attrs):
    mods = []
    for position in modification._dependents:
        #print(' ->')
        position_mods = []
        for p in position._phrases:
            mod = make_phraseologisms(p, function, negativity, attrs=attrs)
            # pass the mod phrase’s type for determining text order
            position_mods.append([(type(p), m) for m in mod])
            #print('      ', p, '->', mod)
        if len(position_mods) > 1:
            mods.append(chain.from_iterable(make_coordinations(mds) for mds in product(*position_mods)))
        else:
            mods.append(position_mods[0])
    return list(product(*mods))

def make_ul(items):
    return '<ul>{}</ul>'.format(''.join(map('<li>{}</li>'.format, items)))