-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
138 lines (114 loc) · 3.84 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import warnings
import re
from itertools import chain
from collections import Counter
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
from kiwipiepy import Kiwi
import mongodb
warnings.filterwarnings(action='ignore')
mpl.rcParams['axes.unicode_minus'] = False
plt.rcParams["font.family"] = 'NanumGothic'
client = mongodb.client
db_names = mongodb.db_names
col_reordered = [
'_id',
'Review_Id',
'Location',
'DatePost',
'Department',
'Employee_status',
'Company_name',
'Title',
'Pros',
'Cons',
'To_Management',
'Ratings',
'Culture',
'WorkLifeBalance',
'Benefits',
'Management',
'Opportunity',
'Potential',
'Recommend'
]
def get_collections(db_no):
db = client.get_database(db_names[db_no])
coll_names = {}
for i, coll in enumerate(db.list_collection_names()):
coll_names[i] = coll
return coll_names
def get_df(coll, collection_no):
cursor = coll.find()
df = pd.DataFrame(list(cursor))
return df[col_reordered]
def get_comp(df, company_name):
df_ = df[df['Company_name'] == company_name]
df_['DatePost'] = pd.to_datetime(df_['DatePost'], errors='coerce')
df_['year'] = df_['DatePost'].apply(lambda x: x.year)
return df_
def get_value_counts(df, year, field):
df_ = df.query(f'year == {year}')
cnt = df_[field].value_counts().values.tolist()
return (cnt[0]/sum(cnt))*100
def get_fluctuation2(df, field):
years = []
trends = []
for year in range(df.year.min(), df.year.max()+1):
years.append(year)
trends.append(get_value_counts(df, year, field))
return years, trends
def get_mean(df, year, field):
df_ = df.query(f'year == {year}')
return df_[field].mean()
def get_fluctuation(df, field):
years = []
trends = []
for year in range(df.year.min(), df.year.max()):
years.append(year)
trends.append(get_mean(df, year, field))
return years, trends
main_words = ["NNG", "NNP", "VV", "VA", "XR"]
sub_words = ["VV", "VA"]
def filter_docs(df, col):
result = []
for index, row in df.iterrows():
if row[col]:
filetered = [(token.form, token.tag) for token in row[col] if token.tag in main_words]
filetered = [form+"다" if tag in sub_words else form for form, tag in filetered]
result.append(filetered)
return result
def get_most_common(df, col, n:int):
kiwi = Kiwi()
# kiwi.load_user_dictionary('user_dictionary.txt')
kiwi.prepare()
compile = re.compile("\W+")
df['text'] = df[col].apply(lambda x: compile.sub(" ", x))
morph_analysis = lambda x: kiwi.tokenize(x) if type(x) is str else None
df['text'] = df['text'].apply(morph_analysis)
result = filter_docs(df, 'text')
flatten = list(chain(*result))
counter = Counter(flatten)
return counter.most_common(n)
def get_most_common_by_year(df_comp, year, col, n:int):
df_year = df_comp.query(f'year == {year}')
data = get_most_common(df_year, col, n)
return pd.DataFrame(data, columns=['words', year]).set_index('words')
def get_all_most_common_join_df(df, company_name, col, n):
df_comp = get_comp(df, company_name)
df_join = pd.DataFrame(get_most_common(df_comp, col, n), columns=['words', 'total']).set_index('words')
for year in range(df_comp.year.min(), df_comp.year.max()+1):
df_ = get_most_common_by_year(df_comp, year, col, n)
df_join = df_join.join(df_, how='outer')
return df_join
def draw_heatmap_most_common(df, company_name, col, n):
df_join = get_all_most_common_join_df(df, company_name, col, n)
df_join.fillna(0, inplace=True)
plt.figure(figsize=(15,15))
ax = sns.heatmap(df_join.iloc[:,1:], annot=True, fmt=".0f")
ax.set(xlabel="", ylabel="")
ax.xaxis.tick_top()
plt.title(f"{company_name} {col} TOP WORDS {n}")
plt.show()