def generate_rhythms():
    while 1:
        user_input = input('◼︎ 请输入一个你想押韵的词(按回车退出): ')
        if user_input:
            word = prune_word(user_input)
            if word:
                num_char = len(word)

                cs = input(
                    '\t-是否押声母?例如对于"欢喜",押"还席"不押"惯技"。\n\t 请输入要押声母的字的位置\n\t (0-不押;1-押"欢";2-押"喜";12-押"欢喜"): '
                )
                vs = input(
                    '\t-是否押全韵母?例如对于"欢喜",押"端倪"不押"叹息"。\n\t 请输入要押全韵母的字的位置\n\t (0-不押;1-押"欢";2-押"喜";12-押"欢喜"): '
                )
                c_ids = check_positions(cs, num_char)
                v_ids = check_positions(vs, num_char)

                pinyins = word_parser(word)
                candidates = get_candidates(word, pinyins, num_char, c_ids,
                                            v_ids)
                if candidates:
                    display_results(candidates)
                else:
                    print(
                        '>>> 太可惜了,没有适合押韵的词!请尝试分解押韵,例如将"光明磊落"分为"光明"和"磊落"分别进行查询。'
                    )
        else:
            break
def single_rhyme(target_word, pinyins, candidates, c_ids, v_ids):
    print('单押检索较慢,请稍等...')
    target_vowel = pinyins[0][1][-1]
    for k, v in phrase_dict.items():
        if k[-1] == target_vowel:
            for word in v:
                word_pys = word_parser(word)
                if word[-1] != target_word \
                        and match_cv(word_pys[-1:], pinyins, c_ids, v_ids) \
                        and word not in candidates[len(word)]:
                    candidates[len(word)].append(word)
    return candidates
def single_rhyme(target_word, pinyins, candidates, c_ids, v_ids):
    print('单押检索较慢,请稍等...')
    target_vowel = pinyins[0][1][-1]
    for k, v in phrase_dict.items():
        if k[-1] == target_vowel:
            for word in v:
                word_pys = word_parser(word)
                if word[-1] != target_word \
                        and match_cv(word_pys[-1:], pinyins, c_ids, v_ids) \
                        and word not in candidates[len(word)]:
                    candidates[len(word)].append(word)
    return candidates
def multi_rhyme(target_word, pinyins, num_char, candidates, c_ids, v_ids):
    hash_vowels = tuple([pinyin[1][-1] for pinyin in pinyins])

    try:
        basic_candidates = phrase_dict[hash_vowels]
    except KeyError:
        return candidates

    parsed_candidates = [(word, word_parser(word)) for word in basic_candidates]

    for word, word_pinyins in parsed_candidates:
        if word[-num_char:] != target_word \
                and match_cv(word_pinyins[-num_char:], pinyins, c_ids, v_ids):
            candidates[len(word)].append(word)

    return candidates
def multi_rhyme(target_word, pinyins, num_char, candidates, c_ids, v_ids):
    hash_vowels = tuple([pinyin[1][-1] for pinyin in pinyins])

    try:
        basic_candidates = phrase_dict[hash_vowels]
    except KeyError:
        return candidates

    parsed_candidates = [(word, word_parser(word))
                         for word in basic_candidates]

    for word, word_pinyins in parsed_candidates:
        if word[-num_char:] != target_word \
                and match_cv(word_pinyins[-num_char:], pinyins, c_ids, v_ids):
            candidates[len(word)].append(word)

    return candidates
def generate_rhythms():
    while 1:
        user_input = input('◼︎ 请输入一个你想押韵的词(按回车退出): ')
        if user_input:
            word = prune_word(user_input)
            if word:
                num_char = len(word)

                cs = input('\t-是否押声母?例如对于"欢喜",押"还席"不押"惯技"。\n\t 请输入要押声母的字的位置\n\t (0-不押;1-押"欢";2-押"喜";12-押"欢喜"): ')
                vs = input('\t-是否押全韵母?例如对于"欢喜",押"端倪"不押"叹息"。\n\t 请输入要押全韵母的字的位置\n\t (0-不押;1-押"欢";2-押"喜";12-押"欢喜"): ')
                c_ids = check_positions(cs, num_char)
                v_ids = check_positions(vs, num_char)

                pinyins = word_parser(word)
                candidates = get_candidates(word, pinyins, num_char, c_ids, v_ids)
                if candidates:
                    display_results(candidates)
                else:
                    print('>>> 太可惜了,没有适合押韵的词!请尝试分解押韵,例如将"光明磊落"分为"光明"和"磊落"分别进行查询。')
        else:
            break
Esempio n. 7
0
        if len(word) > 1 and re.match(r'^[\u4E00-\u9FA5,]+$', word):
            vocab.add(word)

print('+ 各大输入法词库 (无俗语,含英语):', len(vocab))


for words in phrases_dict:
    for word in words.split(','):
        vocab.add(word.strip())

print('+ 自带词库 (无俗语):', len(vocab))


look_up = defaultdict(list)
for word in vocab:
    pinyins = word_parser(word)
    if len(word) > 1:
        look_up[tuple([pinyin[1][-1] for pinyin in pinyins[-2:]])].append(word)
    if len(word) > 2:
        look_up[tuple([pinyin[1][-1] for pinyin in pinyins[-3:]])].append(word)
    if len(word) > 3 and word[-4] != ',':
        look_up[tuple([pinyin[1][-1] for pinyin in pinyins[-4:]])].append(word)

with open('phrase_dict.txt', 'w') as f:
    for k, v in look_up.items():
        f.write('{}\t{}\n'.format(' '.join(list(k)), ' '.join(sorted(v))))
    print('done!')


# with open('phrase_dict.py', 'w') as f:
#     f.write('phrase_dict = {\n')
Esempio n. 8
0
    for line in f:
        word = line.strip().split()[0]
        if len(word) > 1 and re.match(r'^[\u4E00-\u9FA5,]+$', word):
            vocab.add(word)

print('+ 各大输入法词库 (无俗语,含英语):', len(vocab))

for words in phrases_dict:
    for word in words.split(','):
        vocab.add(word.strip())

print('+ 自带词库 (无俗语):', len(vocab))

look_up = defaultdict(list)
for word in vocab:
    pinyins = word_parser(word)
    if len(word) > 1:
        look_up[tuple([pinyin[1][-1] for pinyin in pinyins[-2:]])].append(word)
    if len(word) > 2:
        look_up[tuple([pinyin[1][-1] for pinyin in pinyins[-3:]])].append(word)
    if len(word) > 3 and word[-4] != ',':
        look_up[tuple([pinyin[1][-1] for pinyin in pinyins[-4:]])].append(word)

with open('phrase_dict.txt', 'w', encoding="utf-8") as f:
    for k, v in look_up.items():
        f.write('{}\t{}\n'.format(' '.join(list(k)), ' '.join(sorted(v))))
    print('done!')

# with open('phrase_dict.py', 'w') as f:
#     f.write('phrase_dict = {\n')
#     for k, v in look_up.items():