# -*- coding: utf-8 -*-
import codecs
import pyplwn

def print_jsp_header():
	print """<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>"""
	print """<%
	if(langID==0)
		out.println("<p class=\\"stats_header\\">Statystyki Słowosieci (ver. 0.93)</p>");
	else
		out.println("<p class=\\"stats_header\\">plWordNet Statistics (ver. 0.93)</p>");%>
		"""


def print_jsp_methods():
	print """
	<%!
	// empty (for now)
	%>
	"""

def print_wn_numbers_by_pos(db):
	stats = pyplwn.database.WNStatistics(db)

	labels = [ ('Liczba lematów', 'No of lemmas'), ('Liczba jednostek', 'No of LUs'), 
			('Liczba synsetów', 'No of synsets'), ('Lematy monosemiczne', 'Monosemous lemmas'), 
			('Lematy polisemiczne', 'Polysemous lemmas') ]
	noun = stats.get_wn_numbers( [pyplwn.database.DBEnums.get_pos('subst')] )
	verb = stats.get_wn_numbers( [pyplwn.database.DBEnums.get_pos('verb') ] )
	adj  = stats.get_wn_numbers( [pyplwn.database.DBEnums.get_pos('adj')  ] )
	all  = stats.get_wn_numbers( [ pyplwn.database.DBEnums.get_pos('subst'),pyplwn.database.DBEnums.get_pos('verb'),\
			pyplwn.database.DBEnums.get_pos('adj')]   )

	print r"""<table align="center" border="1">
<%
	if(langID==0)
		out.println("<caption>Różne aspekty rozmiaru Słowosieci</caption>");
	else
		out.println("<caption>Different aspects of the plWordNet size</caption>");
	out.print("<tr>");
	if(langID==0)
		out.println("<td></td> <th> Rzeczowniki </th> <th> Czasowniki </th> <th>Przymiotniki  </th> <th> Wszystkie </th> ");
	else
		out.println("<td></td> <th> Nouns </th> <th> Verbs </th> <th>Adjectives  </th> <th> All </th> ");
	out.println("</tr>");"""
	for p in xrange( len(noun) ):
		print """
		if(langID==0)
			out.print("<tr><th>%s</th>");
		else 
			out.print("<tr><th>%s</th>");
		out.print("<td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td></tr>");""" % (labels[p][0], labels[p][1], str(noun[p]), verb[p], adj[p], all[p])
	print "%></table><br>"

def print_avg_polysemy(db):
	stats = pyplwn.database.WNStatistics(db)
	
	poses = [ ( ("Rzeczowniki", "Nouns"), [pyplwn.database.DBEnums.get_pos('subst')] ), 
			  ( ("Czasowniki", "Verbs"), [pyplwn.database.DBEnums.get_pos('verb')] ),
			  ( ("Przymiotniki", "Adjectives"), [pyplwn.database.DBEnums.get_pos('adj')] )
			]
	print r"""<table align="center" border="1">
<%
	if(langID==0)
		out.println("<caption>Średnia polisemia</caption>");
	else
		out.println("<caption>Average polysemy</caption>");

	if(langID==0)
		out.println("<tr><td></td><th>Z monosemicznymi lematami </th> <th> Bez monosemicznych lematów </th></tr>");
	else
		out.println("<tr><td></td><th>Including Monosemous Lemmas </th> <th> Excluding Monosemous Lemmas </th></tr>");"""
	for (l, p) in poses:
		print """
	if(langID==0) 
		out.print("<tr><th>%s</th>");
	else
		out.print("<tr><th>%s</th>");
	out.println( "<td>%s</td><td>%s</td></tr>" ); """ % ( l[0], l[1], stats.get_avg_polysemy(p, True), stats.get_avg_polysemy(p, False) )
	print "%></table><br>"

