# Path: nordlys/config.py
# DATA_DIR = path.dirname(path.dirname(path.abspath(__file__))) + "/data"
# 
# 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:")


import sys
from collections import defaultdict
from nordlys.config import DATA_DIR
from nordlys.wikipedia.utils import WikipediaUtils


def load_kb():
    """Loads Freebase snapshot of proper noun entities."""
    print "Loading knowledge base snapshot ..."
    __fb_dbp_file = open(DATA_DIR + "/fb_dbp_snapshot.txt", "r")
    global KB_SNP_DBP
    for line in __fb_dbp_file:
        cols = line.strip().split("\t")
        KB_SNP_DBP.add(cols[1])
    __fb_dbp_file.close()

KB_SNP_DBP = set()


def read_file(input_file, score_th):
    lines = []
    with open(input_file, "r") as input:
        for line in input:
            cols = line.strip().split("\t")
            if float(cols[1]) < score_th:
                continue
            lines.append(cols)
    return lines


def filter_general_ens(lines):
    """Returns tab-separated lines: qid score   en  men fb_id"""
    filtered_annots = []
    for line in lines:

# Path: nordlys/config.py
# DATA_DIR = path.dirname(path.dirname(path.abspath(__file__))) + "/data"
# 
# 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: scripts/to_elq.py
import sys
from collections import defaultdict
from nordlys.config import DATA_DIR
from nordlys.wikipedia.utils import WikipediaUtils



def load_kb():
    """Loads Freebase snapshot of proper noun entities."""
    print "Loading knowledge base snapshot ..."
    __fb_dbp_file = open(DATA_DIR + "/fb_dbp_snapshot.txt", "r")
    global KB_SNP_DBP
    for line in __fb_dbp_file:
        cols = line.strip().split("\t")
        KB_SNP_DBP.add(cols[1])
    __fb_dbp_file.close()

KB_SNP_DBP = set()


def read_file(input_file, score_th):
    lines = []
    with open(input_file, "r") as input:
        for line in input:
            cols = line.strip().split("\t")
            if float(cols[1]) < score_th:
                continue
            lines.append(cols)
    return lines


def filter_general_ens(lines):
    """Returns tab-separated lines: qid score   en  men fb_id"""
    filtered_annots = []
    for line in lines:

        dbp_uri = WikipediaUtils.wiki_uri_to_dbp_uri(line[2])