示例#1
0
文件: info.py 项目: jart/poemy
def mkline(meter, rhythm, rhyme, **opts):
    opts.setdefault('tries', 100)
    opts.setdefault('originality', 1)
    opts.setdefault('bigwords', 0.9)
    opts.setdefault('feminine', False)
    opts.setdefault('visited', set())
    words = firstwords()
    for n in xrange(opts['tries']):
        w1, w2 = words[pick(words, opts)]
        if (w1, w2) in opts['visited']:
            continue
        opts['visited'].add((w1, w2))
        if w1 not in poemy.db.sounds or w2 not in poemy.db.sounds:
            continue
        if w1 in poemy.badstartwords:
            continue
        wcm = poemy.wordcompatmeter(meter, w1, w2)
        if wcm is None:
            continue
        if poemy.wordcompatrhythm(rhythm, w1, w2) is None:
            continue
        try:
            return [w1, w2] + mkword(
                w1, w2, meter[len(wcm):], rhythm[len(wcm):], rhyme, opts)
        except Exhausted:
            pass
    raise Exhausted()
示例#2
0
def mkline(meter, rhythm, rhyme, **opts):
    opts.setdefault('tries', 100)
    opts.setdefault('originality', 1)
    opts.setdefault('bigwords', 0.9)
    opts.setdefault('feminine', False)
    opts.setdefault('visited', set())
    words = firstwords()
    for n in xrange(opts['tries']):
        w1, w2 = words[pick(words, opts)]
        if (w1, w2) in opts['visited']:
            continue
        opts['visited'].add((w1, w2))
        if w1 not in poemy.db.sounds or w2 not in poemy.db.sounds:
            continue
        if w1 in poemy.badstartwords:
            continue
        wcm = poemy.wordcompatmeter(meter, w1, w2)
        if wcm is None:
            continue
        if poemy.wordcompatrhythm(rhythm, w1, w2) is None:
            continue
        try:
            return [w1, w2] + mkword(w1, w2, meter[len(wcm):],
                                     rhythm[len(wcm):], rhyme, opts)
        except Exhausted:
            pass
    raise Exhausted()
示例#3
0
文件: info.py 项目: jart/poemy
def mkword(w1, w2, meter, rhythm, rhyme, opts):
    if not meter:
        if w2 in poemy.badendwords:
            raise Exhausted()
        if rhyme:
            if w2 == rhyme:
                raise Exhausted()
            rf = poemy.is_frhyme if opts['feminine'] else poemy.is_rhyme
            if not rf(w2, rhyme):
                raise Exhausted()
        return []
    words = poemy.db.chain.get((w1, w2), [])[:]
    if len(words) < opts['originality']:
        raise Exhausted()
    for n in range(opts['tries']):
        if not words:
            break
        w3 = words.pop(pick(words, opts))
        if (w2, w3) in opts['visited']:
            continue
        opts['visited'].add((w2, w3))
        if w3 not in poemy.db.sounds:
            continue
        wcm = poemy.wordcompatmeter(meter, w3)
        if wcm is None:
            continue
        if poemy.wordcompatrhythm(rhythm, w3) is None:
            continue
        try:
            return [w3] + mkword(
                w2, w3, meter[len(wcm):], rhythm[len(wcm):], rhyme, opts)
        except Exhausted:
            pass
    raise Exhausted()
示例#4
0
def mkword(w1, w2, meter, rhythm, rhyme, opts):
    if not meter:
        if w2 in poemy.badendwords:
            raise Exhausted()
        if rhyme:
            if w2 == rhyme:
                raise Exhausted()
            rf = poemy.is_frhyme if opts['feminine'] else poemy.is_rhyme
            if not rf(w2, rhyme):
                raise Exhausted()
        return []
    words = poemy.db.chain.get((w1, w2), [])[:]
    if len(words) < opts['originality']:
        raise Exhausted()
    for n in range(opts['tries']):
        if not words:
            break
        w3 = words.pop(pick(words, opts))
        if (w2, w3) in opts['visited']:
            continue
        opts['visited'].add((w2, w3))
        if w3 not in poemy.db.sounds:
            continue
        wcm = poemy.wordcompatmeter(meter, w3)
        if wcm is None:
            continue
        if poemy.wordcompatrhythm(rhythm, w3) is None:
            continue
        try:
            return [w3] + mkword(w2, w3, meter[len(wcm):], rhythm[len(wcm):],
                                 rhyme, opts)
        except Exhausted:
            pass
    raise Exhausted()