def __init__(self, JID, Password): """ Create a new bot. Connect to the server and log in. """ # connect... jid = xmpp.JID(JID) self.connection = xmpp.Client(jid.getDomain(), debug=[]) self.en_ml_db = None result = self.connection.connect() if result is None: raise ConnectionError # authorize result = self.connection.auth(jid.getNode(), Password) if result is None: raise AuthorizationError self.connection.RegisterHandler('presence',self.presenceHandler) self.connection.RegisterHandler('message',self.messageHandler) # ...become available self.connection.sendInitPresence() # presence #self.connection.sendInitPresence(requestRoster=0) try: #search the dictionary in same directory of program self.en_ml_db = DictDB("freedict-eng-mal") except: #retry in standard directory of dictd self.en_ml_db = DictDB("/usr/share/dictd/freedict-eng-mal")
def __init__(self, JID, Password): """ Create a new bot. Connect to the server and log in. """ # connect... jid = xmpp.JID(JID) self.connection = xmpp.Client(jid.getDomain(), debug=[]) self.en_ml_db = None result = self.connection.connect() if result is None: raise ConnectionError # authorize result = self.connection.auth(jid.getNode(), Password) if result is None: raise AuthorizationError self.connection.RegisterHandler('presence', self.presenceHandler) self.connection.RegisterHandler('message', self.messageHandler) # ...become available self.connection.sendInitPresence() # presence #self.connection.sendInitPresence(requestRoster=0) try: #search the dictionary in same directory of program self.en_ml_db = DictDB("freedict-eng-mal") except: #retry in standard directory of dictd self.en_ml_db = DictDB("/usr/share/dictd/freedict-eng-mal")
def convert(args): """ This scripts cleans dwa file and creates DictDB for GoldenDict. Requires dictdlib library:: sudo apt-get install python-dictdlib """ input_name, locale_name = args base_name = os.path.splitext(input_name)[0] output_name = base_name clean_name = base_name + '.clean.dwa' clean(input_name, clean_name, locale_name) from dictdlib import DictDB db = DictDB(output_name.decode('utf-8'), mode='write') with open(clean_name) as fin: for i, line in enumerate(fin): try: word, meaning = line.decode('utf-8')[:-1].split('=', 1) match = re.match(r'(.*)(\w*)(\d*)', word, re.UNICODE) db.addentry( meaning.encode('utf-8'), [match.groups()[0].encode('utf-8')]) except: print i, line raise db.finish()
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" return meaningstring return meaningstring.decode("utf-8")
def getdef(self, word, dictionary): """ :param word: The word which has to be looked up. :param dictionary: The convertion needed, format src-dest >>> """ src = dictionary.split("-")[0] dest = dictionary.split("-")[1] dictdata = self.get_free_dict(src, dest) if dictdata: meaningstring = "" dict = DictDB(dictdata) clean_word = word.lower() clean_word = clean_word.strip() meanings = dict.getdef(clean_word) for meaning in meanings: meaningstring += meaning if meaningstring == "None": meaningstring = "No definition found" return meaningstring return meaningstring.decode("utf-8")
def getdef(self, word): en_hi_db = None hi_en_db = None meaning_str = "" #search the dictionary in same directory of program en_hi_db = DictDB("/usr/share/dictd/freedict-eng-hin") hi_en_db = DictDB("/usr/share/dictd/freedict-hin-eng") if en_hi_db == None or hi_en_db == None: return "[FATAL ERROR] Dictionary not found." try: meanings = en_hi_db.getdef(word) for meaning in meanings: meaning_str+=meaning meanings = hi_en_db.getdef(word.encode("utf-8")) for meaning in meanings: meaning_str+=meaning if meaning_str == "" : return "Sorry, No definition found." else: return meaning_str except: return "Sorry, No definition found."
def __init__(self): self.db_connection = None self.en_ml_db = DictDB("freedict-eng-mal")
class MLDictBot(object): def __init__(self): self.db_connection = None self.en_ml_db = DictDB("freedict-eng-mal") def process_requests(self,status,dict_pos): if self.db_connection == None: self.__initialize_db__() if not self.__is_processed__(str(status.id)): # len('dict')+space = 5 word = status.text[dict_pos + 5:] try: definition = self.en_ml_db.getdef(word)[0] except: definition = "No definition!" self.__mark_undefined__(word) self.__update_ledger__(status) return definition def __initialize_db__(self): self.db_connection = sqlite3.connect("/tmp/mlbot.db") c = self.db_connection.cursor() c.execute(''' create table if not exists ml_bot_ledger( _id integer primary key autoincrement, tweet_id text not null) ''') c.execute(''' create table if not exists ml_bot_undefined( _id integer primary key autoincrement, word text not null, frequency integer not null) ''') self.db_connection.commit() c.close() def __is_processed__(self,tweet_id): c = self.db_connection.cursor() c.execute('select count(*) from ml_bot_ledger where tweet_id = ?',(tweet_id,)) count = c.fetchone() if count[0] == 0: return False else: return True c.close() def __mark_undefined__(self,word): if len(word.split(' ')) == 1: # If request has mutliple word # discard it c = self.db_connection.cursor() c.execute('select count(*) from ml_bot_undefined where word = ?',(word,)) count = c.fetchone() if count[0] == 0: c.execute('insert into ml_bot_undefined (word,frequency) values (?,?)',(word,1)) else: c.execute('update ml_bot_undefined set frequency = frequency + 1 where word = ?',(word,)) self.db_connection.commit() c.close() def __update_ledger__(self,status): c = self.db_connection.cursor() c.execute("insert into ml_bot_ledger (tweet_id) values (?)",(status.id,)) self.db_connection.commit() c.close()
class Bot: """ The main bot class. """ def __init__(self, JID, Password): """ Create a new bot. Connect to the server and log in. """ # connect... jid = xmpp.JID(JID) self.connection = xmpp.Client(jid.getDomain(), debug=[]) self.en_ml_db = None result = self.connection.connect() if result is None: raise ConnectionError # authorize result = self.connection.auth(jid.getNode(), Password) if result is None: raise AuthorizationError self.connection.RegisterHandler('presence', self.presenceHandler) self.connection.RegisterHandler('message', self.messageHandler) # ...become available self.connection.sendInitPresence() # presence #self.connection.sendInitPresence(requestRoster=0) try: #search the dictionary in same directory of program self.en_ml_db = DictDB("freedict-eng-mal") except: #retry in standard directory of dictd self.en_ml_db = DictDB("/usr/share/dictd/freedict-eng-mal") def loop(self): """ Do nothing except handling new xmpp stanzas. """ try: while self.connection.Process(1): pass except KeyboardInterrupt: pass def messageHandler(self, conn, message_node): word = message_node.getBody() output = "" if word: if word == "hi" or word == "Hi" or word == "Hello" or word == "hello": output = "നമസ്കാരം! \n" output += "ഞാന് സ്വതന്ത്ര മലയാളം കമ്പ്യൂട്ടിങ്ങിന്റെ ഇംഗ്ലീഷ് മലയാളം നിഘണ്ടു." + "\n" output += "ഇംഗ്ലീഷ് വാക്കുകളുടെ അര്ത്ഥം കണ്ടുപിടിക്കാന് എനിക്കു നിങ്ങളെ സഹായിക്കാന് കഴിയും." + "\n" output += "അര്ത്ഥമറിയേണ്ട വാക്കു് ചോദിച്ചോളൂ." + "\n" output += "ശുഭദിനാശംസകള് നേരുന്നു!" else: dictoutput = self.getdef(word) if dictoutput: output += "From SMC English-Malayalam Dictionary:\n" output += dictoutput conn.send(xmpp.Message(message_node.getFrom(), output)) output = "" wikioutput = wiktionary.get_def(word, "ml", "ml") if wikioutput: output += "From Malayalam wiktionary:\n" output += wikioutput.encode("utf-8") if dictoutput == None and wikioutput == None: output = "ക്ഷമിക്കണം. ഈ വാക്കിന്റെ അര്ത്ഥം കണ്ടെത്താനായില്ല." hun = hunspell.HunSpell( '/usr/share/myspell/dicts/en_US.dic', '/usr/share/myspell/dicts/en_US.aff') output += "\nDid you mean: \n" if hun.spell(word) is False: wordlist = hun.suggest(word) #print wordlist for suggword in wordlist: #print suggword hdictoutput = self.getdef(suggword) hwikioutput = wiktionary.get_def( suggword, "ml", "ml") if hdictoutput is not None or hwikioutput is not None: output += "\t" + suggword + "\n" conn.send(xmpp.Message(message_node.getFrom(), output)) raise NodeProcessed # This stanza is fully processed def getdef(self, word): if self.en_ml_db == None: return "[FATAL ERROR] Dictionary not found." try: word = word.lower() return self.en_ml_db.getdef(word)[0] except: return None def presenceHandler(self, conn, presence): '''Auto authorizing chat invites''' if presence: if presence.getType() == 'subscribe': jabber_id = presence.getFrom().getStripped() self.connection.getRoster().Authorize(jabber_id) print presence.getFrom().getStripped()
tweet = s.user.name + "\t" + s.text dict_keyword_find = tweet.find(username + "dict") if dict_keyword_find > 0: fin = open('dataFile', 'r') fin_contents = fin.read() check_duplicate = fin_contents.find(str(s.id)) print check_duplicate fin.close() if check_duplicate < 0: print "%s --> %s" % (s.user.name, s.text) word = s.text[13:] print word #for debugging en_ml_db = DictDB("freedict-eng-mal") try: definition = en_ml_db.getdef(word)[0] except: definition = "No definitions found" print definition defi = definition[0:110] output = '@' + s.user.screen_name + ' ' + defi #print len(output) print output api.PostUpdate(output.decode("utf-8", 'ignore')) sleep_time = 30 #Adjusting time to make the bot twitter-server friendly fout = open('dataFile', 'a') text = '\n' #To write each status id in a new line text = text + str(s.id)
#! /usr/bin/env python # -*- coding: utf-8 -*- # Dictionary Client # Copyright 2008 Santhosh Thottingal <*****@*****.**> # 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 protected] # URL: http://www.smc.org.in from dictdlib import DictDB en_ml_db = DictDB("/usr/share/dictd/freedict-eng-mal") print en_ml_db.getdef('help')[0] ml_ml_db = DictDB("/usr/share/dictd/dict-ml-ml") print en_ml_db.getdef('ഭയങ്കരം')
tweet = s.user.name + "\t" + s.text dict_keyword_find = tweet.find(username + "dict") if dict_keyword_find > 0: fin = open("dataFile", "r") fin_contents = fin.read() check_duplicate = fin_contents.find(str(s.id)) print check_duplicate fin.close() if check_duplicate < 0: print "%s --> %s" % (s.user.name, s.text) word = s.text[13:] print word # for debugging en_ml_db = DictDB("freedict-eng-mal") try: definition = en_ml_db.getdef(word)[0] except: definition = "No definitions found" print definition defi = definition[0:110] output = "@" + s.user.screen_name + " " + defi # print len(output) print output api.PostUpdate(output.decode("utf-8", "ignore")) sleep_time = 30 # Adjusting time to make the bot twitter-server friendly fout = open("dataFile", "a") text = "\n" # To write each status id in a new line text = text + str(s.id)
#! /usr/bin/env python # -*- coding: utf-8 -*- # Dictionary Client # Copyright 2008 Santhosh Thottingal <*****@*****.**> # 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 protected] # URL: http://www.smc.org.in from dictdlib import DictDB en_ml_db=DictDB("/usr/share/dictd/freedict-eng-mal") print en_ml_db.getdef('help')[0] ml_ml_db=DictDB("/usr/share/dictd/dict-ml-ml") print en_ml_db.getdef('ഭയങ്കരം')
class Bot: """ The main bot class. """ def __init__(self, JID, Password): """ Create a new bot. Connect to the server and log in. """ # connect... jid = xmpp.JID(JID) self.connection = xmpp.Client(jid.getDomain(), debug=[]) self.en_ml_db = None result = self.connection.connect() if result is None: raise ConnectionError # authorize result = self.connection.auth(jid.getNode(), Password) if result is None: raise AuthorizationError self.connection.RegisterHandler('presence',self.presenceHandler) self.connection.RegisterHandler('message',self.messageHandler) # ...become available self.connection.sendInitPresence() # presence #self.connection.sendInitPresence(requestRoster=0) try: #search the dictionary in same directory of program self.en_ml_db = DictDB("freedict-eng-mal") except: #retry in standard directory of dictd self.en_ml_db = DictDB("/usr/share/dictd/freedict-eng-mal") def loop(self): """ Do nothing except handling new xmpp stanzas. """ try: while self.connection.Process(1): pass except KeyboardInterrupt: pass def messageHandler(self, conn,message_node): word = message_node.getBody() output= "" if word : if word.lower() == "hi" or word.lower() == "hello" : output = "നമസ്കാരം! \n" output += "ഞാന് സ്വതന്ത്ര മലയാളം കമ്പ്യൂട്ടിങ്ങിന്റെ ഇംഗ്ലീഷ് മലയാളം നിഘണ്ടു."+"\n" output += "ഇംഗ്ലീഷ് വാക്കുകളുടെ അര്ത്ഥം കണ്ടുപിടിക്കാന് എനിക്കു നിങ്ങളെ സഹായിക്കാന് കഴിയും."+"\n" output += "അര്ത്ഥമറിയേണ്ട വാക്കു് ചോദിച്ചോളൂ."+"\n" output += "ശുഭദിനാശംസകള് നേരുന്നു!" else: dictoutput = self.getdef(word) if dictoutput: output += "From SMC English-Malayalam Dictionary:\n" output += dictoutput conn.send( xmpp.Message( message_node.getFrom() ,output)) output = "" wikioutput = wiktionary.get_def(word, "ml","ml") if wikioutput: output += "From Malayalam wiktionary:\n" output += wikioutput.encode("utf-8") if dictoutput== None and wikioutput==None: output = "ക്ഷമിക്കണം. ഈ വാക്കിന്റെ അര്ത്ഥം കണ്ടെത്താനായില്ല." hun = hunspell.HunSpell('/usr/share/myspell/dicts/en_US.dic', '/usr/share/myspell/dicts/en_US.aff') output += "\nDid you mean: \n" if hun.spell(word) is False: wordlist = hun.suggest(word) #print wordlist for suggword in wordlist: #print suggword hdictoutput = self.getdef(suggword) hwikioutput = wiktionary.get_def(suggword, "ml","ml") if hdictoutput is not None or hwikioutput is not None: output+= "\t" + suggword + "\n" conn.send( xmpp.Message( message_node.getFrom() ,output)) raise NodeProcessed # This stanza is fully processed def getdef(self, word): if self.en_ml_db == None: return "[FATAL ERROR] Dictionary not found." try: word = word.lower() return self.en_ml_db.getdef(word)[0] except: return None def presenceHandler(self, conn, presence): '''Auto authorizing chat invites''' if presence: if presence.getType() == 'subscribe': jabber_id = presence.getFrom().getStripped() self.connection.getRoster().Authorize(jabber_id) print presence.getFrom().getStripped()