#!/usr/bin/env python
# encoding=utf8

# example datatxt python cli

from __future__ import print_function

from datatxt import Datatxt

import argparse

TEXT = """Sulle colline basaltiche, vicino a Rovereto si produce il Marzemino"""
RESOURCE = """http://it.dbpedia.org/resource/Rovereto"""
SPARQL = """select * where { <http://it.dbpedia.org/resource/Marzemino> ?p ?o . } }"""

def print_properties(prop_dict):
    for key, vlist in prop_dict.items():
        if len(vlist) == 1:
            values = unicode(vlist[0])
        else:
            values = u' | '.join([unicode(el) for el in vlist])

        print("%s: %s" % (key, values))
    print('\n')

def pretty_print(datatxt_res):
    for ann in datatxt_res:
        print("You are talking about <%s> (@pos %d, %d), with confidence %f\nProperties:\n" %
                (ann['ref'], ann['start'], ann['end'], ann['rho']))
        try:
            print_properties(ann['properties'])
        except KeyError: # no properties
            pass

def handle_args():
    parser = argparse.ArgumentParser(description='Annota del testo con dataTXT e recupera informazioni sulle annotazioni da DBpedia.')
    parser.add_argument("--text", help='"testo da annotare"', default=TEXT)
    parser.add_argument("--properties", help='"http://it.dbpedia.org/propetà da indagare"', default=RESOURCE)
    # parser.add_argument("--sparql", help='query sparql indagare', default=SPARQL)
    return parser.parse_args()

if __name__ == '__main__':
    args = handle_args()

    # Instantiate the main class
    d = Datatxt()

    # ask for a full annotation
    res = d.annotate(args.text)
    pretty_print(res['annotations'])

    # ask for a single RESOURCE
    print("Proprietà di <%s>:\n" % args.properties)
    prop = d.properties(args.properties) 
    print_properties(prop)



