예제 #1
0
def find_best_acronym(prev_expression):
    current_expression = prev_expression
    while True:
        current_acronym = acronym_from_expression(current_expression)
        # print(current_acronym, current_expression)
        current_score = Acronym.score_seq(current_acronym, current_expression,
                                          word_order_variable)
        prev_score = current_score

        neighbouring_acros = get_neighbouring_acronyms(current_expression)
        for expr in neighbouring_acros:
            acro = acronym_from_expression(expr)
            score = Acronym.score_seq(acro, expr, word_order_variable)
            if score > current_score:
                current_expression = expr
                current_acronym = acro
                current_score = score
        # if no progress, return
        if prev_score == current_score:
            return current_expression
예제 #2
0
def stochastic_beam_search_find_deep_neighbours(exp,
                                                depth,
                                                beta=20,
                                                p=0.9):  # beta = 20, p = 0.9
    # beta nb of exp that are expanded every step
    best_acros = {exp}
    for i in range(depth):
        # generate new candidates from best_acros
        all_neighbours = set()
        for e in best_acros:
            for n in get_neighbouring_acronyms(e):
                all_neighbours.add(n)
        #in last step: return all neighbours instead of 20 'best'
        if i == depth - 1:  # last iteration
            return all_neighbours

        # select stochastic new best acros from candidates
        # pairwise tournament selection: select best with prob p

        new_best_acros = set()
        while len(new_best_acros) < beta and len(
                all_neighbours) > 2:  # todo or convergence
            a = all_neighbours.pop()
            b = all_neighbours.pop()
            if Acronym.score_seq(acronym_from_expression(a), a,
                                 word_order_variable) < Acronym.score_seq(
                                     acronym_from_expression(b), b,
                                     word_order_variable):
                a, b = b, a
            # a is now best acronym
            if random.random() < p:
                # add best with prob p
                new_best_acros.add(a)
                all_neighbours.add(b)
            else:
                new_best_acros.add(b)
                all_neighbours.add(a)

        best_acros = new_best_acros.copy()
    return best_acros
예제 #3
0
def synonym_swap(previous_expression):
    expressions = []
    expression_list = previous_expression.split()
    for i in range(len(expression_list)):
        word = expression_list[i]
        nb_capitals = get_nb_capital_letters(word)
        for syn in Acronym.get_synonyms(word):
            if len(syn) >= nb_capitals:
                new_word = syn[:nb_capitals].upper() + syn[nb_capitals:].lower(
                )
                # print('new word: ' + new_word)
                new_expression_list = expression_list[:]
                new_expression_list[i] = new_word
                expressions.append(' '.join(
                    new_expression_list))  # add new expression to expressions
    return expressions
예제 #4
0
def beam_search_find_deep_neighbours(exp, depth, beta=20):
    # beta nb of exp that are expanded every step
    best_acros = {exp}
    for i in range(depth):
        # generate new candidates from best_acros
        all_neighbours = []
        for e in best_acros:
            for n in get_neighbouring_acronyms(e):
                all_neighbours.append(n)
        # select stochastic new best acros from candidates
        # pairwise tournament selection: select best with prob p
        sorted(all_neighbours,
               key=lambda e: Acronym.score_seq(acronym_from_expression(e), e,
                                               word_order_variable),
               reverse=True)

        best_acros = set(all_neighbours[:beta])
    return best_acros
예제 #5
0
import Acronym

word = "large"

for s in Acronym.get_synonyms(word):
    print(s)
예제 #6
0
def pprint_acros(exps):
    for exp in exps:
        acro = acronym_from_expression(exp)
        print(
            acro, ": ", exp, ':',
            "{:.4f}".format(Acronym.score_seq(acro, exp, word_order_variable)))
예제 #7
0
def sort_acros(exps):
    return sorted(exps,
                  key=lambda e: Acronym.score_seq(acronym_from_expression(e),
                                                  e, word_order_variable),
                  reverse=True)