def controlFun(self): "processes selected files, clears report, shows results in a report" # progressbar if len(self.root.fileStorage.arenafiles) > 1: self.stoppedProcessing = False self.progressWindow = ProgressWindow(self, len(self.root.fileStorage.arenafiles), text = "controlled") # initialization controls = self.controlFrame.controlsGet() # selected controls self.controlReport.clear(self) # clears report self.controlReport.addControls(controls) # adds selected controls to ControlReport self.problemOccured = False # processing for file in self.root.fileStorage.arenafiles: try: if file in self.root.fileStorage.pairedfiles: cm = CM(file, nameR = self.root.fileStorage.pairedfiles[file]) else: cm = CM(file, nameR = "auto") except Exception: self.problemOccured = True for control in controls: self.controlReport.addFile((control[0], (file, "Failed to load!", "Problem", 9999999))) else: for control in controls: try: assessment = self.assessImportance(cm = cm, control = control, file = file) except Exception: self.problemOccured = True assessment = (control[0], (file, "Failed to compute!", "Problem", 9999998)) self.controlReport.addFile(assessment) if len(self.root.fileStorage.arenafiles) > 1: if self.stoppedProcessing: return else: self.progressWindow.addOne() self.controlReport.updateTree(self) # progressbar and status if len(self.root.fileStorage.arenafiles) > 1: self.progressWindow.destroy() if self.problemOccured: self.status.set("Files were not processed successfully!") self.bell() else: self.status.set("Files were processed successfully.") else: if self.problemOccured: self.status.set("File was not processed successfully!") self.bell() else: self.status.set("File was processed successfully.")
def addReflections(self, file, points): "add reflection points from a set in the 'points' argument for a file in the argument" if file in self.addedReflections: self.addedReflections[file].update(points) else: self.addedReflections[file] = points if file in self.reflections: self.reflections[file].update(points) else: if file in self.pairedfiles: cm = CM(file, nameR=self.pairedfiles[file]) else: cm = CM(file, nameR="auto") reflections = cm.findReflections(results="indices") self.reflections[file] = set(reflections[0]) | set( reflections[1]) | points
def main(): from cm import CM import os import os.path svg = SVG(300, 300) cm = CM(os.path.join(os.getcwd(), "TestingFiles", "14rNO465_Arena.dat")) svg.drawAAPA(cm, "room", scale=3, boundary=True, sector=True, shocks=True) svg.save("test.svg") os.startfile("test.svg")
def postear_en_dlm(self, fecha, diario, secciones): resultados = Resultados() visu = Visualizador() tolkien = Escritor() if '-' in secciones: secciones = secciones.split('-') else: secciones = [secciones] freqs = resultados.frecuencias(fecha=fecha, diario=diario, secciones=secciones, top=20, verbos=False) if not bool(freqs): return path_imagen = os.getcwd() + '/imagenes/' + diario + '-todo.png' visu.nube(path_imagen, freqs, con_espacios=False) texto = tolkien.tweet_tendencias(freqs=freqs, fecha=fecha, diario=diario) textos_e_imagenes = [{'media': [path_imagen], 'texto': texto}] for s in secciones: freqs = resultados.frecuencias(fecha=fecha, diario=diario, secciones=s, top=20, verbos=False) if not bool(freqs): continue path_imagen = os.getcwd( ) + '/imagenes/' + diario + '-' + s + '.png' visu.nube(path_imagen, freqs, con_espacios=False) texto = tolkien.tweet_tendencias(freqs=freqs, fecha=fecha, diario=diario, secciones=s) textos_e_imagenes.append({'media': [path_imagen], 'texto': texto}) cm = CM() cm.twittear_hilo('dicenlosmedios', textos_e_imagenes)
def processFun(self): "processes chosen files and saves the results in the save-to file" # checking for mistakes try: self.processCheck() except Exception as e: self.bell() self.status.set(e) return # files to be processed self.filesToProcess = [file for file in self.root.fileStorage.arenafiles if \ self.optionFrame.processFile(file)] # progressWindow and check for number of files for processing if len(self.filesToProcess) > 1: self.stoppedProcessing = False self.progressWindow = ProgressWindow(self, len(self.filesToProcess)) elif len(self.filesToProcess) == 0: self.bell() self.status.set("There is no file selected for processing!") return # selected methods methods = [] for method in Parameters().parameters: if eval("self.parametersF.%sVar.get()" % (method[0].replace(" ", ""))): methods.append(method[0]) output = self.saveToFrame.saveToVar.get() startTime = float(self.timeFrame.startTimeVar.get()) time = float(self.timeFrame.timeVar.get()) separator = optionGet("ResultSeparator", ",", "str") results = separator.join(["File"] + methods) if self.optionFrame.saveTags.get(): results += separator + "Tags" self.log = Log(methods, startTime, time, self.filesToProcess, self.root.fileStorage, self.optionFrame.removeReflectionsWhere.get(), output) self.someProblem = False developer = optionGet("Developer", False, 'bool') for file in self.filesToProcess: # loading of cm object if methods: try: if file in self.root.fileStorage.pairedfiles: cm = CM(file, nameR=self.root.fileStorage.pairedfiles[file]) else: cm = CM(file, nameR="auto") if self.optionFrame.removeReflections(file): cm.removeReflections( points=self.root.fileStorage.reflections.get( file, None)) except Exception as e: if developer: print(e) filename = returnName( filename=file, allFiles=self.root.fileStorage.arenafiles) results += "\n" + filename + "{}NA".format( separator) * len(methods) self.log.failedToLoad.append(file) self.someProblem = True continue result = [] for method in Parameters().parameters: if method[0] in methods: try: if method[2] == "custom": exec("from Stuff.Parameters import {}".format( method[5])) result.append(eval(method[1])) except Exception as e: if developer: print(e) result.append("NA") self.log.methodProblems[method[0]].append(file) self.someProblem = True result = separator.join(map(str, result)) if methods: result = separator + result filename = returnName(filename=file, allFiles=self.root.fileStorage.arenafiles) results += "\n" + filename + result if self.optionFrame.saveTags.get(): # tag inclusion in results if file in self.root.fileStorage.tagged: results += separator + "1" else: results += separator + "0" if len(self.filesToProcess) > 1: if self.stoppedProcessing: writeResults(output, results) self.log.stopped = file self.log.writeLog() self.status.set("Processing stopped") return else: self.progressWindow.addOne() writeResults(output, results) self.log.writeLog() # change of status bar and closing of progressWindow if len(self.filesToProcess) > 1: if self.someProblem: self.status.set("Files were processed.") else: self.status.set("Files were processed successfully.") self.progressWindow.destroy() else: if self.someProblem: self.status.set("File was processed.") else: self.status.set("File was processed successfully.") # removal of files from fileStorage if selected if self.optionFrame.clearFilesAfterProcessing.get(): for file in self.filesToProcess: self.root.fileStorage.arenafiles.remove(file) self.fileStorageFrame.chosenVar.set( len(self.root.fileStorage.arenafiles)) if not self.root.fileStorage.arenafiles: self.fileStorageFrame.removeFiles.state(["disabled"]) self.process.state(["disabled"]) if self.someProblem: ProcessingProblemDialog(self, self.log.filename)
def processFun(self): "processes chosen files and saves the results in the save-to file" # files to be processed self.filesToProcess = [file for file in self.fileStorage if \ self.optionFrame.processFile(file)] # checking for mistakes try: self.processCheck() except Exception as e: self.bell() self.status.set(e) return # progressWindow if len(self.filesToProcess) > 1: self.stoppedProcessing = False self.progressWindow = ProgressWindow(self, len(self.filesToProcess)) # selected methods methods = [] for method in Parameters().parameters: if eval("self.parametersF.%sVar.get()" % (method[0].replace(" ", ""))): methods.append(method[0]) # initializations output = self.saveToFrame.saveToVar.get() startTime = float(self.timeFrame.startTimeVar.get()) time = float(self.timeFrame.timeVar.get()) separator = optionGet("ResultSeparator", ",", "str") batchTime = self.selectedBatchTime if self.useBatchTimeVar.get( ) else None self.log = Log(methods, startTime, time, self.filesToProcess, self.fileStorage, self.optionFrame.removeReflectionsWhere.get(), output, batchTime=batchTime) self.someProblem = False developer = optionGet("Developer", False, 'bool') # results header if self.useBatchTimeVar.get(): info = [ method[0] for method in Parameters().parameters if method[2] == "info" ] results = ["File"] for method in methods: if method in info: results.append(method) else: results.extend([ method + " ({}-{})".format(start, end) for start, end in self.selectedBatchTime ]) results = separator.join(results) else: results = separator.join(["File"] + methods) if self.optionFrame.saveTags.get(): results += separator + "Tags" if self.optionFrame.saveComments.get(): results += separator + "Comment" # computing of results for file in self.filesToProcess: # loading of cm object if methods: try: if file in self.fileStorage.pairedfiles: cm = CM(file, nameR=self.fileStorage.pairedfiles[file]) else: cm = CM(file, nameR="auto") if self.optionFrame.removeReflections(file): cm.removeReflections( points=self.fileStorage.reflections.get( file, None)) except Exception as e: if developer: print(e) filename = returnName(filename=file, allFiles=self.fileStorage.arenafiles) results += "\n" + filename + "{}NA".format( separator) * len(methods) self.log.failedToLoad.append(file) self.someProblem = True continue result = [] for method in Parameters().parameters: if method[0] in methods: if self.useBatchTimeVar.get(): for startTime, time in self.selectedBatchTime: try: if method[2] == "custom": exec("from Stuff.Parameters import {}". format(method[5])) result.append(eval(method[1])) if method[2] == "info": break except Exception as e: if developer: print(e) result.append("NA") self.log.methodProblems[method[0]].append(file) self.someProblem = True else: try: if method[2] == "custom": exec("from Stuff.Parameters import {}".format( method[5])) result.append(eval(method[1])) except Exception as e: if developer: print(e) result.append("NA") self.log.methodProblems[method[0]].append(file) self.someProblem = True result = separator.join(map(str, result)) if methods: result = separator + result filename = returnName(filename=file, allFiles=self.fileStorage.arenafiles) results += "\n" + filename + result # tag inclusion in results if self.optionFrame.saveTags.get(): if file in self.fileStorage.tagged: results += separator + "1" else: results += separator + "0" # comment inclusion in results if self.optionFrame.saveComments.get(): results += separator + self.fileStorage.comments[file] # progress window update if len(self.filesToProcess) > 1: if self.stoppedProcessing: writeResults(output, results) self.log.stopped = file self.log.writeLog() self.status.set("Processing stopped") return else: self.progressWindow.addOne() # results and log writing, ending of processing writeResults(output, results) self.log.writeLog() self._setStatusEndProgressWindow() if self.someProblem: ProcessingProblemDialog(self, self.log.filename) elif self.optionFrame.showResults.get(): os.startfile(output)
def _initializeCM(self, file): "initializes self.cm" if file in self.fileStorage.pairedfiles: self.cm = CM(file, nameR=self.fileStorage.pairedfiles[file]) else: self.cm = CM(file, nameR="auto")
def postear_contador(self, fecha, concepto): resultados = Resultados() tolkien = Escritor() terminos = { 'cfk': [ 'CFK', 'Kirchner', 'Cristina', 'Cristina Kirchner', 'Cristina Fernández', 'Cristina Fernández de Kirchner', 'Cristina Fernández De Kirchner', 'Vicepresidenta' ], 'dolar': ['dólar', 'US$', 'Dólar'], 'corrupcion': ['corrupción', 'corrupto', 'Corrupción'], 'larreta': [ 'Larreta', 'Horacio Larreta', 'Rodríguez Larreta', 'Horacio Rodríguez', 'Horacio Rodríguez Larreta' ], 'venezuela': [ 'Venezuela', 'Nicolás Maduro', 'Maduro', 'Chávez', 'Hugo Chávez', 'Chavez', 'Hugo Chavez' ], 'inseguridad': ['inseguridad', 'inseguro'], 'miedo': ['miedo', 'muerte', 'muerto', 'muerta', 'asesinato', 'horror'] } tabla = {} for medio in [ medio for medio in tolkien.hashtags.keys() if medio != 'casarosada' ]: freqs = resultados.frecuencias(fecha=fecha, diario=medio, verbos=False, top=2000) if not bool(freqs): continue total = sum( [f[1] for f in freqs.items() if f[0] in terminos[concepto]]) if total == 0: continue tabla[medio] = total if len(tabla) == 0: return ordenada = { k: v for k, v in sorted( tabla.items(), key=lambda item: item[1], reverse=True) } texto = tolkien.tweet_contador(concepto=concepto, freqs=ordenada, fecha=fecha) cm = CM() cm.twittear_texto('dicenlosmedios', texto)
def postear_en_discursoshistoricos(self, cuenta, fecha=None): resultados = Resultados() kiosco = Kiosco() visu = Visualizador() tolkien = Escritor() presidente, subfijopng, hashtag = self.info_cuenta(cuenta) # recupero frecuencias al azar freqs = resultados.bd.frecuencias_discursos.aggregate([{ '$match': { 'presidente': presidente } }, { '$sample': { 'size': 1 } }]) if not bool(freqs): return cm = CM() for freq in freqs: # recupero texto del discurso discurso = kiosco.noticias(diario='casarosada', url=freq['url'])[0] # armo tweet con el discurso en imagenes texto = " ".join( re.split("\s+", discurso['texto'], flags=re.UNICODE)) paths_imagenes = visu.texto_en_imagenes( texto, 'calibri.ttf', 17, 800, 600, os.getcwd() + "/imagenes/intro" + subfijopng) # TODO: la fecha esta MAL. pasar de date a string. tw_intro = { 'texto': "Discurso del " + tolkien.separar_fecha(fecha=freq['fecha']) + " de " + hashtag + ". Hilo 👇", 'media': paths_imagenes } # armo textos del tweet txt_sustantivos = tolkien.texto_tweet_sustantivos_discurso( freq['sustxt']) txt_adjetivos = tolkien.texto_tweet_adjetivos_discurso( freq['adjtxt']) # armo tweet con top 15 de terminos path_imagen_sustantivos = os.getcwd( ) + '/imagenes/sustantivos_discurso' + subfijopng + '.png' etiquetas_sustantivos = [ nombre for nombre, m in freq['sustxt'].items() ][:15] data_sustantivos = [m for nombre, m in freq['sustxt'].items()][:15] visu.lollipop(path=path_imagen_sustantivos, colormap=visu.cmap_del_dia(), titulo="Frecuencia de sustantivos", etiquetas=etiquetas_sustantivos, unidad="cantidad de apariciones", valfmt="{x:.0f}", data=data_sustantivos) tw_terminos = { 'texto': txt_sustantivos, 'media': [path_imagen_sustantivos] } # armo tweet con top 15 de verbos path_imagen_adjetivos = os.getcwd( ) + '/imagenes/adjetivos_discurso' + subfijopng + '.png' etiquetas_adjetivos = [ nombre for nombre, m in freq['adjtxt'].items() ][:15] data_adjetivos = [m for nombre, m in freq['adjtxt'].items()][:15] visu.lollipop(path=path_imagen_adjetivos, colormap=visu.cmap_del_dia(), titulo="Frecuencia de adjetivos", etiquetas=etiquetas_adjetivos, unidad="cantidad de apariciones", valfmt="{x:.0f}", data=data_adjetivos) tw_verbos = { 'texto': txt_adjetivos, 'media': [path_imagen_adjetivos] } # el CM twittea cm.twittear_hilo(cuenta, [tw_intro, tw_terminos, tw_verbos])
def postear_en_discursosdeaf(self, fecha): resultados = Resultados() kiosco = Kiosco() visu = Visualizador() tolkien = Escritor() # recupero frecuencias del discurso freqs = resultados.bd.frecuencias_discursos.aggregate([{ '$match': { 'presidente': 'alberto', 'fecha': fecha } }]) if not bool(freqs): return cm = CM() for freq in freqs: # recupero texto del discurso discurso = kiosco.noticias(diario='casarosada', url=freq['url'])[0] # armo tweet con el discurso en imagenes texto = " ".join( re.split("\s+", discurso['texto'], flags=re.UNICODE)) paths_imagenes = visu.texto_en_imagenes( texto, 'calibri.ttf', 17, 800, 600, os.getcwd() + "/imagenes/introaf") tw_intro = { 'texto': "Discurso del " + tolkien.separar_fecha(fecha=freq['fecha']) + " de #AlbertoFernández. Hilo 👇", 'media': paths_imagenes } # armo textos del tweet txt_sustantivos = tolkien.texto_tweet_sustantivos_discurso( freq['sustxt']) txt_adjetivos = tolkien.texto_tweet_adjetivos_discurso( freq['adjtxt']) # armo tweet con top 15 de terminos path_imagen_sustantivos = os.getcwd( ) + '/imagenes/sustantivos_discursoaf.png' etiquetas_sustantivos = [ nombre for nombre, m in freq['sustxt'].items() ][:15] data_sustantivos = [m for nombre, m in freq['sustxt'].items()][:15] visu.lollipop(path=path_imagen_sustantivos, colormap=visu.cmap_del_dia(), titulo="Frecuencia de sustantivos", etiquetas=etiquetas_sustantivos, unidad="cantidad de apariciones", valfmt="{x:.0f}", data=data_sustantivos) tw_terminos = { 'texto': txt_sustantivos, 'media': [path_imagen_sustantivos] } # armo tweet con top 15 de verbos path_imagen_adjetivos = os.getcwd( ) + '/imagenes/adjetivos_discursoaf.png' etiquetas_adjetivos = [ nombre for nombre, m in freq['adjtxt'].items() ][:15] data_adjetivos = [m for nombre, m in freq['adjtxt'].items()][:15] visu.lollipop(path=path_imagen_adjetivos, colormap=visu.cmap_del_dia(), titulo="Frecuencia de adjetivos", etiquetas=etiquetas_adjetivos, unidad="cantidad de apariciones", valfmt="{x:.0f}", data=data_adjetivos) tw_verbos = { 'texto': txt_adjetivos, 'media': [path_imagen_adjetivos] } # el CM twittea cm.twittear_hilo('discursosdeaf', [tw_intro, tw_terminos, tw_verbos])