This repository has been archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
162 lines (97 loc) · 3.67 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# -*- coding: utf-8 -*-
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 12 13:34:48 2019
@author: hana
"""
import pandas as pd
import numpy as np
import string
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import time
import re
#belen unshuulah datanii heseg mongol ugiig utf8 aar unshuulj bga
data = pd.read_csv("aaaa.csv",encoding='UTF-8')
class Nana:
def __init__(self):
# Файлаас зогсох үг буюу туслах үгнүүдийг унших
with open('aaaa.csv', 'r') as f:
self.stopwords = f.read().split('\n')
# Файлаас залгаваруудыг унших
with open('aaaa.csv', 'r') as f:
self.rules = f.read().split('\n')
# Залгавар үгнүүдийн REGEX үүсгэх
self.rulesREGEXP = '$|'.join(self.rules)+'$'
def parse(self, text):
text_sentences = text.split('.')
sentences = []
for text_sentence in text_sentences:
# өгүүлбэрийн текстийг үгүүд болгож хувиргах
tokens = text_sentence.split(' ')
# том үсгүүдийг болиулах
tokens = [w.lower() for w in tokens]
# үг бүрээс тэмдэгтүүдийг хасах
table = str.maketrans('', '', string.punctuation)
stripped = [w.translate(table) for w in tokens]
# текст бус үгүүдийг хасах
words = [word for word in tokens if word.isalpha()]
# stopword уудыг хасах
words = [w for w in words if not w in self.stopwords]
# stemming
words = [re.sub(self.rulesREGEXP, '', w) for w in words if len(w) >= 6]
sentences.append(words)
return sentences
data = data[['asuult', 'angilal']]
#datanii category nii torluudiig harna
data.angilal.unique()
data.groupby('angilal').describe()
#data end toon utguudiig ogno
data['NUM_angilal']=data.angilal.map({'Сэргээн санах':0,'Ойлгох':1,'Хэрэглэх':2,'Задлан шинжлэх':3,'Үнэлэх':4, 'Бүтээх':5})
data.head()
#dataset iin huvaaltiin hesguud
x_train, x_test, y_train, y_test = train_test_split(data.asuult, data.NUM_angilal, random_state=0, test_size=0.2)
#bow iig bigram ruu horvuulj bui heseg
start = time.clock()
vect = CountVectorizer(ngram_range=(2,2))
x_train = vect.fit_transform(x_train)
#surgaltiin datag toon vectorluu horvuuleha
x_test = vect.transform(x_test)
print (vect)
print (time.clock()-start)
#surgalt
start = time.clock()
mnb = MultinomialNB(alpha =0.2)
mnb.fit(x_train,y_train)
result= mnb.predict(x_test)
print(mnb)
print (time.clock()-start)
#model iin heden huviin biyleltiin onoolt
accuracy_score(result,y_test)
print('accurence')
print(accuracy_score(result,y_test))
#garaltiin utga
def predict_news(news):
test = vect.transform(news)
pred= mnb.predict(test)
if pred == 0:
return 'Сэргээн санах'
elif pred ==1 :
return 'Ойлгох'
elif pred ==2 :
return 'Хэрэглэх'
elif pred ==3 :
return 'Задлан шинжлэх'
elif pred ==4 :
return 'Өргөдөл'
else :
return 'Бүтээх'
x=["Дугаарлалтанд үсэг-цифрийн форматыгашиглана"]
r = predict_news(x)
print (r)
#taamaglaliin matrix iig hevlene
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, result)