Nltk: find_concordance () devuelve una lista vacía para left_context

Creado en 20 ago. 2018  ·  4Comentarios  ·  Fuente: 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]

Cuando la primera aparición del término de búsqueda está al principio del texto (por ejemplo, en el desplazamiento 7), suponga que el parámetro de ancho se establece en 20, entonces [i- contexto: i ] se evaluaría como [-13: 7] .
En este caso, si el texto consta de más de 20 palabras, la variable left_context sería una lista vacía, en lugar de una lista que contenga las primeras 7 palabras del texto.

Una simple solución sería suficiente:

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 comentarios

¿Podría proporcionar una entrada de muestra y una salida deseada para que podamos agregar a la prueba de regresión?

Aporte:

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]

Salida (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')

Salida deseada:

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')

¡Gracias @BLKSerene por informar del error!

Ah, hay una buena solución aquí. En lugar del if-else. Podemos recortar el límite mínimo a max() , por ejemplo

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

Agregar el doctest a https://github.com/nltk/nltk/blob/develop/nltk/test/concordance.doctest para la prueba de integración / regresión continua sería muy ú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']

Parcheado en # 2103. ¡Gracias @BLKSerene y @ dnc1994!

¿Fue útil esta página
0 / 5 - 0 calificaciones