Example #1
0
def main ( file ):
	"""Fonction principale du programme"""
	
	# taille des n-grammes de caractères contigus
	NG_CHAR = 3	
	t = time.time()
	
	# Lecture du fichier, détection de son encoding et encodage en unicode
	raw_data = html.read( string.join(file, '') )      
	encoding, u_data = html.encoding( raw_data )
	
	# Si l'encoding n'est pas trouvé, on écourte le traitement de la page
	if encoding == None:
		return None
		
	# Extraction des différentes parties du fichier html + nétoyage des balises
	u_header, u_title, u_body, u_footer = html.extract_parts( u_data )
	u_body, u_body_clean, u_balises = html.clean( tools.decode_html_entities(u_body) )
	
	# Liste les mots et caractères et fait leurs effectifs
	u_list_words = tools.list_words( u_body_clean )
	u_dict_effect = tools.list_2_dict_effectif( u_list_words )	
	u_list_chars = tools.list_chars( u_dict_effect )
	u_char_effect = tools.char_effect( u_dict_effect )

	# Calcul la loi de Zipf sur les mots et les caractères	
	u_zipf = tools.dict_effectif2distrib_zipf( u_dict_effect )
	u_zipf_char = tools.dict_effectif2distrib_zipf( u_char_effect )
	
	# Trouve les groupes de mots et caractères contigus du texte
	u_adjoining_words = adjoining_words( u_list_words, 2 )
	u_adjoining_char = adjoining_words( u_list_chars, NG_CHAR, NG_CHAR )
		
	# Trouve des mot clefs du texte et fait des statistiques sur le nombre de mots
	u_tags = tag_cloud( u_zipf ) if u_zipf else []
	nbword = tools.word_stat( u_dict_effect ) if u_zipf else [0, 0, 0]
			
	# Sauvegarde les n-grammes de caractères dans un fichier texte
	if NG_CHAR in u_adjoining_char:
		with open(file[0] + file[1] + "_adjchars.txt", "w") as f:
			for char in u_adjoining_char[NG_CHAR]:
				f.write( "%s : %s \n"%( string.join(char[0], '').encode("utf-8"), str(char[1]) ) )
			
	# Sauvegarde les graphs sur la loi de zipf, sur le rang x effectif et sur longueur / effectif
	plot.save_zipf( file, "_word", u_zipf, "f(rang) = effectif" )
	plot.save_zipf( file, "_char", u_zipf_char, "f(rang) = effectif" )
	plot.save_rangeffectif( file, u_zipf )
	plot.save_leneffectif( file, u_zipf )
	
	# Sauvegarde le fichier colorié
	html.write(	file[0] + file[1] + "_color.html",
				u_header,
				color(u_adjoining_words, u_body, u_balises),
				u_footer,
				encoding )
	
	# Sauvegarde le fichier de statistique
	output.stat( file, u_title, u_tags, nbword, u_zipf, u_zipf_char, t )	
	
	return u_dict_effect
Example #2
0
					u_effect_lang = tools.add_defdict( u_effect_lang, ret )
				
			if navig: output.nav_lang( dirname+"/", lang, files[lang] )
				
			# traitement des données sur le corpus mono-langue
			u_char_effect = tools.char_effect( u_effect_lang )
			u_zipf = tools.dict_effectif2distrib_zipf( u_effect_lang )
			u_zipf_char = tools.dict_effectif2distrib_zipf( u_char_effect )
			nbword = tools.word_stat( u_effect_lang ) if u_zipf else [0, 0, 0]
			
			plot.save_zipf( path, "_word", u_zipf, "f(rang) = effectif" )
			plot.save_zipf( path, "_char", u_zipf_char, "f(rang) = effectif" )
			plot.save_rangeffectif( path, u_zipf )
			plot.save_leneffectif( path, u_zipf )
			
			output.stat( path, "Statistique de la langue: " + lang, [], nbword, u_zipf, u_zipf_char, t2 )	
		
		if navig: output.nav_princ( dirname+"/", langs )
		shutil.copy( "jquery-1.6.4.min.js", dirname )
		
		print ""
		print "Traitement fini en: %ss"%( str(time.time() - t)[0:5] )
		
	elif len(sys.argv) == 2: 
		if os.path.isfile(sys.argv[1]): # Passage en argument d'un fichier seul
			file = os.path.splitext(sys.argv[1])
			f = list(os.path.split(file[0]))
			if f[0] != "":
				f[0] = f[0] + '/' 
				
			print "Traitement en cours: " + sys.argv[1]