def print_synset_size_histogram(db):
	stats = pyplwn.database.WNStatistics(db)
	
	poses = [ ( ("Rzeczowniki", "Nouns"), [pyplwn.database.DBEnums.get_pos('subst')] ), 
			  ( ("Czasowniki", "Verbs"), [pyplwn.database.DBEnums.get_pos('verb')] ),
			  ( ("Przymiotniki", "Adjectives"), [pyplwn.database.DBEnums.get_pos('adj')] )
			]
	print r"""<table align="center" border="1">
	<%
	if(langID==0)
		out.println("<caption>Histogram wielkości synsetów</caption>");
	else
		out.println("<caption>Sizes of synsets</caption>");

	if(langID==0)
      out.println("<tr><th></th><th colspan=\"10\"> Procent synsetów zawierających n jednostek leksykalnych [%] </th></tr>");
	else
      out.println("<tr><th></th><th colspan=\"10\"> Percentage of synsets including the n lexical units [%] </th></tr>");
	out.println("<tr><td></td> <td>1 </td><td> 2 </td><td> 3 </td><td> 4 </td><td> 5 </td><td> 6 </td><td> 7 </td><td> 8 </td><td> 9 </td><td> >10 </td></tr>");
"""
	for l,p in poses:
		print ("""
		if(langID==0) out.print("<tr> <th>%s</th>"); 
		else out.print("<tr> <th>%s</th>"); 
		out.print(\"""" % (l[0], l[1]) ),
		h,c = stats.get_synset_size_histogram(p)
		for x in sorted(h):
			print ("<td>%2.2f</td>" % (100 * float(h[x]) / c )).rjust(6),
		print "</tr>\");"

	print "%></table><br>\n"

def print_units_polysemy_histogram(db):
	stats = pyplwn.database.WNStatistics(db)
	poses = [ ( ("Rzeczowniki", "Nouns"), [pyplwn.database.DBEnums.get_pos('subst')] ), 
			  ( ("Czasowniki", "Verbs"), [pyplwn.database.DBEnums.get_pos('verb')] ),
			  ( ("Przymiotniki", "Adjectives"), [pyplwn.database.DBEnums.get_pos('adj')] )
			]
	print r"""<table align="center" border="1">
	<%
	if(langID==0)
		out.println("<caption>Liczba synsetów do których należy jednostka</caption>");
	else
		out.println("<caption>No of synsets which a lemmas belongs to</caption>");
	if(langID==0)
      out.println("<tr><th></th><th colspan=\"10\"> Procent lematów należących do n synsetów [%] </th></tr>");
	else
      out.println("<tr><th></th><th colspan=\"10\"> Percentage of lemmas belonging to the n synsets [%] </th></tr>");
	out.println("<tr><td></td> <td>1 </td><td> 2 </td><td> 3 </td><td> 4 </td><td> 5 </td><td> 6 </td><td> 7 </td><td> 8 </td><td> 9 </td><td> >10 </td></tr>");
"""
	for l,p in poses:
		print ("""
		if(langID==0) out.print("<tr> <th>%s</th>"); 
		else out.print("<tr> <th>%s</th>"); 
		out.print(\"""" % (l[0], l[1]) ),
		h,c = stats.get_units_polysemy_histogram(p)
		for x in sorted(h):
			print ("<td>%2.2f</td>" % (100 * float(h[x]) / c )).rjust(6),
		print "</tr>\");"
	print r"""%></table><br>"""

