<commit_before>#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Dictionary
# Copyright 2008 <NAME> <<EMAIL>>
# http://www.smc.org.in
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# If you find any bugs or have any suggestions email: <EMAIL>
# URL: http://www.smc.org.in


from common import *
import os
from dictdlib import DictDB
from jsonrpc import *
class Dictionary(SilpaModule):
    
    def __init__(self):
        self.template=os.path.join(os.path.dirname(__file__), 'dictionary.html')    
        self.response = SilpaResponse(self.template)        
        
    def get_json_result(self):
        error=None
        _id = 0
        try:
            if self.request.get('word'):
                definition = self.getdef(self.request.get('word'),self.request.get('dictionary'))
            data = dumps({"result":definition, "id":_id, "error":error})
        except JSONEncodeException:
            #translate the exception also to the error
            error = {"name": "JSONEncodeException", "message":"Result Object Not Serializable"}
            data = dumps({"result":None, "id":id_, "error":error})
        return data
        
    def get_form(self):
        page = open(self.template,'r').read()
        return page
        
    def get_free_dict(self, src, dest):
        dict_dir=os.path.join(os.path.dirname(__file__), 'dictionaries')
        dictdata=dict_dir+ "/freedict-"+src+"-"+dest
        if os.path.isfile(dictdata+".index"):
            return dictdata
        return None    

    @ServiceMethod  
    def getdef(self, word, dictionary):
        meaningstring= ""
        src = dictionary.split("-")[0]
        dest = dictionary.split("-")[1]
        dictdata = self.get_free_dict(src,dest)
        if dictdata:
            dict = DictDB(dictdata)
            meanings =  dict.getdef(word)
            for meaning in meanings:
                meaningstring += meaning
        if meaningstring == "None":
            meaningstring = "No definition found"<commit_msg>Fixed bug #32786: Dictionary - Failure to retrieve meaning for inputs with Uppercase first letter<commit_after>#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Dictionary
# Copyright 2008 <NAME> <<EMAIL>>
# http://www.smc.org.in
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# If you find any bugs or have any suggestions email: <EMAIL>
# URL: http://www.smc.org.in


from common import *
import os
from dictdlib import DictDB
from jsonrpc import *
class Dictionary(SilpaModule):
    
    def __init__(self):
        self.template=os.path.join(os.path.dirname(__file__), 'dictionary.html')    
        self.response = SilpaResponse(self.template)        
        
    def get_json_result(self):
        error=None
        _id = 0
        try:
            if self.request.get('word'):
                definition = self.getdef(self.request.get('word'),self.request.get('dictionary'))
            data = dumps({"result":definition, "id":_id, "error":error})
        except JSONEncodeException:
            #translate the exception also to the error
            error = {"name": "JSONEncodeException", "message":"Result Object Not Serializable"}
            data = dumps({"result":None, "id":id_, "error":error})
        return data
        
    def get_form(self):
        page = open(self.template,'r').read()
        return page
        
    def get_free_dict(self, src, dest):
        dict_dir=os.path.join(os.path.dirname(__file__), 'dictionaries')
        dictdata=dict_dir+ "/freedict-"+src+"-"+dest
        if os.path.isfile(dictdata+".index"):
            return dictdata
        return None    

    @ServiceMethod  
    def getdef(self, word, dictionary):
        meaningstring= ""
        src = dictionary.split("-")[0]
        dest = dictionary.split("-")[1]
        dictdata = self.get_free_dict(src,dest)
        if dictdata:
            dict = DictDB(dictdata)
            meanings =  dict.getdef(word.lower())
            for meaning in meanings:
                meaningstring += meaning
        if meaningstring == "None":
            meaningstring = "No definition found"
{'fraction_non_alphanumeric': 0.10726133431625506, 'fraction_numerical': 0.009399189089568744, 'mean_word_length': 2.679322033898305, 'pattern_counts': {'":': 18, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1777550', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1493, 'n_words': 504}