File: languageclassifier.py

package info (click to toggle)
w3af 1.0-rc3svn3489-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 59,908 kB
  • ctags: 16,916
  • sloc: python: 136,990; xml: 63,472; sh: 153; ruby: 94; makefile: 40; asm: 35; jsp: 32; perl: 18; php: 5
file content (294 lines) | stat: -rw-r--r-- 9,895 bytes parent folder | download
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/python
# -*- coding: utf-8 -*- 
# Sets the encoding to utf-8 to avoid problems with æøå
import glob
import re
import random
import math
import pickle
import os

from nltk.corpus import stopwords

from urlextracter import URLextracter
from sgmllib import *

class NaiveBayes(object):
       
    p_word_given_lang = {}
    
    training_files = []
    test_files = []
    candidate_languages = []

    def __init__(self):
        self.nor_stopwords = {}
        for word in stopwords.words('norwegian'):
            self.nor_stopwords[word] = True
            
        self.eng_stopwords = {}
        for word in stopwords.words('english'):
            self.eng_stopwords[word] = True
            
        self.load(os.path.join("files","lang_data.pickle"))

     
    """
    load pickled training results
    picklepath is the local path to your picklefile
    """        
    def load(self,picklepath):
        try:
            p = open(picklepath, 'rb')
            data = pickle.load(p)
            self.p_word_given_lang = data["p_word_given_lang"]
            self.candidate_languages = data["canidate_languages"]
            self.p_lang = data["p_lang"]
            self.vocabulary = data["vocabulary"]
        except IOError:
            self.p_word_given_lang = {}
            print "Nothing to load here!"
        
        
    """
    Train the classifier with data placed 
    in a folder named as the related language.
    Example: /path/to/files/eng/file01.txt
    """    
    def train(self, path):
        # Setup
        data_files = glob.glob(path + "/*/*")
        random.shuffle(data_files)
        
        self.training_files = data_files[0:300]
        self.test_files = data_files[300:]
        
        self.files = {}
        self.p_lang = {}
        
        # Calculate P(H)
        for file in self.training_files:
            values = file.split('/')
            lang = values[-2]
        
            if not self.p_lang.has_key(lang):
                self.p_lang[lang] = 0.0
            
            self.p_lang[lang] += 1.0
            
            if not self.files.has_key(lang):
                self.files[lang] = []
            
            f = open(file, 'r')
            self.files[lang] += f.read().replace("\n", " ").replace(".", "").split(" ")
            f.close()
            
        # Calculate probabilities
        for lang in self.p_lang.keys():
            self.p_lang[lang] /= len(self.training_files)
            
        self.vocabulary = self.__createVocabulary(self.files)
        
        # Calculate P(O | H) 
        p_word_given_lang = self.p_word_given_lang
        for lang in self.files.keys():
            p_word_given_lang[lang] = {}
            
            for word in self.vocabulary[lang].keys():
                p_word_given_lang[lang][word] = 1.0
            
            for word in self.files[lang]:
                if self.vocabulary[lang].has_key(word):
                    p_word_given_lang[lang][word] += 1.0
                    
            for word in self.vocabulary[lang].keys():
                p_word_given_lang[lang][word] /= len(self.files[lang]) + len(self.vocabulary[lang])
                
        print "Training finished...(training-set of size %d)" % len(self.training_files)
        self.p_word_given_lang = p_word_given_lang
        self.candidate_languages = self.files.keys()
        
        # Save result as a file
        output = open(os.path.join("files","lang_data.pickle"),'wb')
        data = {}
        data["p_word_given_lang"] = p_word_given_lang
        data["canidate_languages"] = self.files.keys()
        data["p_lang"] = self.p_lang
        data["vocabulary"] = self.vocabulary
        pickler = pickle.dump(data, output, -1)
        output.close()   
    
    """
    Filter out the words we're not interessted in
    and return a dictionary with all remaining words
    sorted by language.
    Example: vocabulary[eng] = {'lazy','fox',...}
    """
    def __createVocabulary(self, files):
        # Count number of occurance of each word
        word_count = {}
        for lang in files.keys():
            for word in files[lang]:
                if not word_count.has_key(word):
                    word_count[word] = 0
                word_count[word] += 1
        
        vocabulary = {}
        vocabulary['eng'] = {}
        vocabulary['no'] = {}
        for word in word_count.keys():
            if word_count[word] > 2:
                if word != '':
                    if not word in self.nor_stopwords:
                        vocabulary['no'][word] = True
                    if not word in self.eng_stopwords:
                        vocabulary['eng'][word] = True
        return vocabulary
    
    
    """
    Test the accuracy of the classifier.
    Provide test files as list or path.
    The path must be on the same form as when training.
    """
    def testAccuracy(self,test_files = ""):
        
        if test_files == "":
            print "No test files given"
            return
        elif os.path.isdir(str(test_files)):
            self.test_files = glob.glob(test_files + "/*/*")
            random.shuffle(self.test_files)
        else:
            self.test_files = test_files
  
        errors = 0.0
        total = 0.0
        
        # Use if test_files is provided as path
        #test_files = glob.glob(path + "/*/*")
        #random.shuffle(test_files)
        
        
        for file in self.test_files:
            values = file.split(os.sep)
            true_lang = values[-2]

            f = open(file, "r")    
            file_to_be_classified = f.read().replace("\n", " ").replace(".", "").split(" ")
            f.close()
            
            # Finds group with max P(O | H) * P(H)
            max_lang = 0
            max_p = 1
            for candidate_lang in self.candidate_languages:
                # Calculates P(O | H) * P(H) for candidate group
                p = math.log(self.p_lang[candidate_lang])
                for word in file_to_be_classified:
                    if self.vocabulary[candidate_lang].has_key(word):
                        p += math.log(self.p_word_given_lang[candidate_lang][word])
        
                if p > max_p or max_p == 1:
                    max_p = p
                    max_lang = candidate_lang
        
            total += 1.0
            if true_lang != max_lang:
                errors += 1.0
        print "Classifying finished...(test-set of size %d)" % len(self.test_files)
        print "Errors %d" % errors
        print "Total %d" % total
        print "Accuracy: %.3f" % (1.0 - errors/total)
    
    def classifyText(self, text):
        max_lang = 0
        max_p = 1
        for candidate_lang in self.candidate_languages:
            # Calculates P(O | H) * P(H) for candidate group
            p = math.log(self.p_lang[candidate_lang])
            words = text.split(' ')
            unknown_words = []
            known_words = []
            for word in words:
                if self.vocabulary[candidate_lang].has_key(word):
                    p += math.log(self.p_word_given_lang[candidate_lang][word])
                    if word not in known_words:
                        known_words.append(word)
                else:
                    if word not in unknown_words:
                        unknown_words.append(word)
            if p > max_p or max_p == 1:
                max_p = p
                max_lang = candidate_lang
        
        percent = (float(len(known_words)) / float(len(unknown_words)))
        # return unknown if the ratio of known words is less or equal to 0.25
        if percent <= 0.25:        
            max_lang = "unknown"
        
        return max_lang
    
    def classifyURL(self, url):
        ue = URLextracter(url)
        print 'Classifying %s' % url
        content = ue.output() 
        content = re.sub(r"[^a-zA-ZæøåÆØÅ]", " ", content)
        content = content.strip()
        return self.classifyText(content)
    
    def handle_decl(self,data):
        pass

    def report_unbalanced(self,tag):
        pass
    
    def demo(self):
        print "Demo of language classifier"
        print "=" * 40
        nb = NaiveBayes()
        nb.load(os.path.join("files","lang_data.pickle"))
        
        print "Classifying plain text(10 first sentences from \"nltk.corpus.abc.sents\")"
        print "=" * 40
        text = ""
        import nltk.corpus
        sents = nltk.corpus.abc.sents()
        for words in sents[0:10]:
            text+= " ".join(words) + "\n"
        print text
        print "=" * 40
        print "Languages is: %s" % nb.classifyText(text)
        
        print "\n"
        print "Classifying 10 URLs"
        print "=" * 40
        
        lang = nb.classifyURL("http://harvardscience.harvard.edu/")
        print "-->language: %s \n" % lang
        lang = nb.classifyURL("http://vg.no")
        print "-->language: %s \n" % lang
        lang = nb.classifyURL("http://bbc.co.uk")
        print "-->language: %s \n" % lang
        lang = nb.classifyURL("http://startsiden.no")
        print "-->language: %s \n" % lang
        lang = nb.classifyURL("http://news.com")
        print "-->language: %s \n" % lang
        lang = nb.classifyURL("http://www.munimadrid.es")
        print "-->language: %s \n" % lang
        lang = nb.classifyURL("http://www.welt.de/")
        print "-->language: %s \n" % lang
        lang = nb.classifyURL("http://www.news.pl/")
        print "-->language: %s \n" % lang
        lang = nb.classifyURL("http://www.ekstrabladet.dk/")
        print "-->language: %s \n" % lang
        lang = nb.classifyURL("http://www.gazzetta.it/")
        print "-->language: %s \n" % lang      
    demo = classmethod(demo)
    
def demo():
    NaiveBayes.demo()
    
if __name__=="__main__":
    NaiveBayes.demo()