Nltk: find_concordance () retorna lista vazia para left_context

Criado em 20 ago. 2018  ·  4Comentários  ·  Fonte: nltk/nltk

if offsets:
            for i in offsets:
                query_word = self._tokens[i]
                # Find the context of query word.
                left_context = self._tokens[i-context:i]

Quando a primeira ocorrência do termo de pesquisa está no início do texto (por exemplo, no deslocamento 7), suponha que o parâmetro de largura seja definido como 20, então [i- contexto: i ] seria avaliado como [-13: 7] .
Nesse caso, se o texto consistir em mais de 20 palavras, a variável left_context seria uma lista vazia, ao invés de uma lista contendo as 7 primeiras palavras do texto.

Uma solução simples faria:

if offsets:
    for i in offsets:
        query_word = self._tokens[i]
        # Find the context of query word.
        if i - context < 0:
            left_context = self._tokens[:i]
        else:
            left_context = self._tokens[i-context:i]
bug corpus goodfirstbug

Todos 4 comentários

Você poderia fornecer uma amostra de entrada e saída desejada para que possamos adicionar ao teste de regressão?

Entrada:

jane_eyre = 'Chapter 1\nTHERE was no possibility of taking a walk that day. We had been wandering, indeed, in the leafless shrubbery an hour in the morning; but since dinner (Mrs. Reed, when there was no company, dined early) the cold winter wind had brought with it clouds so sombre, and a rain so penetrating, that further outdoor exercise was now out of the question.'
text = nltk.Text(nltk.word_tokenize(jane_eyre))
text.concordance('taking')
text.concordance_list('taking')[0]

Resultado (NLTK 3.3):

Displaying 1 of 1 matches:
    taking a walk that day . We had been wander
ConcordanceLine(left=[],
                query='taking',
                right=['a', 'walk', 'that', 'day', '.', 'We', 'had', 'been', 'wandering', ',', 'indeed', ',', 'in', 'the', 'leafless', 'shrubbery', 'an', 'hour'],
                offset=7,
                left_print='',
                right_print='a walk that day . We had been wande',
                line=' taking a walk that day . We had been wande')

Resultado desejado:

Displaying 1 of 1 matches:
    Chapter 1 THERE was no possibility of taking a walk that day . We had been wander
ConcordanceLine(left=['Chapter', '1', 'THERE', 'was', 'no', 'possibility', 'of'],
                query='taking',
                right=['a', 'walk', 'that', 'day', '.', 'We', 'had', 'been', 'wandering', ',', 'indeed', ',', 'in', 'the', 'leafless', 'shrubbery', 'an', 'hour'],
                offset=7,
                left_print='Chapter 1 THERE was no possibility of',
                right_print='a walk that day . We had been wande',
                line='Chapter 1 THERE was no possibility of taking a walk that day . We had been wande')

Obrigado @BLKSerene por relatar o bug!

Ah, há uma solução legal aqui. Em vez do if-else. Podemos cortar o limite mínimo de max() , por exemplo

left_context = self._tokens[max(0, i-context):i]

Adicionar o doctest a https://github.com/nltk/nltk/blob/develop/nltk/test/concordance.doctest para o teste de integração / regressão contínua seria muito útil =)

Patching https://github.com/nltk/nltk/issues/2088
The left slice of the left context should be clip to 0 if the `i-context` < 0.

>>> from nltk import Text, word_tokenize
>>> jane_eyre = 'Chapter 1\nTHERE was no possibility of taking a walk that day. We had been wandering, indeed, in the leafless shrubbery an hour in the morning; but since dinner (Mrs. Reed, when there was no company, dined early) the cold winter wind had brought with it clouds so sombre, and a rain so penetrating, that further outdoor exercise was now out of the question.'
>>> text = Text(word_tokenize(jane_eyre))
>>> text.concordance_list('taking')[0].left
['Chapter', '1', 'THERE', 'was', 'no', 'possibility', 'of']

Remendado em # 2103. Obrigado @BLKSerene e @ dnc1994!

Esta página foi útil?
0 / 5 - 0 avaliações