# Path: sesql/config.py
# CONFIG_PATH_FALLBACK = 'sesql_config'
#     CONFIG_PATH = getattr(settings, 'SESQL_CONFIG_PATH', CONFIG_PATH_FALLBACK)
#     CONFIG_PATH = CONFIG_PATH_FALLBACK
# 
# Path: sesql/fieldmap.py
# class FieldMap(object):
#     def __init__(self):
#     def all_fields(self):
#     def get_field(self, field):
#     def get_primary(self):


from sesql import config
from sesql.fieldmap import fieldmap
from GenericCache import GenericCache
# along with SeSQL.  If not, see <http://www.gnu.org/licenses/>.


# Maximal number of words to lemmatize at once
MAX_WORDS = 1000

# Use GenericCache for now, but will probably be moved to memcached later
_word_cache = GenericCache(maxsize = 50000, expiry = 86400)

def lemmatize_for(words, dictionnary):
    """
    Lemmatize a word with given dictionnary
    """
    if len(words) > MAX_WORDS:
        return lemmatize_for(words[:MAX_WORDS], dictionnary) + lemmatize_for(words[MAX_WORDS:], dictionnary)

    values = {}
    remaining = []

    for word in words:
        value = _word_cache[(word, dictionnary)]
        if value is not None:
            values[word] = value
        else:
            remaining.append(word)

    if remaining:
        pattern = "plainto_tsquery('%s', %%s)" % dictionnary
        patterns = [ pattern for word in remaining ]


# Path: sesql/config.py
# CONFIG_PATH_FALLBACK = 'sesql_config'
#     CONFIG_PATH = getattr(settings, 'SESQL_CONFIG_PATH', CONFIG_PATH_FALLBACK)
#     CONFIG_PATH = CONFIG_PATH_FALLBACK
# 
# Path: sesql/fieldmap.py
# class FieldMap(object):
#     def __init__(self):
#     def all_fields(self):
#     def get_field(self, field):
#     def get_primary(self):


# Path: sesql/lemmatize.py
from sesql import config
from sesql.fieldmap import fieldmap
from GenericCache import GenericCache

# along with SeSQL.  If not, see <http://www.gnu.org/licenses/>.


# Maximal number of words to lemmatize at once
MAX_WORDS = 1000

# Use GenericCache for now, but will probably be moved to memcached later
_word_cache = GenericCache(maxsize = 50000, expiry = 86400)

def lemmatize_for(words, dictionnary):
    """
    Lemmatize a word with given dictionnary
    """
    if len(words) > MAX_WORDS:
        return lemmatize_for(words[:MAX_WORDS], dictionnary) + lemmatize_for(words[MAX_WORDS:], dictionnary)

    values = {}
    remaining = []

    for word in words:
        value = _word_cache[(word, dictionnary)]
        if value is not None:
            values[word] = value
        else:
            remaining.append(word)

    if remaining:
        pattern = "plainto_tsquery('%s', %%s)" % dictionnary
        patterns = [ pattern for word in remaining ]


        cursor = config.orm.cursor()