new portuguese dictionary from Brigido
[iramuteq] / textstat.py
1 #!/bin/env python
2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2008-2012 Pierre Ratinaud
5 #License: GNU/GPL
6
7 #from chemins import ConstructPathOut, StatTxtPathOut, ffr
8 from chemins import PathOut
9 from analysetxt import AnalyseText
10 #from corpus import Corpus
11 from guifunct import getPage, getCorpus
12 from ConfigParser import RawConfigParser
13 from functions import sortedby, progressbar, CreateIraFile, exec_rcode, check_Rresult, DoConf
14 from dialog import StatDialog
15 from openanalyse import OpenAnalyse
16 #from ttparser import * 
17 import tempfile
18 from time import sleep
19 import wx
20 import os
21
22 import logging
23
24 logger = logging.getLogger('iramuteq.textstat')
25
26
27
28 class Stat(AnalyseText) :
29     def doanalyse(self) :
30         self.make_stats()
31
32     def preferences(self) :
33         return self.parametres
34
35     def make_stats(self):
36         if self.dlg :
37             if not 'dlg' in dir(self) :
38                 self.dlg = progressbar(self, 7)
39
40         formes = self.corpus.lems
41         tot = [[forme, formes[forme].freq, formes[forme].gram] for forme in formes if formes[forme].freq > 1]
42         tot = sortedby(tot, 2,1)
43         tot = [[i, val] for i, val in enumerate(tot)]
44         hapax = [[forme, formes[forme].freq, formes[forme].gram] for forme in formes if formes[forme].freq == 1]
45         hapax = sortedby(hapax, 1, 1)
46         hapax = [[i, val] for i, val in enumerate(hapax)]
47         act = [[forme, formes[forme].freq, formes[forme].gram] for forme in formes if formes[forme].act == 1]
48         act = sortedby(act, 2, 1)
49         act = [[i, val] for i, val in enumerate(act)]
50         supp = [[forme, formes[forme].freq, formes[forme].gram] for forme in formes if formes[forme].act == 2]        
51         supp = sortedby(supp, 2, 1)
52
53         supp = [[i, val] for i, val in enumerate(supp)]
54
55         self.result = {u'total' : dict(tot), u'formes_actives' : dict(act), u'formes_supplĂ©mentaires' : dict(supp), u'hapax' : dict(hapax), u'glob' : ''}
56         occurrences = sum([val[1][1] for val in tot]) + len(hapax)
57         phapax = (float(len(hapax)) / float(occurrences)) * 100
58         phapax_forme = (float(len(hapax)) / (float(len(formes)))) * 100
59         moy_occu_mot = float(occurrences) / float(len(formes))
60         txt = 'Globale\n'
61         txt += 'nombre de textes : %i\n' % len(self.corpus.ucis)
62         txt += 'nombre d\'occurrences : %i\n' % occurrences
63         txt += 'nombre de formes : %i\n' % (len(formes))
64         txt += 'moyenne d\'occurrences par forme : %.2f\n' % moy_occu_mot
65         txt += 'nombre d\'hapax : %i (%.2f%% des occurrences - %.2f%% des formes)\n' % (len(hapax), phapax, phapax_forme)
66         print float(occurrences), float(len(self.corpus.ucis))
67         txt += 'moyenne d\'occurrences par texte : %.2f' % (float(occurrences)/float(len(self.corpus.ucis)))
68         if self.dlg :
69             self.dlg.Update(7, u'Ecriture...')
70         self.result['glob'] = txt
71         self.print_result()
72         # for Zipf grap
73         txt = """
74         source("%s")
75         tot <- read.csv2("%s", header = FALSE, row.names = 1)
76         """ % (self.parent.RscriptsPath['Rgraph'], self.pathout['total.csv'])
77         if len(hapax) :
78             txt += """
79             hapax <- read.csv2("%s", header = FALSE, row.names = 1)
80             tot <- rbind(tot, hapax)
81             """ % self.pathout['hapax.csv']
82         txt += """
83         open_file_graph("%s", width = 400, height = 400)
84         plot(tot[,1], log = 'xy', xlab='log(rangs)', ylab = 'log(frequences)', col = 'red', pch=16)
85         dev.off()
86         """ % (self.pathout['zipf.png'])
87         tmpscript = tempfile.mktemp(dir=self.parent.TEMPDIR)
88         with open(tmpscript, 'w') as f :
89             f.write(txt)
90         pid = exec_rcode(self.parent.RPath, tmpscript, wait = False)
91         while pid.poll() == None :
92             sleep(0.2)
93         check_Rresult(self.parent, pid)
94         if self.dlg :
95             self.dlg.Destroy()
96
97     def print_result(self) :
98         for key in self.result :
99             if key != 'glob' :
100                 dico = self.result[key]
101                 toprint = [[dico[val][0],`dico[val][1]`, dico[val][2]] for val in dico]
102                 with open(self.pathout['%s.csv' % key], 'w') as f :
103                     f.write('\n'.join([';'.join([val for val in ligne]) for ligne in toprint]).encode(self.parent.syscoding))
104             else :
105                 with open(self.pathout['%s.txt' % 'glob'], 'w') as f :
106                     f.write(self.result['glob'].encode(self.parent.syscoding))