# -*- coding: utf-8 -*

import getopt
import os
import sys

from east import applications
from east.synonyms import synonyms
from east import utils


def main():
    args = sys.argv[1:]
    opts, args = getopt.getopt(args, "a:l:t:sdg")
    opts = dict(opts)
    opts.setdefault("-a", "easa")
    opts.setdefault("-l", "0.6")
    opts.setdefault("-t", "0.25")

    if len(args) < 2:
        print "Invalid syntax."
        return 1

    command_group = args[0]
    command = args[1]

    if command_group == "keyphrases":

        keyphrases_file = os.path.abspath(args[2])
        input_path = os.path.abspath(args[3])
        use_synonyms = "-s" in opts
        normalized_scores = "-d" not in opts
        ast_algorithm = opts["-a"]
        significance_level = float(opts["-l"])
        score_threshold = float(opts["-t"])

        if os.path.isdir(input_path):
            input_files = [os.path.abspath(input_path) + "/" + filename
                           for filename in os.listdir(input_path)
                           if filename.endswith(".txt")]
        else:
            input_files = [os.path.abspath(input_path)]

        texts = {}
        for filename in input_files:
            with open(filename) as f:
                text_name = os.path.basename(filename).decode("utf-8")[:-4]
                texts[text_name] = f.read()

        with open(keyphrases_file) as f:
            keyphrases = map(lambda k: utils.prepare_text(k), f.read().splitlines())

        if use_synonyms:
            synonimizer = synonyms.SynonymExtractor(input_path)
        else:
            synonimizer = None

        if command == "table":

            keyphrases_table = applications.keyphrases_table(keyphrases, texts, ast_algorithm,
                                                             normalized_scores, synonimizer)
            res = u""
            for keyphrase in sorted(keyphrases_table.keys()):
                res += u'<keyphrase value="%s">\n' % keyphrase
                for text in sorted(keyphrases_table[keyphrase].keys()):
                    res += u'  <text name="%s">' % text
                    res += u'%.3f' % keyphrases_table[keyphrase][text]
                    res += u'</text>\n'
                res += u'</keyphrase>\n'

            print res.encode("utf-8", "ignore")

        elif command == "graph":

            graph = applications.keyphrases_graph(keyphrases, texts, significance_level,
                                                  score_threshold, ast_algorithm,
                                                  normalized_scores, synonimizer)
            print_gml = "-g" in opts
            if print_gml:
                res = utils.graph2gml(graph)
            else:
                res = u""
                for node in graph:
                    if graph[node]:
                        res += "%s -> %s\n" % (node, ", ".join(graph[node]))

            print res.encode("utf-8", "ignore")

        else:
            print "Invalid command."
            return 1

    else:
        print "Invalid command."
        return 1



if __name__ == "__main__":
    main()

@@ -223,15 +223,16 @@
  %22a:
+f:
 l:t:
+d
 s
-dg
 %22)%0A 
@@ -289,75 +289,281 @@
 sa%22)
-%0A    opts.setdefault(%22-l%22, %220.6%22)%0A    opts.setdefault(%22-t%22, %220.25%22)
+   # Algorithm to use for computing ASTs%0A    opts.setdefault(%22-f%22, %22edges%22)  # Graph output format (also %22gml%22 possible)%0A    opts.setdefault(%22-l%22, %220.6%22)    # Level of significance for graph construction%0A    opts.setdefault(%22-t%22, %220.25%22)   # Threshold of the matching score
 %0A%0A  
@@ -935,16 +935,50 @@
 s%5B%22-a%22%5D%0A
+        graph_format = opts%5B%22-f%22%5D%0A
         
@@ -2074,32 +2074,39 @@
         res = u%22
+%3Ctable%3E
 %22%0A            fo
@@ -2178,16 +2178,18 @@
 es += u'
+  
 %3Ckeyphra
@@ -2319,16 +2319,18 @@
 es += u'
+  
   %3Ctext 
@@ -2484,16 +2484,18 @@
 es += u'
+  
 %3C/keyphr
@@ -2501,16 +2501,49 @@
 rase%3E%5Cn'
+%0A            res += u%22%3C/table%3E%5Cn%22
 %0A%0A      
@@ -2888,57 +2888,32 @@
     
-print_gml
+if graph_format
  =
+=
  %22
--g%22 in opts%0A            if print_
 gml
+%22
 :%0A  
@@ -2961,34 +2961,58 @@
 )%0A            el
-se
+if graph_format == %22edges%22
 :%0A              
@@ -3094,16 +3094,16 @@
 %5Bnode%5D:%0A
-
         
@@ -3170,16 +3170,189 @@
 %5Bnode%5D))
+%0A            else:%0A                print (%22Unknown graph format: '%25s'. %22%0A                       %22Please use one of: 'gml', 'edges'.%22 %25 graph_format)%0A                return 1
 %0A%0A      

Change CLI interface for different graph formats