diff options
Diffstat (limited to 'silpa/modules')
-rw-r--r-- | silpa/modules/__init__.py | 5 | ||||
-rw-r--r-- | silpa/modules/anagram/__init__.py | 4 | ||||
-rw-r--r-- | silpa/modules/anagram/anagram.py | 105 | ||||
-rw-r--r-- | silpa/modules/dictionary/dictionary.py | 70 | ||||
-rw-r--r-- | silpa/modules/fortune/fortune.py | 39 | ||||
-rw-r--r-- | silpa/modules/guesslanguages/guess_language.py | 2 | ||||
-rw-r--r-- | silpa/modules/hyphenator/hyphenator.py | 2 | ||||
-rw-r--r-- | silpa/modules/ngram/ngram.py | 16 |
8 files changed, 220 insertions, 23 deletions
diff --git a/silpa/modules/__init__.py b/silpa/modules/__init__.py index 7a3b875..4fe98d1 100644 --- a/silpa/modules/__init__.py +++ b/silpa/modules/__init__.py @@ -1,10 +1,13 @@ #! /usr/bin/env python # -*- coding: utf-8 -*- -from lemmatizer import * +from stemmer import * from payyans import * from transliterator import * from syllabalizer import * from guesslanguages import * from hyphenator import * from fortune import * +from inexactsearch import * +from dictionary import * +from anagram import * diff --git a/silpa/modules/anagram/__init__.py b/silpa/modules/anagram/__init__.py new file mode 100644 index 0000000..34480c2 --- /dev/null +++ b/silpa/modules/anagram/__init__.py @@ -0,0 +1,4 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +import anagram + diff --git a/silpa/modules/anagram/anagram.py b/silpa/modules/anagram/anagram.py new file mode 100644 index 0000000..0147c43 --- /dev/null +++ b/silpa/modules/anagram/anagram.py @@ -0,0 +1,105 @@ +#Anagram Maker +import random +import array +import sys +from common import * +class Anagram(SilpaModule): + def getRandomWord (self): + words = [ i.rstrip () for i in file ('./modules/anagram/ml_IN.dic') ] + len_words = len (words) + randnum=random.randrange(0,len_words) + return [randnum,words[randnum].decode("utf-8")] + + def syllabalize_ml(self,text): + signs = [ + u'\u0d02', u'\u0d03', u'\u0d3e', u'\u0d3f', u'\u0d40', u'\u0d41', + u'\u0d42', u'\u0d43', u'\u0d44', u'\u0d46', u'\u0d47', u'\u0d48', + u'\u0d4a', u'\u0d4b', u'\u0d4c', u'\u0d4d'] + limiters = ['.','\"','\'','`','!',';',',','?'] + + chandrakkala = u'\u0d4d' + lst_chars = [] + for char in text: + if char in limiters: + lst_chars.append(char) + elif char in signs: + lst_chars[-1] = lst_chars[-1] + char + else: + try: + if lst_chars[-1][-1] == chandrakkala: + lst_chars[-1] = lst_chars[-1] + char + else: + lst_chars.append(char) + except IndexError: + lst_chars.append(char) + + return lst_chars + + def scramble(self, word): + newword = "" + randused = [] + i=0 + while i < len(word): + randnum=random.randrange(0, len(word)) + if randnum not in randused: + randused.append(randnum) + #oldchar=word[i] + newword=newword+word[randnum] + i+=1 + #newword[randnum]=oldchar + return newword + + def check_answer(self,ans_hint): + words = [ i.rstrip () for i in file ('./modules/anagram/ml_IN.dic') ] + return words[ans_hint].decode("utf-8") + + def anagram(self): + ans_hint,orig_word=self.getRandomWord() + scrambled_word=self.scramble(self.syllabalize_ml(orig_word)) + return [ans_hint, scrambled_word] + + def process(self, form): + response = """ + <h2>Malayalam Anagram</h2></hr> + <p>Find out the original word from the scrambled word given below. + </p> + <form action="" method="post"> + %s + <br/> + <input type="hidden" name="ans_hint" value="%s"> + <input type="hidden" name="action" value="Anagram"> + <input type="text" cols='100' name='input_text' id='input_text' value="%s"/> + <br/> + <input type="submit" id="anagram" value="Submit" style="width:12em;"/> + <br/> + </form> + """ + if(form.has_key('input_text')): + text = form['input_text'].value .decode('utf-8') + ans_hint= int(form['ans_hint'].value) + answer=self.check_answer(ans_hint) + if(answer==text): + response = response+"<h2>You are correct!</h2></hr>" + response = response+"<b>Answer: "+answer+"</b>" + else: + response = response+"<h2>Your Answer is Wrong!</h2></hr>" + response = response+"<b>Answer: "+answer+"</b>" + response=response % (answer ,ans_hint,text) + else: + text="" + anagram_pair=self.anagram() + ans_hint=anagram_pair[0] + qn_word=anagram_pair[1] + response=response % (qn_word ,ans_hint,text) + return response + def get_module_name(self): + return "Malayalam Anagram" + def get_info(self): + return "Find out the original word from scrambled word!" +def getInstance(): + return Anagram() + +if __name__ == "__main__": + anagram = Anagram() + pair=anagram.anagram() + print pair[0]+"-->"+pair[1] diff --git a/silpa/modules/dictionary/dictionary.py b/silpa/modules/dictionary/dictionary.py new file mode 100644 index 0000000..7ba877a --- /dev/null +++ b/silpa/modules/dictionary/dictionary.py @@ -0,0 +1,70 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# English Malayalam Dictionary +# Copyright 2008 Santhosh Thottingal <santhosh.thottingal@gmail.com> +# http://www.smc.org.in +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU 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 Library 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: santhosh.thottingal@gmail.com +# URL: http://www.smc.org.in + + +from common import * +import os +import pickle +class Dictionary(SilpaModule): + + def lookup_en_ml(self, key): + self.dictFile=os.path.dirname(__file__) + "/data/dict.dat" + pickled_dict=open(self.dictFile,'r') + self.dictionary=pickle.load(pickled_dict) + meaning="" + if self.dictionary.has_key(key): + meaningList=self.dictionary[key] + for meaning_item in meaningList: + meaning=meaning+meaning_item.strip() +"<br/>" + else : + meaning="No Meaning found" + return meaning.decode('utf-8') + def process(self,form): + response = """ + <h2>English Malayalam Dictionary</h2></hr> + <p>Enter the word to lookup in the dictionary + </p> + <form action="" method="get"> + <input type="text" value="%s" name="word"/> + <input type="submit" id="Find_Meaning" value="Find Meaning" name="action" style="width:12em;"/> + </br> + </form> + """ + if(form.has_key('word')): + search_key = form['word'].value.decode('utf-8') + response=response % search_key + response = response+"<h2>Search Results</h2></hr>" + if(search_key==None): + response = response+ "Enter a word to find meaning." + else: + response = response+ self.lookup_en_ml(search_key) + else: + response=response % "" + return response + def get_module_name(self): + return "English Malayalam Dictionary" + def get_info(self): + return "English Malayalam Dictionary. Dictionary is compiled by Kerala state IT Mission" + +def getInstance(): + return Dictionary() diff --git a/silpa/modules/fortune/fortune.py b/silpa/modules/fortune/fortune.py index e7aac98..cf31aad 100644 --- a/silpa/modules/fortune/fortune.py +++ b/silpa/modules/fortune/fortune.py @@ -1,22 +1,37 @@ -# Spellchecker with language detection -# coding: utf-8 +# Fortune +# -*- coding: utf-8 -*- # # Copyright © 2008 Santhosh Thottingal # Released under the GPLV3+ license -import os +import os,random from common import * class Fortune(SilpaModule): + def fortunes(self,infile,pattern=None): + """ Yield fortunes as lists of lines """ + result = [] + for line in infile: + line=line.decode("utf-8") + if line == "%\n": + yield result + result = [] + else: + if(pattern==None): + result.append(line) + else: + if(line.find(pattern)==-1): + result.append(line) + if result: + yield result + def fortune_ml(self, word): - if(word>""): - command = "/usr/games/fortune -m " + word + " ./modules/fortune/database/fortune-ml" - else: - command = "/usr/games/fortune ./modules/fortune/database/fortune-ml" - command=command.encode('utf-8') - pipe = os.popen('{ ' + command + '; } 2>&1', 'r') - text = pipe.read().decode('utf-8') - pipe.close() - return text + filename="./modules/fortune/database/fortune-ml" + """ Pick a random fortune from a file """ + for index, fortune in enumerate(self.fortunes(file(filename),None)): + if random.random() < (1.0 / (index+1)): + chosen = fortune + + return "".join(chosen) def process(self, form): response = """ diff --git a/silpa/modules/guesslanguages/guess_language.py b/silpa/modules/guesslanguages/guess_language.py index 1d88891..158b5f5 100644 --- a/silpa/modules/guesslanguages/guess_language.py +++ b/silpa/modules/guesslanguages/guess_language.py @@ -531,7 +531,7 @@ def normalize(u): class LangGuess(SilpaModule): def process(self, form): response = """ - <h2>Lemmatization</h2></hr> + <h2>Guess the language</h2></hr> <p>Enter the text for guessing the language in the below text area. You can give the text in any language and even with mixed language </p> diff --git a/silpa/modules/hyphenator/hyphenator.py b/silpa/modules/hyphenator/hyphenator.py index 3f223d4..f5e8fe2 100644 --- a/silpa/modules/hyphenator/hyphenator.py +++ b/silpa/modules/hyphenator/hyphenator.py @@ -244,7 +244,7 @@ class Hyphenator(SilpaModule): text = action=form['input_text'].value .decode('utf-8') response=response % text words=text.split(" ") - response = response+"<h2>Language Detection Results</h2></hr>" + response = response+"<h2>Hyphenation Results</h2></hr>" response = response+"<table class=\"table1\"><tr><th>Word</th><th>Hyphenated Word</th></tr>" for word in words: word=word.strip() diff --git a/silpa/modules/ngram/ngram.py b/silpa/modules/ngram/ngram.py index 8f2d65b..cab2ed9 100644 --- a/silpa/modules/ngram/ngram.py +++ b/silpa/modules/ngram/ngram.py @@ -305,7 +305,7 @@ class NGram: pickle.dump(self.getRoot(),open(PICKLED_TREE,'w')) if __name__ == "__main__": usage = "usage: %prog [options] inputfile" - parser = OptionParser(version="%prog 0.1",description="Malayalama NGram Analyser") + parser = OptionParser(version="%prog 0.1",description="Malayalam NGram Analyser") parser.set_usage(usage) parser.add_option("-g", "--generate-graph", dest="gen_graph",help="Generates a graph in png format to visualize the ngram") parser.add_option("-p", "--print", action="store_true",default=False,dest="print_ngram",help="Print the Ngram") @@ -317,9 +317,9 @@ if __name__ == "__main__": if(options.gen_graph): ng = NGram () ng.toGraph(options.gen_graph) - if(options. input_file): + if(options.input_file): if not os.path.exists(options.input_file): - print "File Doesnot Existis" + print "File Doesnot Exist" sys.exit(1) else: corpus_file = codecs. open(options.input_file,encoding='utf-8', errors='ignore') @@ -332,16 +332,16 @@ if __name__ == "__main__": ng.populateSyllableNgram(text) ng.populateWordNgram(text) print "Populated" - if(options. print_ngram): + if(options.print_ngram): ng = NGram () print ng.getRoot().toString() - if(options. suggest_syllables): + if(options.suggest_syllables): ng = NGram () print "Searching for" + options.suggest_words - print ng.searchNodeByName(unicode(options. suggest_syllables)) - if(options. suggest_syllables): + print ng.searchNodeByName(unicode(options.suggest_syllables)) + if(options.suggest_syllables): ng = NGram () print "Searching for "+ options.suggest_words - print ng.searchNodeByName(unicode(options. suggest_words)) + print ng.searchNodeByName(unicode(options.suggest_words)) |