-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapaProject_MAX.py
174 lines (133 loc) · 5.12 KB
/
apaProject_MAX.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
163
164
165
166
167
168
169
170
171
172
import csv
import collections, itertools
import nltk.classify.util, nltk.metrics
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import movie_reviews, stopwords
from nltk.collocations import BigramCollocationFinder
from nltk.metrics import BigramAssocMeasures
from nltk.probability import FreqDist, ConditionalFreqDist
from nltk.tokenize import RegexpTokenizer
from nltk.corpus import stopwords
import random
stopset = set(stopwords.words('english'))
# Construction du dictionnaire avec variable bool indiquant presence du mot
def best_word_feats(words):
return dict([(word, True) for word in words if word in bestwords and word not in stopset])
#Classe permettant l'extraction des phrases du fichier
class PipeDialect(csv.Dialect):
delimiter = "|"
quotechar = None
escapechar = None
doublequote = None
lineterminator = "\r\n"
quoting = csv.QUOTE_NONE
skipinitialspace = False
#Classifieur binaire de base
def evaluate_classifier(featx):
fneg = "data.neg.txt"
fpos = "data.pos.txt"
f = "data.txt"
fileNeg = open(fneg, "rb")
filePos = open(fpos, "rb")
file = open(f, "rb")
reader = csv.reader(file, PipeDialect())
readerNeg = csv.reader(fileNeg, PipeDialect())
readerPos = csv.reader(filePos, PipeDialect())
sentencesNeg = []
sentencesPos = []
wordsNeg = []
wordsPos = []
for row in readerNeg:
sentencesNeg.append(row[2].lower())
for row in readerPos:
sentencesPos.append(row[2].lower())
tokenizer = RegexpTokenizer(r'\w+')
for i in range(0, len(sentencesNeg)-1):
wordsNeg.append(tokenizer.tokenize(sentencesNeg[i]))
for i in range(0, len(sentencesPos)-1):
wordsPos.append(tokenizer.tokenize(sentencesPos[i]))
negfeats = [(featx(wordsNeg[i]), 'neg') for i in range(0, len(wordsNeg)-1)]
posfeats = [(featx(wordsPos[i]), 'pos') for i in range(0, len(wordsPos)-1)]
random.shuffle(negfeats)
random.shuffle(posfeats)
negcutoff = len(negfeats)*3/4
poscutoff = len(posfeats)*3/4
trainfeats = negfeats[:negcutoff] + posfeats[:poscutoff]
testfeats = negfeats[negcutoff:] + posfeats[poscutoff:]
print 'train on %d instances, test on %d instances' % (len(trainfeats), len(testfeats))
classifier = NaiveBayesClassifier.train(trainfeats)
refsets = collections.defaultdict(set)
testsets = collections.defaultdict(set)
for i, (feats, label) in enumerate(testfeats):
refsets[label].add(i)
observed = classifier.classify(feats)
testsets[observed].add(i)
print 'accuracy:', nltk.classify.util.accuracy(classifier, testfeats)
print 'pos precision:', nltk.metrics.precision(refsets['pos'], testsets['pos'])
print 'pos recall:', nltk.metrics.recall(refsets['pos'], testsets['pos'])
print 'neg precision:', nltk.metrics.precision(refsets['neg'], testsets['neg'])
print 'neg recall:', nltk.metrics.recall(refsets['neg'], testsets['neg'])
classifier.show_most_informative_features()
file.close()
filePos.close()
fileNeg.close()
fneg = "data.neg.txt"
fpos = "data.pos.txt"
f = "data.txt"
fileNeg = open(fneg, "rb")
filePos = open(fpos, "rb")
file = open(f, "rb")
reader = csv.reader(file, PipeDialect())
readerNeg = csv.reader(fileNeg, PipeDialect())
readerPos = csv.reader(filePos, PipeDialect())
sentencesNeg = []
sentencesPos = []
wordsNeg = []
wordsPos = []
wordsFullPos = []
wordsFullNeg = []
for row in readerNeg:
sentencesNeg.append(row[2])
for row in readerPos:
sentencesPos.append(row[2])
tokenizer = RegexpTokenizer(r'\w+')
for i in range(0, len(sentencesNeg)-1):
wordsNeg.append(tokenizer.tokenize(sentencesNeg[i]))
for i in range(0, len(sentencesPos)-1):
wordsPos.append(tokenizer.tokenize(sentencesPos[i]))
#Frequence des mots
word_fd = FreqDist()
label_word_fd = ConditionalFreqDist()
for i in range(0, len(wordsPos)-1):
for j in range(0, len(wordsPos[i])-1):
wordsFullPos.append(wordsPos[i][j])
for i in range(0, len(wordsNeg)-1):
for j in range(0, len(wordsNeg[i])-1):
wordsFullNeg.append(wordsNeg[i][j])
for word in wordsFullPos:
word_fd.inc(word.lower())
label_word_fd['pos'].inc(word.lower())
for word in wordsFullNeg:
word_fd.inc(word.lower())
label_word_fd['neg'].inc(word.lower())
# n_ii = label_word_fd[label][word]
# n_ix = word_fd[word]
# n_xi = label_word_fd[label].N()
# n_xx = label_word_fd.N()
#Nombre d'occurence des mots
pos_word_count = label_word_fd['pos'].N()
neg_word_count = label_word_fd['neg'].N()
total_word_count = pos_word_count + neg_word_count
word_scores = {}
#Utilisation de bigrams
for word, freq in word_fd.iteritems():
pos_score = BigramAssocMeasures.chi_sq(label_word_fd['pos'][word],
(freq, pos_word_count), total_word_count)
neg_score = BigramAssocMeasures.chi_sq(label_word_fd['neg'][word],
(freq, neg_word_count), total_word_count)
word_scores[word] = pos_score + neg_score
#On utilise uniquement les 10 000 mots les plus informatifs
best = sorted(word_scores.iteritems(), key=lambda (w,s): s, reverse=True)[:5000]
bestwords = set([w for w, s in best])
print 'evaluating best word features'
evaluate_classifier(best_word_feats)