from konlpy.tag import Okt
okt = Okt()
token = okt.morphs('꿈의 거처') # morphs - 형태소 분석
token

from konlpy.tag import Okt
okt = Okt()
token = okt.pos('달이 참 예쁘다고') # pos - 품사의 정보 추가해줌
print(token)
token = okt.pos('달이 참 예쁘다고',join=True)
print(token)
-pos() : 품사 정보를 추가해주는 기능

3. 문장의 한 구절만 출력하기
from konlpy.tag import Okt
okt = Okt()
sentence = okt.phrases('달이 참 예쁘다고') # phrases - 한 구절 출력
sentence

from konlpy.tag import Okt
okt = Okt()
text = input()
sentence_tag = okt.pos(text)
print(sentence_tag)

5. 문장을 입력받아 명사와 형용사의 빈도를 계산하여 가장 빈도가 높은 단어들을 찾기
import collections # 동일한 값이 몇개인지 표현해주는 모듈
text = '서기가 영원해도 난 마지막 나야 난 나라는 시대의 처음과 끝이야 난 나라는 인류의 기원과 종말이야 넌 나라는 마음의 유일한 무덤이야 넌 나라는 시계의 마지막 시침이야 난 나라는 우주의 빅뱅과 블랙홀이야 난 나라는 신화의 실체와 허구야 난 너의 이름을 닮은 집을 지을 거야'
sentece_tag=okt.pos(text)
adj_list=[]
for word,tag in sentece_tag:
if tag in ['Noun','Adjective']: # 품사 중에서 명사, 형용사인 것만 추출하기
adj_list.append(word)
counts=collections.Counter(adj_list) # 개수 세어줌
tag=counts.most_common(10) # 가장 많이 나온 순서대로 n개
print(tag)


!pip install wordcloud
import wordcloud
import matplotlib.pyplot as plt
text1 = input()
sentence_tag = okt.pos(text1)
adj_list1 = []
for word, tag in sentence_tag:
if tag in ['Noun', 'Adjective']:
adj_list1.append(word)
print(adj_list1)

3. 형태소 개수 Count
import collections
counts = collections.Counter(adj_list1)
tag = counts.most_common(10)
print(tag)

4. word cloud로 시각화
from wordcloud import WordCloud
wc = WordCloud(font_path='NanumGothic', background_color='white', max_font_size=60)
cloud = wc.generate_from_frequencies(dict(tag)) # 항상 dict형태로 해야함
cloud
plt.figure(figsize=(10,8))
plt.imshow(cloud)

import pandas as pd
import matplotlib.pyplot as plt
from konlpy.tag import Okt
from wordcloud import WordCloud
okt = Okt()
df = pd.read_table('/content/drive/MyDrive/AI스쿨 파일/ratings_train.txt')
df

2. 중복, 결측치 확인
# 중복값, 결측치 확인(id 기준)
print(df['id'].nunique())
print(df.isnull().sum())

3. 결측치 제거
# 결측치 제거
df = df.dropna(how = 'any') # Null 값이 존재하는 행 제거
print(df.isnull().values.any()) # Null 값이 존재하는지 확인

df['document'] = df['document'].str.replace("[^ㄱ-ㅎㅏ-ㅣ가-힣 ]","")
from konlpy.tag import Okt
okt=Okt()
temp_list=[]
for sentence in df['document']:
s_list=okt.pos(sentence)
for word,tag in s_list:
if tag in ['Noun','Adjective']:
temp_list.append(word)
counts=collections.Counter(temp_list)
tag=counts.most_common(50)
tag

6. word cloud 생성
from wordcloud import WordCloud
#wc=WordCloud(font_path=font_path,background_color='skyblue', max_font_size=60)
wc=WordCloud(font_path='malgun',background_color='skyblue', max_font_size=60)
cloud=wc.generate_from_frequencies(dict(tag))
import matplotlib.pyplot as plt
plt.figure(figsize=(10,8))
plt.imshow(cloud)

import pandas as pd
df = pd.read_csv('/content/drive/MyDrive/AI스쿨 파일/comment_rank.csv')
df.head()

2. 중복 & 결측치 확인
# 중복 & 결측치 확인
df['movie'].nunique()
df.isnull().sum()
# 제거
df = df.dropna(how = 'any') # Null 값이 존재하는 행 제거
df.isnull().sum()

3. movie 컬럼을 기준으로 그룹화 한 뒤 rank의 총합 계산
data = df.groupby('movie')['rank'].sum()
data.nlargest(5)

4. 고양이 집사 영화 코멘트 중 가장 많이 쓰인 단어 10개 word cloud
data = df[df.movie=='고양이 집사']
data['comment'] = data['comment'].str.replace("[^ㄱ-ㅎㅏ-ㅣ가-힣 ]","")
temp_list = []
for sentence in data['comment']:
s_list = okt.pos(sentence)
for word, tag in s_list:
if tag in ['Noun', 'Adjective']:
temp_list.append(word)
counts = collections.Counter(temp_list)
tag = counts.most_common(10)
tag
wc = WordCloud(font_path='NanumGothic', background_color='white', max_font_size=70)
cloud = wc.generate_from_frequencies(dict(tag))
plt.figure(figsize=(10,8))
plt.imshow(cloud)
