<s> def format_table ( table , format ) : <EOL> if format == "<STR_LIT>" : <EOL> return table2xml ( keyphrases_table ) <EOL> elif format == "<STR_LIT>" : <EOL> return table2csv ( keyphrases_table ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" % format ) <EOL> def table2xml ( keyphrases_table ) : <EOL> res = "<STR_LIT>" <EOL> for keyphrase in sorted ( keyphrases_table . keys ( ) ) : <EOL> res += '<STR_LIT>' % keyphrase <EOL> for text in sorted ( keyphrases_table [ keyphrase ] . keys ( ) ) : <EOL> res += '<STR_LIT>' % text <EOL> res += '<STR_LIT>' % keyphrases_table [ keyphrase ] [ text ] <EOL> res += '<STR_LIT>' <EOL> res += '<STR_LIT>' <EOL> res += "<STR_LIT>" <EOL> return res <EOL> def table2csv ( keyphrases_table ) : <EOL> def quote ( s ) : <EOL> return '<STR_LIT:">' + s . replace ( '<STR_LIT:">' , "<STR_LIT:'>" ) + '<STR_LIT:">' <EOL> keyphrases = sorted ( keyphrases_table . keys ( ) ) <EOL> texts = sorted ( keyphrases_table [ keyphrases [ <NUM_LIT:0> ] ] . keys ( ) ) <EOL> res = "<STR_LIT:U+002C>" + "<STR_LIT:U+002C>" . join ( map ( quote , keyphrases ) ) + "<STR_LIT:\n>" <EOL> for text in texts : <EOL> scores = map ( lambda score : u"<STR_LIT>" % score , <EOL> [ keyphrases_table [ keyphrase ] [ text ] for keyphrase in keyphrases ] ) <EOL> res += ( quote ( text ) + "<STR_LIT:U+002C>" + "<STR_LIT:U+002C>" . join ( scores ) + "<STR_LIT:\n>" ) <EOL> return res <EOL> def format_graph ( graph , format ) : <EOL> if format == "<STR_LIT>" : <EOL> return graph2gml ( graph ) <EOL> elif format == "<STR_LIT>" : <EOL> return graph2edges ( graph ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" % format ) <EOL> def graph2edges ( graph ) : <EOL> res = "<STR_LIT>" <EOL> node_edges = { } <EOL> for edge in graph [ "<STR_LIT>" ] : <EOL> source_label = graph [ "<STR_LIT>" ] [ edge [ "<STR_LIT:source>" ] ] [ "<STR_LIT:label>" ] <EOL> target_label = graph [ "<STR_LIT>" ] [ edge [ "<STR_LIT:target>" ] ] [ "<STR_LIT:label>" ] <EOL> if source_label not in node_edges : <EOL> node_edges [ source_label ] = [ ] <EOL> node_edges [ source_label ] . append ( target_label ) <EOL> for node in node_edges : <EOL> res += "<STR_LIT>" % ( node , "<STR_LIT:U+002CU+0020>" . join ( node_edges [ node ] ) ) <EOL> return res <EOL> def graph2gml ( graph ) : <EOL> res = "<STR_LIT>" <EOL> res += "<STR_LIT>" <EOL> res += "<STR_LIT>" % graph [ "<STR_LIT>" ] <EOL> res += "<STR_LIT>" % graph [ "<STR_LIT>" ] <EOL> res += "<STR_LIT>" % graph [ "<STR_LIT>" ] <EOL> for node in graph [ "<STR_LIT>" ] : <EOL> res += ( '<STR_LIT>' % <EOL> ( node [ "<STR_LIT:id>" ] , node [ "<STR_LIT:label>" ] ) ) <EOL> for edge in graph [ "<STR_LIT>" ] : <EOL> res += ( '<STR_LIT>' % <EOL> ( edge [ "<STR_LIT:source>" ] , edge [ "<STR_LIT:target>" ] , edge [ "<STR_LIT>" ] ) ) <EOL> res += "<STR_LIT>" <EOL> return res </s>
