def twittear_hilo(parametros): # tw_intro = {'texto': "Análisis de discurso del string_fecha de #MauricioMacri.", 'media': ['intro0.png','intro1.png']} # tw_terminos = { 'texto': '@dicenlospresis tweet con terminos', 'media': ["dlp_terminos.png"] } # tw_verbos = {'texto': '@dicenlospresis tweet con verbos', 'media': ["dlp_verbos.png"]} # utiles.twittear_hilo([tw_intro, tw_terminos, tw_verbos], cuenta="dlp") # return fecha = parametros['fecha'] kiosco = Kiosco() textos = [ noticia['texto'] for noticia in kiosco.noticias( diario='casarosada', categorias='', fecha=fecha) ] if len(textos) == 0: return nlp = NLP() nlp.separador = '' for texto in textos: # tw_intro = tweet_intro(texto, string_fecha) # ACA ESTA EL ERROR; STRING FECHA ESTA EN FORMADO MM.DD.YYYY, DEBERIA ESTAR EN YYYY.MM.DD tw_intro = tweet_intro(texto, fecha) kiosco = Kiosco() top_terminos = nlp.top_terminos(textos=[texto], n=15) top_verbos = nlp.top_verbos(textos=[texto], n=15) tw_terminos = tweet_terminos(top_terminos) tw_verbos = tweet_verbos(top_verbos) if parametros['twittear']: utiles.twittear_hilo([tw_intro, tw_terminos, tw_verbos], cuenta="dlp")
def top_verbos(parametros): fecha = parametros['fecha'] top_max = parametros['top_max'] medios = set(parametros['medios']) categorias = parametros['categorias'] twittear = parametros['twittear'] solo_titulos = parametros['solo_titulos'] kiosco = Kiosco() with open('medios/diarios/config.yaml', 'r') as stream: try: config = yaml.safe_load(stream) except yaml.YAMLError as exc: print(exc) nlp = NLP() for diario in config['diarios']: tag = diario['tag'] if tag not in medios and len(medios) > 0: continue twitter = diario['twitter'] textos = [] contenido = "las noticias" if solo_titulos: textos = [ noticia['titulo'] for noticia in kiosco.noticias( diario=tag, categorias=categorias, fecha=fecha) ] contenido = "los títulos" else: textos = [ noticia['titulo'] + " " + noticia['titulo'] + " " + noticia['texto'] for noticia in kiosco.noticias( diario=tag, categorias=categorias, fecha=fecha) ] print("tag: " + tag + " textos: " + str(len(textos))) if len(textos) == 0: continue string_fecha = "" if type(fecha) is dict: string_fecha = fecha['desde'].strftime( "%d.%m.%Y") + " al " + fecha['hasta'].strftime("%d.%m.%Y") else: string_fecha = fecha.strftime("%d.%m.%Y") texto = "Tendencias en " + contenido + " de " + twitter + " del " + string_fecha + "\n" top_100 = nlp.top_verbos(textos, n=top_max) i = 0 for nombre, m in top_100: linea = "" i += 1 if i >= 10: linea = str(i) + ". #" + nombre + " " + str(m) + "\n" texto += linea break else: linea = str(i) + ". #" + nombre + " " + str(m) + "\n" if twittear and len(texto) + len(linea) > 220: break else: texto += linea print(texto)
def top_todo(parametros): fecha = parametros['fecha'] top_max = parametros['top_max'] medios = set(parametros['medios']) categorias = parametros['categorias'] twittear = parametros['twittear'] solo_titulos = parametros['solo_titulos'] kiosco = Kiosco() with open('medios/diarios/config.yaml', 'r') as stream: try: config = yaml.safe_load(stream) except yaml.YAMLError as exc: print(exc) nlp = NLP() nlp.separador = '' for diario in config['diarios']: tag = diario['tag'] if tag not in medios and len(medios) > 0: continue hashtag = diario['hashtag'] textos = [] contenido = "las noticias" if solo_titulos: textos = [ noticia['titulo'] for noticia in kiosco.noticias( diario=tag, categorias=categorias, fecha=fecha) ] contenido = "los títulos" else: textos = [ noticia['titulo'] + " " + noticia['titulo'] + " " + noticia['texto'] for noticia in kiosco.noticias( diario=tag, categorias=categorias, fecha=fecha) ] print("tag: " + tag + " textos: " + str(len(textos))) if len(textos) == 0: continue string_fecha = "" if type(fecha) is dict: string_fecha = fecha['desde'].strftime( "%d.%m.%Y") + " al " + fecha['hasta'].strftime("%d.%m.%Y") else: string_fecha = fecha.strftime("%d.%m.%Y") categorias = ["#" + c for c in categorias] secciones = "" if len(categorias) > 0: secciones = " de " + " y ".join( [", ".join(categorias[:-1]), categorias[-1]] if len(categorias) > 2 else categorias) texto = "Tendencias en " + contenido + secciones + " de " + hashtag + " del " + string_fecha + "\n" top_todo = nlp.top(textos, n=top_max) i = 0 for nombre, m in top_todo: linea = "" i += 1 if i >= 10: linea = str(i) + ". #" + nombre + " " + str(m) + "\n" texto += linea break else: linea = str(i) + ". #" + nombre + " " + str(m) + "\n" if twittear and len(texto) + len(linea) > 220: break else: texto += linea print(texto) if twittear: path_imagen = tag + ".png" utiles.nube_de_palabras(path=path_imagen, data=dict(top_todo)) utiles.twittear(texto=texto, path_imagen=path_imagen, cuenta="dlm")
def top_personas(self, textos, n=10): if self.bigramas == None and self.trigramas == None: # si todavia no se calcularon los ngramas, los calculo # recupero las noticias deL ultimo dia para armar los bigramas hasta = datetime.datetime.now() desde = hasta - datetime.timedelta(days=1) kiosco = Kiosco() set_entrenamiento = [noticia['titulo'] + " " + noticia['texto'] for noticia in kiosco.noticias(fecha={'desde':desde, 'hasta':hasta})][:200] personas = self.__bolsa_de_personas__(set_entrenamiento) self.__entrenar_bigramas__(set_entrenamiento=personas) self.__entrenar_trigramas__(set_entrenamiento=personas) personas = self.__bolsa_de_personas__(textos) personas_con_bigramas = self.bigramas[personas] personas_con_trigramas = self.trigramas[personas_con_bigramas] personas_freq_tri = defaultdict(int) for sent in personas_con_trigramas: for i in sent: personas_freq_tri[i] += 1 # top_tri = {k: personas_freq_tri[k] for k in sorted(personas_freq_tri, key=personas_freq_tri.get, reverse=True)} # lo limito a 50 porque estan los relevantes, mas alla del 100 hay basura list_top_tri_ordenado = [(k, personas_freq_tri[k]) for k in sorted(personas_freq_tri, key=personas_freq_tri.get, reverse=True)][:50] nombres_a_borrar = [] for nombre, freq in list_top_tri_ordenado: for nombre_2, freq_2 in list_top_tri_ordenado: if nombre != nombre_2 and nombre in nombre_2: personas_freq_tri[nombre_2] += freq if nombres_a_borrar.count(nombre) == 0: nombres_a_borrar.append(nombre) break for nombre in nombres_a_borrar: del personas_freq_tri[nombre] list_top_tri = [(k, personas_freq_tri[k]) for k in sorted(personas_freq_tri, key=personas_freq_tri.get, reverse=True) if k.count("_") > 0 and k.count("_") < 3][:n] return [(concepto.replace('_', self.separador), numero) for concepto, numero in list_top_tri]