1

クロスワード ソルバーの一部である次の関数があります。

def CrosswordPossibleWords(p_words, p_cw_words):
    """For each word found in the crossword, find the possible words and keep track of the one with the minimum possible words.

    Keyword arguments:
    p_words    -- The dictionary words.
    p_cw_words -- The crossword word attributes.
    """
    l_min = 999999999
    l_min_index = -1
    l_index = 0
    l_choices = []
    for l_cw_word in p_cw_words:
        if l_cw_word[2] >= l_min_length and '-' in l_cw_word[4]:
            pattern = re.compile('^' + l_cw_word[4].replace('.', '%').replace('-', '.').upper() + '$', re.UNICODE)
            l_choice = []
            for l_word in [w for w in p_words if len(w) == len(l_cw_word[4])]:
                if re.match(pattern, l_word):
                    l_choice.append(l_word)
            l_choices.append(l_choice)
            if len(l_choice) < l_min:
                l_min_index = l_index
                l_min = len(l_choice)
        else:
            l_choices.append([])
        l_index = l_index + 1
    return (l_choices, l_min_index)

クロスワードの単語は次の形式です。

[row, col, length, direction, word]

'.'その単語を解決できない場合は単語に、'-'その文字を知らない場合はaがあります。

このコードを高速化するにはどうすればよいですか? 現在、実行に約 2.5 秒かかります。numpy 文字列を使用することを考えていました。どうやらnumpyは10倍高速ですが、numpyについては何も知らず、現在のすべての文字列関数を使用できるかどうかもわかりません。

何か案は?

4

2 に答える 2

1

この関数を呼び出す前に単語の長さで辞書を分割できるため、呼び出しごとに再実行する必要はありません。

于 2013-02-26T21:54:26.130 に答える
1

私は Scott Hunter に同意しますが、リストが dict に置き換えられた次のようなものを探している可能性があります。

def CrosswordPossibleWords(p_words, p_cw_words):
    """For each word found in the crossword, find the possible words and keep track of the one with the minimum possible words.

    Keyword arguments:
    p_words    -- The dictionary words.
    p_cw_words -- The crossword word attributes.
    """
    l_min = 999999999
    l_min_index = -1
    l_index = 0
    l_choices = {}    # using dict instead of list
    for l_cw_word in p_cw_words:
        if l_cw_word[2] >= l_min_length and '-' in l_cw_word[4]:
            pattern = re.compile('^' + l_cw_word[4].replace('.', '%').replace('-', '.').upper() + '$', re.UNICODE)
                l_choice = {}  # using dict instead of list

            for l_word in [w for w in p_words if len(w) == len(l_cw_word[4])]:
                if re.match(pattern, l_word):

                    l_choice[l_word]=None

            l_choices[l_choice]=None

            if len(l_choice) < l_min:  ##
                l_min_index = l_index  ## Get rid of this.
                l_min = len(l_choice)  ##
        else:
            l_choices.append([])    # why append empty list?
        l_index = l_index + 1
        l_choices=list(l_choices.keys())   # ...you probably need the list again...
    return (l_choices, l_min_index)
于 2013-02-26T22:03:39.517 に答える