def print_relations_count(db):
	stats = pyplwn.database.WNStatistics(db)
	noun = pyplwn.database.DBEnums.get_pos('subst')
	verb = pyplwn.database.DBEnums.get_pos('verb')
	adj  = pyplwn.database.DBEnums.get_pos('adj')
	print r"""
	<table align="center" border="1"><%
	if(langID==0)
		out.println("<caption>Statystki relacji</caption>");
	else
		out.println("<caption>Relation statistics</caption>");
	if(langID==0){
		out.println("<tr><th rowspan=\"2\">Relacja</th><th colspan=\"4\"> Liczba instancji </th></tr>");
		out.println("<tr><th>Rzeczowniki</th><th>Czasowniki</th><th>Przymiotniki</th><th>Razem</th></tr>");
	}
	else {
		out.println("<tr><th rowspan=\"2\">Relaction</th><th colspan=\"4\"> No of instances </th></tr>");
		out.println("<tr><th>Nouns</th><th>Verbs</th><th>Adjectives</th><th>All</th></tr>");
	}
	"""
	synset_stats = stats.get_synset_relation_count()
	s = synset_stats[11]
	print """if(langID==0)
		out.print("<tr><th>Hiperonimia</th>"); 
	else
		out.print("<tr><th>Hypernymy</th>");
	out.println("<td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td></tr>");"""% ( s[noun], s[verb], s[adj], s[noun] + s[verb] + s[adj])
	s = synset_stats[15]
	print """if(langID==0)
		out.print("<tr><th>Holonimia</th>"); 
	else
		out.print("<tr><th>Holonymy</th>");
	out.println("<td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td></tr>");"""% ( s[noun], s[verb], s[adj], s[noun] + s[verb] + s[adj])
	s = synset_stats[14]
	print """if(langID==0)
		out.print("<tr><th>Meronimia</th>"); 
	else
		out.print("<tr><th>Meronymy</th>");
	out.println("<td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td></tr><tr><td colspan=\\\"5\\\">&nbsp;</td></tr>");"""% ( s[noun], s[verb], s[adj], s[noun] + s[verb] + s[adj])
	lex_stats = stats.get_lexical_relation_count()
	s = lex_stats[16]
	print """if(langID==0)
		out.print("<tr><th>Troponimia</th>"); 
	else
		out.print("<tr><th>Troponymy</th>");
	out.println("<td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td></tr>");"""% ( s[noun], s[verb], s[adj], s[noun] + s[verb] + s[adj])
	s = lex_stats[12]
	print """if(langID==0)
		out.print("<tr><th>Antonimia</th>"); 
	else
		out.print("<tr><th>Antonymy</th>");
	out.println("<td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td></tr>");"""% ( s[noun], s[verb], s[adj], s[noun] + s[verb] + s[adj])
	s = lex_stats[13]
	print """if(langID==0)
		out.print("<tr><th>Konwersja</th>"); 
	else
		out.print("<tr><th>Conversion</th>");
	out.println("<td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td></tr>");"""% ( s[noun], s[verb], s[adj], s[noun] + s[verb] + s[adj])
	s = lex_stats[17]
	print """if(langID==0)
		out.print("<tr><th>Relacyjność</th>"); 
	else
		out.print("<tr><th>Relatedness</th>");
	out.println("<td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td></tr>");"""% ( s[noun], s[verb], s[adj], s[noun] + s[verb] + s[adj])
	s = lex_stats[18]
	print """if(langID==0)
		out.print("<tr><th>Odniośność</th>"); 
	else
		out.print("<tr><th>Partainymy</th>");
	out.println("<td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td></tr>");"""% ( s[noun], s[verb], s[adj], s[noun] + s[verb] + s[adj])
	s = lex_stats[19]
	print """if(langID==0)
		out.print("<tr><th>Fuzynimia</th>"); 
	else
		out.print("<tr><th>Fuzzynimy</th>");
	out.println("<td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td></tr>");"""% ( s[noun], s[verb], s[adj], s[noun] + s[verb] + s[adj])

	print "%></table> <br>"

if __name__ == '__main__':
	db = pyplwn.database.Database(user = "wordnet", passwd = "w0rdn3tDBP@sswd", host = "plwordnet.pwr.wroc.pl", db = "wordnet_temp", port = 3308, encoding = "utf8")
	#db = pyplwn.database.Database('plwn', 'plwn', 'localhost', 'plwn2008_czerwiec', 3306)
	print_jsp_header()
	
	print_wn_numbers_by_pos(db)
	print_avg_polysemy(db)
	print_synset_size_histogram(db)
	print_units_polysemy_histogram(db)
	print_relations_count(db)

	print_jsp_methods()

