if offsets:
for i in offsets:
query_word = self._tokens[i]
# Find the context of query word.
left_context = self._tokens[i-context:i]
Jika kemunculan pertama istilah penelusuran berada di awal teks (misalnya di offset 7), misalkan parameter lebar disetel ke 20, maka [i- context: i ] akan dievaluasi sebagai [-13: 7] .
Dalam kasus ini, jika teks terdiri lebih dari 20 kata, variabel left_context akan menjadi daftar kosong, bukan daftar yang berisi 7 kata pertama dari teks.
Perbaikan sederhana akan dilakukan:
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]
Bisakah Anda memberikan masukan sampel dan keluaran yang diinginkan sehingga kami dapat menambahkan ke uji regresi?
Memasukkan:
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]
Keluaran (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')
Output yang Diinginkan:
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')
Terima kasih @BLKSerene telah melaporkan bug!
Ah, ada perbaikan keren di sini. Alih-alih jika-lain. Kita dapat memotong batas minimum menjadi max()
, misalnya
left_context = self._tokens[max(0, i-context):i]
Menambahkan doctest ke https://github.com/nltk/nltk/blob/develop/nltk/test/concordance.doctest untuk uji integrasi / regresi berkelanjutan akan sangat membantu =)
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']
Ditambal di # 2103. Terima kasih @BLKSerene dan @ dnc1994!