Beispiel #1
0
def synonym_replacement(words, n, key_words):
    """
      同义词替换,替换一个语句中的n个单词为其同义词
    :param words: list, inupt sentence
    :param n: int, replace words
    :return: list, new_words
    """
    new_words = words.copy()
    random_word_list = list(
        set([word for word in words if word not in stop_words]))
    random.shuffle(random_word_list)
    num_replaced = 0
    for random_word in random_word_list:
        sim_synonyms = get_syn_by_synonyms(random_word)
        if len(
                sim_synonyms
        ) >= 1 and random_word not in key_words and not is_total_english(
                random_word) and not is_total_number(random_word):
            synonym = random.choice(sim_synonyms)
            new_words = [
                synonym if word == random_word else word for word in new_words
            ]
            num_replaced += 1
        if num_replaced >= n:
            break
    sentence = ' '.join(new_words)
    new_words = sentence.split(' ')
    return new_words
Beispiel #2
0
def add_word(new_words, key_words):
    """
      在list上随机插入一个同义词
    :param words: list, inupt sentence
    :return: list, new_words
    """
    synonyms = []
    counter = 0
    while len(synonyms) < 1:
        random_word = new_words[random.randint(0, len(new_words) - 1)]
        # 过滤
        if random_word not in key_words and not is_total_english(random_word) and not is_total_number(random_word):
            synonyms = get_syn_by_synonyms(random_word)
            counter += 1
        if counter >= 10:
            return
    random_synonym = random.choice(synonyms)
    random_idx = random.randint(0, len(new_words) - 1)
    new_words.insert(random_idx, random_synonym)
Beispiel #3
0
def get_syn_by_synonyms(word):
    if not is_total_english(word.strip()):
        return synonyms.nearby(word)[0]
    else:
        return word