# Natural Language Toolkit: PanLex Corpus Reader
#
# Copyright (C) 2001-2015 NLTK Project
# Author: David Kamholz <kamholz@panlex.org>
# URL: <http://nltk.org/>
# For license information, see LICENSE.TXT

"""
Module docstring from readme
"""

import os
import sqlite3

from nltk.corpus.reader.util import *
from nltk.corpus.reader.api import *

class PanLexCorpusReader(CorpusReader):
    MEANING_Q = """
        SELECT dnx2.mn, dnx2.uq, dnx2.ap, dnx2.ui, ex2.tt, ex2.lv
        FROM dnx
        JOIN ex ON (ex.ex = dnx.ex)
        JOIN dnx dnx2 ON (dnx2.mn = dnx.mn)
        JOIN ex ex2 ON (ex2.ex = dnx2.ex)
        WHERE dnx.ex != dnx2.ex AND ex.tt = ? AND ex.lv = ?
        ORDER BY dnx2.uq DESC
    """

    def __init__(self):
        self._conn = sqlite3.connect(os.environ['PANLEX_LITE'])
        self._c = self._conn.cursor()

        self._uid_lv = {}
        self._lv_uid = {}

        for row in self._c.execute('SELECT uid, lv, lc FROM lv'):
            self._uid_lv[row[0]] = row[1]
            self._lv_uid[row[1]] = row[0]

    def language_varieties(self, lc=None):
        """given "eng" returns ["eng-001", ...]
        """

        lvs = []

        if lc == None:
            for row in self._c.execute('SELECT uid FROM lv'):
                lvs.append(row[0])
        else:
            for row in self._c.execute('SELECT uid FROM lv WHERE lc = ?', (lc,)):
                lvs.append(row[0])

        return sorted(lvs)

    def meanings(self, expr_tt, expr_uid):
        """
        >>> panlex.meanings("book", "eng-001")

        """

        expr_lv = self._uid_lv[expr_uid]

        mn_info = {}

        for i in self._c.execute(self.MEANING_Q, (expr_tt, expr_lv)):
            mn = i[0]
            uid = self._lv_uid[i[5]]

            if not mn in mn_info:
                mn_info[mn] = { 'uq': i[1], 'ap': i[2], 'ui': i[3], 'ex': {} }

            if not uid in mn_info[mn]['ex']:
                mn_info[mn]['ex'][uid] = []

            mn_info[mn]['ex'][uid].append(i[4])

        mns = []

        for mn in mn_info:
            mns.append(Meaning(mn, mn_info[mn]))

        return mns

class Meaning(object):
    def __init__(self, mn, attr):
        self = {
            'mn': mn,
            'uq': attr['uq'],
            'ap': attr['ap'],
            'ui': attr['ui'],
            'ex': attr['ex']
        }

    def id(self):
        return self['mn']

    def quality(self):
        return self['uq']

    def source(self):
        return self['ap']

    def source_group(self):
        return self['ui']

    def expressions(self):
        return self['ex']

@@ -698,24 +698,483 @@
 SC%0A    %22%22%22%0A%0A
+    TRANSLATION_Q = %22%22%22%0A        SELECT s.tt, sum(s.uq) AS trq FROM (%0A            SELECT ex2.tt, max(dnx.uq) AS uq%0A            FROM dnx%0A            JOIN ex ON (ex.ex = dnx.ex)%0A            JOIN dnx dnx2 ON (dnx2.mn = dnx.mn)%0A            JOIN ex ex2 ON (ex2.ex = dnx2.ex)%0A            WHERE dnx.ex != dnx2.ex AND ex.lv = ? AND ex.tt = ? AND ex2.lv = ?%0A            GROUP BY ex2.tt, dnx.ui%0A        ) s%0A        GROUP BY s.tt%0A        ORDER BY trq DESC, s.tt%0A    %22%22%22%0A%0A
     def __in
@@ -1926,20 +1926,20 @@
 xpr_
-tt
+uid
 , expr_
-uid
+tt
 ):%0A 
@@ -1982,23 +1982,23 @@
 gs(%22
-book%22, %22eng-001
+eng-000%22, %22book
 %22)%0A%0A
@@ -2572,16 +2572,240 @@
 rn mns%0A%0A
+    def translations(self, from_uid, from_tt, to_uid):%0A        from_lv = self._uid_lv%5Bfrom_uid%5D%0A        to_lv = self._uid_lv%5Bto_uid%5D%0A%0A        return self._c.execute(self.TRANSLATION_Q, (from_lv, from_tt, to_lv)).fetchall()%0A%0A
 class Me

add translations method to panlex reader