if offsets:
for i in offsets:
query_word = self._tokens[i]
# Find the context of query word.
left_context = self._tokens[i-context:i]
عندما يكون التواجد الأول لمصطلح البحث في بداية النص (على سبيل المثال عند الإزاحة 7) ، افترض أن معلمة العرض مضبوطة على 20 ، ثم [i- Context : i ] سيتم تقييمها على أنها [-13: 7] .
في هذه الحالة ، إذا كان النص يتكون من أكثر من 20 كلمة ، فسيكون المتغير left_context قائمة فارغة ، وليس قائمة تحتوي على أول 7 كلمات من النص.
الإصلاح البسيط سيفي بالغرض:
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]
هل يمكنك تقديم عينة من المدخلات والمخرجات المرغوبة حتى نتمكن من إضافة اختبار الانحدار؟
إدخال:
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]
الإخراج (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')
النتيجة المرجوة:
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')
شكرًا BLKSerene على الإبلاغ عن الخطأ!
آه ، هناك حل رائع هنا. بدلاً من if-else. يمكننا قص الحد الأدنى المرتبط بـ max()
، على سبيل المثال
left_context = self._tokens[max(0, i-context):i]
إضافة الدكتورة إلى https://github.com/nltk/nltk/blob/develop/nltk/test/concordance.doctest لاختبار التكامل / الانحدار المستمر ستكون مفيدة جدًا =)
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']
مصححة في # 2103. شكرا BLKSerene و @ dnc1994!