# Path: sanskrit_parser/base/sanskrit_base.py
# class SanskritImmutableString(SanskritString):
#     """ Immutable version of SanskritString
#     """
#     def __init__(self, thing=None, encoding=None, unicode_encoding='utf-8'):
#         super().__init__(thing, encoding, unicode_encoding)
# 
#     def __hash__(self):
#         return hash(str(self))
# 
#     def __eq__(self, other):
#         return str(self) == str(other)
# 
#     def __ne__(self, other):
#         return str(self) != str(other)
# 
# SCHEMES = {
#     'Bengali': BENGALI,
#     'Devanagari': DEVANAGARI,
#     'Gujarati': GUJARATI,
#     'Gurmukhi': GURMUKHI,
#     'Kannada': KANNADA,
#     'Malayalam': MALAYALAM,
#     'Oriya': ORIYA,
#     'Tamil': TAMIL,
#     'Telugu': TELUGU,
#     'HK': HK,
#     'IAST': IAST,
#     'ITRANS': ITRANS,
#     'Kolkata': KOLKATA,
#     'SLP1': SLP1,
#     'Velthuis': VELTHUIS,
#     'WX': WX
# }
# 
# Path: sanskrit_parser/util/lexical_lookup.py
# class LexicalLookup(object):
# 
#     __metaclass__ = abc.ABCMeta
# 
#     @abc.abstractmethod
#     def valid(self, word):
#         """ Return True if word is a valid pada """
# 
#     @abc.abstractmethod
#     def get_tags(self, word, tmap=True):
#         """ Return lexical tags of word """
# 
#     @staticmethod
#     def getArgs():
#         """
#           Argparse routine.
#           Returns args variable
#         """
#         # Parser Setup
#         parser = ArgumentParser(description='Interface to INRIA XML database')
#         # Input Encoding (autodetect by default)
#         parser.add_argument('--input-encoding', type=str, default=None)
#         parser.add_argument('--loglevel', type=str, default="info",
#                             help="logging level. Can be any level supported by logging module")
#         parser.add_argument('--no-map-tags', dest='map_tags',
#                             action='store_false')
# 
#         parser.add_argument('word', nargs='?', type=str,
#                             default=None,
#                             help="Word to look up")
# 
#         return parser.parse_args()
# 
#     def main(self, args):
#         if args.input_encoding is None:
#             ie = None
#         else:
#             ie = SCHEMES[args.input_encoding]
# 
#         word_in = SanskritImmutableString(args.word,
#                                           encoding=ie).canonical()
#         print("Getting tags for", word_in)
#         tags = self.get_tags(word_in, tmap=args.map_tags)
#         if tags is not None:
#             for tag in tags:
#                 print(tag)
# 
# Path: sanskrit_parser/util/inriatagmapper.py
# def inriaTagMapper(tags):
#     ''' Map an INRIA tag to our format
# 
#     Params:
#        tags: List of INRIA format tags
#     Returns
#        list[(<stem>,set([our tags])) ...]
#     '''
#     return list(map(inriaMapTag, tags))
# 
# Path: sanskrit_parser/util/data_manager.py
# def data_file_path(filename):
#     file_manager = ExitStack()
#     atexit.register(file_manager.close)
#     ref = importlib_resources.files('sanskrit_parser') / 'data' / filename
#     path = file_manager.enter_context(
#         importlib_resources.as_file(ref))
#     return str(path)


