X-Git-Url: http://www.iramuteq.org/git?a=blobdiff_plain;f=tools.py;h=1fbc9d1846328d5ccdae19916098fa6e3b6fea95;hb=refs%2Fheads%2F3.0;hp=7db0e9e20ea092d0ac2f5e4d7c8531e698fce4e2;hpb=adc24190c47d42d5fa02f074516fc331950d8c5c;p=iramuteq diff --git a/tools.py b/tools.py index 7db0e9e..1fbc9d1 100644 --- a/tools.py +++ b/tools.py @@ -1,23 +1,41 @@ -#!/bin/env python # -*- coding: utf-8 -*- #Author: Pierre Ratinaud -#Copyright (c) 2008-2013, Pierre Ratinaud -#Lisense: GNU GPL +#Copyright (c) 2008-2020 Pierre Ratinaud +#modification pour python 3 : Laurent Mérat, 6x7 - mai 2020 +#License: GNU/GPL +#------------------------------------ +# import des modules python +#------------------------------------ import codecs import os + +#------------------------------------ +# import des modules wx +#------------------------------------ +import wx + +#------------------------------------ +# import des fichiers du projet +#------------------------------------ from dialog import ExtractDialog from corpus import Corpus, copycorpus -import wx parametres = {'filein' : 'corpus/lru2.txt', 'encodein' : 'utf8', 'encodeout' : 'utf8', - 'mods' : [u'*annee_2010', u'*annee_2011']} + 'mods' : ['*annee_2010', '*annee_2011']} + def istext(line) : - if line.startswith(u'**** ') : + if line.startswith('**** ') : + return True + else : + return False + +def isthem(line): + if line.startswith('-*') : return True else : return False @@ -27,7 +45,7 @@ def testvar(line, variable) : varmod = [val.split('_') for val in line[1:]] vars = [var[0] for var in varmod] if variable in vars : - return '_'.join([variable, varmod[vars.index(variable)][1]]).replace(u'*','') + return '_'.join([variable, varmod[vars.index(variable)][1]]).replace('*','') else : return False @@ -35,11 +53,12 @@ def testmod(line, mods) : line = line.split() for mod in mods : if mod in line[1:] : - return mod.replace(u'*','') + return mod.replace('*','') return False class Extract : + def __init__(self, parent, option) : dial = ExtractDialog(parent, option) dial.CenterOnParent() @@ -48,14 +67,20 @@ class Extract : parametres = dial.make_param() if option == 'splitvar' : SplitFromVar(parametres) - else : + elif option == 'mods' : ExtractMods(parametres) - dial.Destroy() - dial = wx.MessageDialog(parent, 'Done !', style = wx.OK) - dial.ShowModal() - dial.Destroy() + elif option == 'them' : + SplitFromThem(parametres) + dial.Destroy() + dial = wx.MessageDialog(parent, 'Done !', style = wx.OK) + dial.ShowModal() + dial.Destroy() + else : + dial.Destroy() + class SplitFromVar : + def __init__(self, parametres) : self.filein = parametres['filein'] self.var = parametres['var'] @@ -68,23 +93,63 @@ class SplitFromVar : keepline = False filedict = {} with codecs.open(self.filein, 'r', self.encodein) as fin : - for line in fin : - if istext(line) : - varmod = testvar(line, self.var) - if varmod : - keepline = True - if varmod not in filedict : - filename = os.path.join(self.basepath, varmod + '.txt') - filedict[varmod] = open(filename, 'w') - fileout = filedict[varmod] - else : - keepline = False - if keepline : - fileout.write(line.encode(self.encodeout)) + for line in fin : + if istext(line) : + varmod = testvar(line, self.var) + if varmod : + keepline = True + if varmod not in filedict : + filename = os.path.join(self.basepath, varmod + '.txt') + filedict[varmod] = open(filename, 'w') + fileout = filedict[varmod] + else : + keepline = False + if keepline : + fileout.write(line) for f in filedict : filedict[f].close() + +class SplitFromThem : + + def __init__(self, parametres) : + self.filein = parametres['filein'] + self.them = parametres['them'] + self.encodein = parametres['encodein'] + self.encodeout = parametres['encodeout'] + self.basepath = os.path.dirname(self.filein) + self.pathout = os.path.join(self.basepath, '_'.join([them.replace('-*','') for them in self.them])) + self.fileout = open(self.pathout, 'w') + self.doparse() + self.fileout.close() + + def doparse(self): + text = '' + keepline = False + lastet = '' + with codecs.open(self.filein, 'r', self.encodein) as fin : + for line in fin : + if istext(line) : + self.writetext(self.fileout, lastet, text) + text = '' + lastet = line + if isthem(line) : + l = line.strip().rstrip('\n\r') + if l in self.them : + keepline = True + else : + keepline = False + if keepline : + text += line + self.writetext(self.fileout, lastet, text) + + def writetext(self, fileout, lastet, text): + if text != '' : + self.fileout.write(lastet + text) + + class ExtractMods : + def __init__(self, parametres) : self.onefile = parametres.get('onefile', False) self.filein = parametres['filein'] @@ -93,7 +158,7 @@ class ExtractMods : self.encodeout = parametres['encodeout'] self.basepath = os.path.dirname(self.filein) if self.onefile : - filename = os.path.join(self.basepath, '_'.join([mod.replace(u'*','') for mod in self.mods])+'.txt') + filename = os.path.join(self.basepath, '_'.join([mod.replace('*','') for mod in self.mods])+'.txt') self.fileout = open(filename, 'w') self.doparse() @@ -101,22 +166,22 @@ class ExtractMods : keepline = False filedict = {} with codecs.open(self.filein, 'r', self.encodein) as fin : - for line in fin : - if istext(line) : - modinline = testmod(line, self.mods) - if modinline : - keepline = True - if not self.onefile : + for line in fin : + if istext(line) : + modinline = testmod(line, self.mods) + if modinline : + keepline = True + if not self.onefile : if modinline not in filedict : filename = os.path.join(self.basepath, modinline + '.txt') filedict[modinline] = open(filename, 'w') fileout = filedict[modinline] - else : - fileout = self.fileout - else : - keepline = False - if keepline : - fileout.write(line.encode(self.encodeout)) + else : + fileout = self.fileout + else : + keepline = False + if keepline : + fileout.write(line) if not self.onefile : for f in filedict : filedict[f].close() @@ -125,6 +190,7 @@ class ExtractMods : class SubCorpus(Corpus) : + def __init__(self, parent, corpus, sgts) : Corpus.__init__(self, parent, corpus.parametres) self.sgts = sgts @@ -142,7 +208,7 @@ class SubCorpus(Corpus) : self.formes = {} for forme in self.corpus.formes : sgtseff = self.corpus.getformeuceseff(forme) - sgts = set(self.sgts).intersection(sgtseff.keys()) + sgts = set(self.sgts).intersection(list(sgtseff.keys())) if len(sgts) : self.formes[forme] = self.corpus.formes[forme] self.formes[forme].freq = sum([sgtseff[sgt] for sgt in sgts]) @@ -151,10 +217,18 @@ class SubCorpus(Corpus) : return list(set(self.sgts).intersection(self.corpus.getlemuces(lem))) +def converttabletocorpus(table, fileout, enc='UTF8') : + var = table.pop(0) + var = var[0:len(var)-1] + print(var) + et = [list(zip(var, line[0:len(line)-1])) for line in table] + et = ['**** ' + ' '.join(['*' + '_'.join(val) for val in line]) for line in et] + txt = ['\n'.join([et[i], line[-1]]) for i, line in enumerate(table)] + print('\n'.join(txt)) + #with open(fileout, 'w') as f : - - +# execution directe ??? if __name__ == '__main__' : #SplitFromVar(parametres) ExtractMods(parametres, True)