# Path: nordlys/config.py
# OUTPUT_DIR = path.dirname(path.dirname(path.abspath(__file__))) + "/output"
# 
# Path: nordlys/tagme/test_coll.py
# def read_tagme_queries(dataset_file):
#     """
#     Reads queries from snippet file.
# 
#     :return dictionary {qid : query}
#     """
#     queries = {}
#     q_file = open(dataset_file, "r")
#     for line in q_file:
#         line = line.strip().split("\t")
#         query_id = line[0].strip()
#         query = line[1].strip()
#         queries[query_id] = query
#     q_file.close()
#     print "Number of queries:", len(queries)
#     return queries
# 
# def read_yerd_queries(y_erd_file=config.Y_ERD):
#     """
#     Reads queries from Erd query file.
# 
#     :return dictionary {query_id : query_content}
#     """
#     queries = {}
#     with open(y_erd_file, 'rb') as y_erd:
#         reader = csv.DictReader(y_erd, delimiter="\t", quoting=csv.QUOTE_NONE)
# 
#         for line in reader:
#             qid = line['qid']
#             query = line['query']
#             queries[qid] = query.strip()
#     print "Number of queries:", len(queries)
#     return queries
# 
# def read_erd_queries(erd_q_file=config.ERD_QUERY):
#     """
#     Reads queries from Erd query file.
# 
#     :return dictionary {qid : query}
#     """
#     queries = {}
#     q_file = open(erd_q_file, "r")
#     for line in q_file:
#         line = line.split("\t")
#         query_id = line[0].strip()
#         query = line[-1].strip()
#         queries[query_id] = query
#     q_file.close()
#     print "Number of queries:", len(queries)
#     return queries
# 
# Path: nordlys/wikipedia/utils.py
# class WikipediaUtils(object):
#     mongo = None
#     
#     @staticmethod
#     def wiki_title_to_uri(title):
#         """
#         Converts wiki page title to wiki_uri
#         based on https://en.wikipedia.org/wiki/Wikipedia:Page_name#Spaces.2C_underscores_and_character_coding
#         encoding based on http://dbpedia.org/services-resources/uri-encoding
#         """
#         if title:
#             wiki_uri = "<wikipedia:" + quote(title, ' !$&\'()*+,-./:;=@_~').replace(' ', '_') + ">"
#             return wiki_uri
#         else:
#             return None
# 
#     @staticmethod
#     def wiki_uri_to_dbp_uri(wiki_uri):
#         """Converts Wikipedia uri to DBpedia URI."""
#         return wiki_uri.replace("<wikipedia:", "<dbpedia:")
# 
# Path: nordlys/tagme/config.py
# Y_ERD = DATA_DIR + "/Y-ERD.tsv"
# ERD_QUERY = DATA_DIR + "/Trec_beta.query.txt"
# ERD_ANNOTATION = DATA_DIR + "/Trec_beta.annotation.txt"
# WIKI_ANNOT30 = DATA_DIR + "/wiki-annot30"
# WIKI_ANNOT30_SNIPPET = DATA_DIR + "/wiki-annot30-snippet.txt"
# WIKI_ANNOT30_ANNOTATION = DATA_DIR + "/wiki-annot30-annotation.txt"
# WIKI_DISAMB30 = DATA_DIR + "/wiki-disamb30"
# WIKI_DISAMB30_SNIPPET = DATA_DIR + "/wiki-disamb30-snippet.txt"
# WIKI_DISAMB30_ANNOTATION = DATA_DIR + "/wiki-disamb30-annotation.txt"
# COLLECTION_SURFACEFORMS_WIKI = "surfaceforms_wiki_20100408"
# SF_WIKI = SurfaceForms(collection=COLLECTION_SURFACEFORMS_WIKI)
# INDEX_PATH = "/xxx/20100408-index"
# INDEX_ANNOT_PATH = "/xxx/20100408-index-annot/"


import argparse
import requests
from nordlys.config import OUTPUT_DIR
from nordlys.tagme.test_coll import read_tagme_queries, read_yerd_queries, read_erd_queries
from nordlys.wikipedia.utils import WikipediaUtils
from nordlys.tagme import config
        none_str = "*NONE*"
        out_str = ""
        for annot in response['spots']:
            wiki_uri = self.ask_title(annot.get('entity', none_str))
            if wiki_uri is None:
                continue
            qid_str = str(qid) + "\t" + str(annot.get('score', none_str)) + "\t" + wiki_uri + "\t" + \
                      annot.get('mention', none_str) + "\t" + str(annot.get('entity', none_str)) + "\t" + \
                      str(annot.get('start', none_str)) + "\t" + str(annot.get('end', none_str)) + "\t" + \
                      str(annot.get('linkProbability', none_str)) + "\t" + str(annot.get('linkFrequency', none_str)) + "\t" +\
                      str(annot.get('documentFrequency', none_str)) + "\t" + str(annot.get('entityFrequency', none_str)) + "\t" +\
                      str(annot.get('commonness', none_str)) + "\n"
            out_str += qid_str
        return out_str


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-th", "--threshold", help="rho score threshold", type=float, default=0)
    parser.add_argument("-qid", help="annotates queries from this qid", type=str)
    parser.add_argument("-data", help="Data set name", choices=['y-erd', 'erd-dev', 'wiki-annot30', 'wiki-disamb30'])
    args = parser.parse_args()

    if args.data == "erd-dev":
        queries = read_erd_queries()
    elif args.data == "y-erd":
        queries = read_yerd_queries()
    elif args.data == "wiki-annot30":
        queries = read_tagme_queries(config.WIKI_ANNOT30_SNIPPET)
    elif args.data == "wiki-disamb30":

