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) ์ธ ๊ฒฝ์ฐ width ๋งค๊ฐ ๋ณ์๊ฐ 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์ 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 ๊ฐ์ฌํฉ๋๋ค!