# Path: nordlys/config.py
# MONGO_DB = "nordlys"
# 
# MONGO_HOST = "localhost"
# 
# Path: nordlys/storage/mongo.py
# class Mongo(object):
#     """Manages the MongoDB connection and operations."""
#     ID_FIELD = "_id"
# 
#     def __init__(self, host, db, collection):
#         self.client = MongoClient(host)
#         self.db = self.client[db]
#         self.collection = self.db[collection]
#         self.db_name = db
#         self.collection_name = collection
#         print "Connected to " + self.db_name + "." + self.collection_name
# 
#     @staticmethod
#     def escape(s):
#         """Escapes string (to be used as key or fieldname).
#         Replaces . and $ with their unicode eqivalents."""
#         return s.replace(".", "\u002e").replace("$", "\u0024")
# 
#     @staticmethod
#     def unescape(s):
#         """Unescapes string."""
#         return s.replace("\u002e", ".").replace("\u0024", "$")
# 
#     def find_by_id(self, doc_id):
#         """Returns all document content for a given document id."""
#         return self.get_doc(self.collection.find_one({Mongo.ID_FIELD: self.escape(doc_id)}))
# 
#     def get_doc(self, mdoc):
#         """Returns document contents with with keys and _id field unescaped."""
#         if mdoc is None:
#             return None
# 
#         doc = {}
#         for f in mdoc:
#             if f == Mongo.ID_FIELD:
#                 doc[f] = self.unescape(mdoc[f])
#             else:
#                 doc[self.unescape(f)] = mdoc[f]
# 
#         return doc
# 
#     @staticmethod
#     def print_doc(doc):
#         print "_id: " + doc[Mongo.ID_FIELD]
#         for key, value in doc.iteritems():
#             if key == Mongo.ID_FIELD: continue  # ignore the id key
#             if type(value) is list:
#                 print key + ":"
#                 for v in value:
#                     print "\t" + str(v)
#             else:
#                 print key + ": " + str(value)


from nordlys.config import MONGO_DB, MONGO_HOST
from nordlys.storage.mongo import Mongo
"""
Entity surface forms stored in MongoDB.

The surface form is used as _id. The associated entities are stored in key-value format.

@author: Krisztian Balog (krisztian.balog@uis.no)
"""



class SurfaceForms(object):

    def __init__(self, collection):
        self.collection = collection
        self.mongo = Mongo(MONGO_HOST, MONGO_DB, self.collection)

    def get(self, surface_form):
        """Returns all information associated with a surface form."""

        # need to unescape the keys in the value part
        mdoc = self.mongo.find_by_id(surface_form)
        if mdoc is None:
            return None
        doc = {}
        for f in mdoc:

# Path: nordlys/config.py
# MONGO_DB = "nordlys"
# 
# MONGO_HOST = "localhost"
# 
# Path: nordlys/storage/mongo.py
# class Mongo(object):
#     """Manages the MongoDB connection and operations."""
#     ID_FIELD = "_id"
# 
#     def __init__(self, host, db, collection):
#         self.client = MongoClient(host)
#         self.db = self.client[db]
#         self.collection = self.db[collection]
#         self.db_name = db
#         self.collection_name = collection
#         print "Connected to " + self.db_name + "." + self.collection_name
# 
#     @staticmethod
#     def escape(s):
#         """Escapes string (to be used as key or fieldname).
#         Replaces . and $ with their unicode eqivalents."""
#         return s.replace(".", "\u002e").replace("$", "\u0024")
# 
#     @staticmethod
#     def unescape(s):
#         """Unescapes string."""
#         return s.replace("\u002e", ".").replace("\u0024", "$")
# 
#     def find_by_id(self, doc_id):
#         """Returns all document content for a given document id."""
#         return self.get_doc(self.collection.find_one({Mongo.ID_FIELD: self.escape(doc_id)}))
# 
#     def get_doc(self, mdoc):
#         """Returns document contents with with keys and _id field unescaped."""
#         if mdoc is None:
#             return None
# 
#         doc = {}
#         for f in mdoc:
#             if f == Mongo.ID_FIELD:
#                 doc[f] = self.unescape(mdoc[f])
#             else:
#                 doc[self.unescape(f)] = mdoc[f]
# 
#         return doc
# 
#     @staticmethod
#     def print_doc(doc):
#         print "_id: " + doc[Mongo.ID_FIELD]
#         for key, value in doc.iteritems():
#             if key == Mongo.ID_FIELD: continue  # ignore the id key
#             if type(value) is list:
#                 print key + ":"
#                 for v in value:
#                     print "\t" + str(v)
#             else:
#                 print key + ": " + str(value)


# Path: nordlys/storage/surfaceforms.py
from nordlys.config import MONGO_DB, MONGO_HOST
from nordlys.storage.mongo import Mongo

"""
Entity surface forms stored in MongoDB.

The surface form is used as _id. The associated entities are stored in key-value format.

@author: Krisztian Balog (krisztian.balog@uis.no)
"""



class SurfaceForms(object):

    def __init__(self, collection):
        self.collection = collection
        self.mongo = Mongo(MONGO_HOST, MONGO_DB, self.collection)

    def get(self, surface_form):
        """Returns all information associated with a surface form."""

        # need to unescape the keys in the value part
        mdoc = self.mongo.find_by_id(surface_form)
        if mdoc is None:
            return None
        doc = {}
        for f in mdoc:

            if f != Mongo.ID_FIELD: