2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2008-2012 Pierre Ratinaud
7 from chemins import 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 *
18 from time import sleep
24 logger = logging.getLogger('iramuteq.textstat')
28 class Stat(AnalyseText) :
32 def preferences(self) :
33 return self.parametres
37 if not 'dlg' in dir(self) :
38 self.dlg = progressbar(self, 7)
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)
53 supp = [[i, val] for i, val in enumerate(supp)]
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))
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)))
69 self.dlg.Update(7, u'Ecriture...')
70 self.result['glob'] = txt
75 tot <- read.csv2("%s", header = FALSE, row.names = 1)
76 """ % (ffr(self.parent.RscriptsPath['Rgraph']), ffr(self.pathout['total.csv']))
79 hapax <- read.csv2("%s", header = FALSE, row.names = 1)
80 tot <- rbind(tot, hapax)
81 """ % ffr(self.pathout['hapax.csv'])
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)
86 """ % (ffr(self.pathout['zipf.png']))
87 tmpscript = tempfile.mktemp(dir=self.parent.TEMPDIR)
88 with open(tmpscript, 'w') as f :
90 pid = exec_rcode(self.parent.RPath, tmpscript, wait = False)
91 while pid.poll() == None :
93 check_Rresult(self.parent, pid)
97 def print_result(self) :
98 for key in self.result :
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))
105 with open(self.pathout['%s.txt' % 'glob'], 'w') as f :
106 f.write(self.result['glob'].encode(self.parent.syscoding))