import pickle
import sqlite3
import logging
from collections import namedtuple
from sanskrit_parser.base.sanskrit_base import SanskritImmutableString, SCHEMES
from sanskrit_parser.util.lexical_lookup import LexicalLookup
from sanskrit_parser.util.inriatagmapper import inriaTagMapper
from sanskrit_parser.util.data_manager import data_file_path
    from argparse import ArgumentParser
    by Prof. Gerard Huet
    https://gitlab.inria.fr/huet/Heritage_Resources
    """

    '''
    The custom database format has two parts:
        1. A pickle file that contains a list of stems,
           a list of tags, and a serialized buffer of the
           indices of stems and tags for each form. The indices
           are used as it is more efficient to store such integers
           instead of the string for each tag.
        2. An sqlite file that maps each form to the position
           within the buffer that contains the serialized tuple
           of stems and tags for that form. An sqlite database
           is used to avoid having to build a huge dict in
           memory for the 600K forms that are present in this db,
           which consumes a lot of memory. (See
           https://github.com/kmadathil/sanskrit_parser/issues/151)

    To lookup the tag for a form, we use the sqlite db to find the
    position in the buffer, deserialize the data at that position,
    which gives us a list of the tag set for that form. For each
    item in that list, we then lookup the right stem and tag in
    the list of stems and tags loaded from the pickle file
    '''

    def __init__(self, logger=None):
        self.pickle_file = "inria_forms.pickle"
        self.logger = logger or logging.getLogger(__name__)
        db_file = data_file_path("inria_forms_pos.db")

# Path: sanskrit_parser/base/sanskrit_base.py
# class SanskritImmutableString(SanskritString):
#     """ Immutable version of SanskritString
#     """
#     def __init__(self, thing=None, encoding=None, unicode_encoding='utf-8'):
#         super().__init__(thing, encoding, unicode_encoding)
# 
#     def __hash__(self):
#         return hash(str(self))
# 
#     def __eq__(self, other):
#         return str(self) == str(other)
# 
#     def __ne__(self, other):
#         return str(self) != str(other)
# 
# SCHEMES = {
#     'Bengali': BENGALI,
#     'Devanagari': DEVANAGARI,
#     'Gujarati': GUJARATI,
#     'Gurmukhi': GURMUKHI,
#     'Kannada': KANNADA,
#     'Malayalam': MALAYALAM,
#     'Oriya': ORIYA,
#     'Tamil': TAMIL,
#     'Telugu': TELUGU,
#     'HK': HK,
#     'IAST': IAST,
#     'ITRANS': ITRANS,
#     'Kolkata': KOLKATA,
#     'SLP1': SLP1,
#     'Velthuis': VELTHUIS,
#     'WX': WX
# }
# 
# Path: sanskrit_parser/util/lexical_lookup.py
# class LexicalLookup(object):
# 
#     __metaclass__ = abc.ABCMeta
# 
#     @abc.abstractmethod
#     def valid(self, word):
#         """ Return True if word is a valid pada """
# 
#     @abc.abstractmethod
#     def get_tags(self, word, tmap=True):
#         """ Return lexical tags of word """
# 
#     @staticmethod
#     def getArgs():
#         """
#           Argparse routine.
#           Returns args variable
#         """
#         # Parser Setup
#         parser = ArgumentParser(description='Interface to INRIA XML database')
#         # Input Encoding (autodetect by default)
#         parser.add_argument('--input-encoding', type=str, default=None)
#         parser.add_argument('--loglevel', type=str, default="info",
#                             help="logging level. Can be any level supported by logging module")
#         parser.add_argument('--no-map-tags', dest='map_tags',
#                             action='store_false')
# 
#         parser.add_argument('word', nargs='?', type=str,
#                             default=None,
#                             help="Word to look up")
# 
#         return parser.parse_args()
# 
#     def main(self, args):
#         if args.input_encoding is None:
#             ie = None
#         else:
#             ie = SCHEMES[args.input_encoding]
# 
#         word_in = SanskritImmutableString(args.word,
#                                           encoding=ie).canonical()
#         print("Getting tags for", word_in)
#         tags = self.get_tags(word_in, tmap=args.map_tags)
#         if tags is not None:
#             for tag in tags:
#                 print(tag)
# 
# Path: sanskrit_parser/util/inriatagmapper.py
# def inriaTagMapper(tags):
#     ''' Map an INRIA tag to our format
# 
#     Params:
#        tags: List of INRIA format tags
#     Returns
#        list[(<stem>,set([our tags])) ...]
#     '''
#     return list(map(inriaMapTag, tags))
# 
# Path: sanskrit_parser/util/data_manager.py
# def data_file_path(filename):
#     file_manager = ExitStack()
#     atexit.register(file_manager.close)
#     ref = importlib_resources.files('sanskrit_parser') / 'data' / filename
#     path = file_manager.enter_context(
#         importlib_resources.as_file(ref))
#     return str(path)


# Path: sanskrit_parser/util/inriaxmlwrapper.py
import pickle
import sqlite3
import logging
from collections import namedtuple
from sanskrit_parser.base.sanskrit_base import SanskritImmutableString, SCHEMES
from sanskrit_parser.util.lexical_lookup import LexicalLookup
from sanskrit_parser.util.inriatagmapper import inriaTagMapper
from sanskrit_parser.util.data_manager import data_file_path
    from argparse import ArgumentParser

    by Prof. Gerard Huet
    https://gitlab.inria.fr/huet/Heritage_Resources
    """

    '''
    The custom database format has two parts:
        1. A pickle file that contains a list of stems,
           a list of tags, and a serialized buffer of the
           indices of stems and tags for each form. The indices
           are used as it is more efficient to store such integers
           instead of the string for each tag.
        2. An sqlite file that maps each form to the position
           within the buffer that contains the serialized tuple
           of stems and tags for that form. An sqlite database
           is used to avoid having to build a huge dict in
           memory for the 600K forms that are present in this db,
           which consumes a lot of memory. (See
           https://github.com/kmadathil/sanskrit_parser/issues/151)

    To lookup the tag for a form, we use the sqlite db to find the
    position in the buffer, deserialize the data at that position,
    which gives us a list of the tag set for that form. For each
    item in that list, we then lookup the right stem and tag in
    the list of stems and tags loaded from the pickle file
    '''

    def __init__(self, logger=None):
        self.pickle_file = "inria_forms.pickle"
        self.logger = logger or logging.getLogger(__name__)
        db_file = data_file_path("inria_forms_pos.db")

        pkl_path = data_file_path("inria_stems_tags_buf.pkl")