๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
2. NLTK๋ก ๋ฌธ์ ๊ฐ ์ ์ฌ๋ ์ธก์ ํ๊ธฐ “์ด ์์ค์ ์์์๋ ๋๊ตฌ์ผ๊น” ๋ณธ๋ฌธ
2. NLTK๋ก ๋ฌธ์ ๊ฐ ์ ์ฌ๋ ์ธก์ ํ๊ธฐ “์ด ์์ค์ ์์์๋ ๋๊ตฌ์ผ๊น”
์ง์ง์ํ์นด 2022. 10. 17. 15:55221017 ์์ฑ
<๋ณธ ๋ธ๋ก๊ทธ๋์ค์ ํ์ด์ฌ ํธ์ฆ์จ ํ๋ก์ ํธ์ github ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค>
https://www.onlybook.co.kr/m/entry/python-projects
์ค์ ํ์ด์ฌ ํธ์ฆ์จ ํ๋ก์ ํธ
์ค์ ํ์ด์ฌ ํธ์ฆ์จ ํ๋ก์ ํธ ๋ฌธ์ ํด๊ฒฐ๊ณผ ์ค๋ฌด ์์ฉ๋ ฅ์ ํค์ฐ๊ธฐ ์ํ ๋๋ง์ ํ์ด์ฌ ํฌํธํด๋ฆฌ์ค ๋ง๋ค๊ธฐ ๋ฆฌ ๋ณธ ์ง์ | ์คํ์ ์ฎ๊น 420์ชฝ | 28,000์ | 2022๋ 5์ 31์ผ ์ถ๊ฐ | 185*240*20 | ISBN13 9791189909406
www.onlybook.co.kr
๐ NLTK
: Natural Language ToolKit์ ์ฝ์๋ก ์์ฐ์ด ์ฒ๋ฆฌ ๋ฐ ๋ถ์์ ์ํ ํ์ด์ฌ ํจํค์ง
: ํ ํฐ์์ฑํ๊ธฐ, ํํ์ ๋ถ์, ํ์ฌ ํ๊น ํ๊ธฐ ๋ฑ ๋ค์ํ ๊ธฐ๋ฅ์ ์ ๊ณต
๐ NLTK๋ก ๋ฌธ์ ๊ฐ ์ ์ฌ๋ ์ธก์ ํ๊ธฐ
: ์์ฐ์ด ์ฒ๋ฆฌ๋ฅผ ํตํด ์์ ์ฝ๋ ๋์ผ์ด๋ H. G. ์ฐ์ค ์ค ๋๊ฐ ใ์์ด๋ฒ๋ฆฐ ์ธ๊ณใ๋ฅผ ์ผ๋์ง๋ฅผ ๊ฒฐ์
: NLTK, matplotlib ๋ฑ์ ๋ชจ๋์ ๋ฌผ๋ก ์ด๊ณ ๋ถ์ฉ์ด(stop words), ํ์ฌ, ์ดํ์ ํ๋ถํจ, ์์นด๋ ์ ์ฌ์ฑ(Jaccard similarity) ๋ฑ์ ์คํ์ผ๋ก๋ฉํธ๋ฆฌ(stylometry) ๊ธฐ๋ฒ์ ํ์ฉ
# punctuation ํํธ๋งต ๋ง๋ค๊ธฐ
import math
from string import punctuation # ๋ฐ์ดํ,๋ง์นจํ ๋ฌผ์ํ ๋ฑ๋ฑ ์ด๋ฐ๋ฅ์ ๋ฌธ์ฅ๋ถํธ
import nltk
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import seaborn as sns
PUNCT_SET = set(punctuation)
def main():
# ์์ฑ์๊ฐ ์ฌ์ ์ ํ
์คํธ ํ์ผ์ ๋ก๋
strings_by_author = dict()
strings_by_author['doyle'] = text_to_string('hound.txt')
strings_by_author['wells'] = text_to_string('war.txt')
strings_by_author['unknown'] = text_to_string('lost.txt')
# ๋ฌธ์ฅ ๋ถํธ๋ง ๋ณด์กดํ ํ
์คํธ ๋ฌธ์์ด์ ํ ํฐํ
punct_by_author = make_punct_dict(strings_by_author)
# punctuation์ ์ซ์ ๊ฐ์ผ๋ก ๋ณํํ๊ณ ํํธ๋งต์ ํ์
plt.ion()
for author in punct_by_author:
heat = convert_punct_to_number(punct_by_author, author)
arr = np.array((heat[:6561])) # ์ ์ฌ๊ฐํ ๋ฐฐ์ด์ ๊ฐ์ฅ ํฐ ํฌ๊ธฐ๋ก ์๋ฅด๊ธฐ
arr_reshaped = arr.reshape(int(math.sqrt(len(arr))),
int(math.sqrt(len(arr))))
fig, ax = plt.subplots(figsize=(7, 7))
sns.heatmap(arr_reshaped,
cmap=ListedColormap(['blue', 'yellow']), # ์ธ์๋ก ์ฃผ์ด์ง ์์์ ๊ทธ๋ํ์์ ํ์ํ๊ธฐ ์ํ ๊ฐ์ฒด
square=True,
ax=ax)
ax.set_title('Heatmap Semicolons {}'.format(author))
plt.show()
# # """ํ
์คํธ ํ์ผ์ ์ฝ๊ณ ๋ฌธ์์ด ๋ชฉ๋ก์ ๋ฐํ"""
# def text_to_string(filename):
# strings = []
# with open(filename) as f:
# strings.append(f.read())
# return '\n'.join(strings)
# """ํ
์คํธ ํ์ผ์ ์ฝ๊ณ ๋ฌธ์์ด์ ๋ฐํ"""
def text_to_string(filename):
"""Read a text file and return a string."""
with open(filename) as infile:
return infile.read()
# """์ ์๊ฐ ๋ง๋ญ์น์ ์ํด ํ ํฐํ๋ punctuation ์ฌ์ ์ ๋ฐํ"""
def make_punct_dict(strings_by_author):
"""Return dictionary of tokenized punctuation by corpus by author."""
punct_by_author = dict()
for author in strings_by_author:
# ๋จ์ด ๋จ์๋ก ๋๋๊ธฐ
tokens = nltk.word_tokenize(strings_by_author[author])
# punct_by_author : ๋ฌธ์ฅ ๋ถํธ๋ง ๋ณด์กดํ ํ
์คํธ ๋ฌธ์์ด์ ํ ํฐํ
punct_by_author[author] = ([token for token in tokens
if token in PUNCT_SET])
print("Number punctuation marks in {} = {}"
.format(author, len(punct_by_author[author])))
return punct_by_author
# """์ซ์ ๊ฐ์ผ๋ก ๋ณํ๋ punctuation ๋ชฉ๋ก์ ๋ฐํ"""
def convert_punct_to_number(punct_by_author, author):
"""Return list of punctuation marks converted to numerical values."""
heat_vals = []
for char in punct_by_author[author]:
if char == ';':
value = 1
else:
value = 2
heat_vals.append(value)
return heat_vals
if __name__ == '__main__':
main()