# Path: nordlys/config.py
# OUTPUT_DIR = path.dirname(path.dirname(path.abspath(__file__))) + "/output"
# 
# Path: nordlys/tagme/test_coll.py
# def read_tagme_queries(dataset_file):
#     """
#     Reads queries from snippet file.
# 
#     :return dictionary {qid : query}
#     """
#     queries = {}
#     q_file = open(dataset_file, "r")
#     for line in q_file:
#         line = line.strip().split("\t")
#         query_id = line[0].strip()
#         query = line[1].strip()
#         queries[query_id] = query
#     q_file.close()
#     print "Number of queries:", len(queries)
#     return queries
# 
# def read_yerd_queries(y_erd_file=config.Y_ERD):
#     """
#     Reads queries from Erd query file.
# 
#     :return dictionary {query_id : query_content}
#     """
#     queries = {}
#     with open(y_erd_file, 'rb') as y_erd:
#         reader = csv.DictReader(y_erd, delimiter="\t", quoting=csv.QUOTE_NONE)
# 
#         for line in reader:
#             qid = line['qid']
#             query = line['query']
#             queries[qid] = query.strip()
#     print "Number of queries:", len(queries)
#     return queries
# 
# def read_erd_queries(erd_q_file=config.ERD_QUERY):
#     """
#     Reads queries from Erd query file.
# 
#     :return dictionary {qid : query}
#     """
#     queries = {}
#     q_file = open(erd_q_file, "r")
#     for line in q_file:
#         line = line.split("\t")
#         query_id = line[0].strip()
#         query = line[-1].strip()
#         queries[query_id] = query
#     q_file.close()
#     print "Number of queries:", len(queries)
#     return queries
# 
# Path: nordlys/wikipedia/utils.py
# class WikipediaUtils(object):
#     mongo = None
#     
#     @staticmethod
#     def wiki_title_to_uri(title):
#         """
#         Converts wiki page title to wiki_uri
#         based on https://en.wikipedia.org/wiki/Wikipedia:Page_name#Spaces.2C_underscores_and_character_coding
#         encoding based on http://dbpedia.org/services-resources/uri-encoding
#         """
#         if title:
#             wiki_uri = "<wikipedia:" + quote(title, ' !$&\'()*+,-./:;=@_~').replace(' ', '_') + ">"
#             return wiki_uri
#         else:
#             return None
# 
#     @staticmethod
#     def wiki_uri_to_dbp_uri(wiki_uri):
#         """Converts Wikipedia uri to DBpedia URI."""
#         return wiki_uri.replace("<wikipedia:", "<dbpedia:")
# 
# Path: nordlys/tagme/config.py
# Y_ERD = DATA_DIR + "/Y-ERD.tsv"
# ERD_QUERY = DATA_DIR + "/Trec_beta.query.txt"
# ERD_ANNOTATION = DATA_DIR + "/Trec_beta.annotation.txt"
# WIKI_ANNOT30 = DATA_DIR + "/wiki-annot30"
# WIKI_ANNOT30_SNIPPET = DATA_DIR + "/wiki-annot30-snippet.txt"
# WIKI_ANNOT30_ANNOTATION = DATA_DIR + "/wiki-annot30-annotation.txt"
# WIKI_DISAMB30 = DATA_DIR + "/wiki-disamb30"
# WIKI_DISAMB30_SNIPPET = DATA_DIR + "/wiki-disamb30-snippet.txt"
# WIKI_DISAMB30_ANNOTATION = DATA_DIR + "/wiki-disamb30-annotation.txt"
# COLLECTION_SURFACEFORMS_WIKI = "surfaceforms_wiki_20100408"
# SF_WIKI = SurfaceForms(collection=COLLECTION_SURFACEFORMS_WIKI)
# INDEX_PATH = "/xxx/20100408-index"
# INDEX_ANNOT_PATH = "/xxx/20100408-index-annot/"


# Path: nordlys/tagme/dexter_api.py
import argparse
import requests
from nordlys.config import OUTPUT_DIR
from nordlys.tagme.test_coll import read_tagme_queries, read_yerd_queries, read_erd_queries
from nordlys.wikipedia.utils import WikipediaUtils
from nordlys.tagme import config

        none_str = "*NONE*"
        out_str = ""
        for annot in response['spots']:
            wiki_uri = self.ask_title(annot.get('entity', none_str))
            if wiki_uri is None:
                continue
            qid_str = str(qid) + "\t" + str(annot.get('score', none_str)) + "\t" + wiki_uri + "\t" + \
                      annot.get('mention', none_str) + "\t" + str(annot.get('entity', none_str)) + "\t" + \
                      str(annot.get('start', none_str)) + "\t" + str(annot.get('end', none_str)) + "\t" + \
                      str(annot.get('linkProbability', none_str)) + "\t" + str(annot.get('linkFrequency', none_str)) + "\t" +\
                      str(annot.get('documentFrequency', none_str)) + "\t" + str(annot.get('entityFrequency', none_str)) + "\t" +\
                      str(annot.get('commonness', none_str)) + "\n"
            out_str += qid_str
        return out_str


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-th", "--threshold", help="rho score threshold", type=float, default=0)
    parser.add_argument("-qid", help="annotates queries from this qid", type=str)
    parser.add_argument("-data", help="Data set name", choices=['y-erd', 'erd-dev', 'wiki-annot30', 'wiki-disamb30'])
    args = parser.parse_args()

    if args.data == "erd-dev":
        queries = read_erd_queries()
    elif args.data == "y-erd":
        queries = read_yerd_queries()
    elif args.data == "wiki-annot30":
        queries = read_tagme_queries(config.WIKI_ANNOT30_SNIPPET)
    elif args.data == "wiki-disamb30":

        queries = read_tagme_queries(config.WIKI_DISAMB30_SNIPPET)