Nltk: find_concordance () gibt eine leere Liste für left_context zurück

Erstellt am 20. Aug. 2018  ·  4Kommentare  ·  Quelle: 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]

Wenn das erste Vorkommen des Suchbegriffs am Anfang des Textes steht (z. B. bei Versatz 7), wird angenommen, dass der Parameter width auf 20 gesetzt ist, und [i- context: i ] wird als [-13: 7] ausgewertet. .
In diesem Fall wäre die Variable left_context eine leere Liste und keine Liste mit den ersten 7 Wörtern des Textes, wenn der Text mehr als 20 Wörter enthält.

Eine einfache Lösung würde reichen:

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

Alle 4 Kommentare

Könnten Sie eine Beispieleingabe und die gewünschte Ausgabe bereitstellen, damit wir den Regressionstest ergänzen können?

Eingang:

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]

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

Gewünschte Ausgabe:

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

Vielen Dank an @BLKSerene für die Meldung des Fehlers!

Ah, hier gibt es eine coole Lösung. Anstelle des Wenn-Sonst. Wir können die an ein max() gebundene Mindestgrenze abschneiden, z

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

Das Hinzufügen des Doctest zu https://github.com/nltk/nltk/blob/develop/nltk/test/concordance.doctest für den kontinuierlichen Integrations- / Regressionstest wäre sehr hilfreich =)

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

In # 2103 gepatcht. Danke @BLKSerene und @ dnc1994!

War diese Seite hilfreich?
0 / 5 - 0 Bewertungen

Verwandte Themen

mwess picture mwess  ·  5Kommentare

alvations picture alvations  ·  4Kommentare

DavidNemeskey picture DavidNemeskey  ·  4Kommentare

stevenbird picture stevenbird  ·  4Kommentare

ndvbd picture ndvbd  ·  4Kommentare