From 97106eeee9a7fe946d59e4d80459874412d8c206 Mon Sep 17 00:00:00 2001 From: Santhosh Thottingal Date: Sat, 25 Apr 2009 20:38:12 +0530 Subject: Anagram Module --- silpa/modules/anagram/anagram.py | 105 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 silpa/modules/anagram/anagram.py (limited to 'silpa/modules/anagram/anagram.py') 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 = """ +

Malayalam Anagram

+

Find out the original word from the scrambled word given below. +

+
+ %s +
+ + + +
+ +
+
+ """ + 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+"

You are correct!

" + response = response+"Answer: "+answer+"" + else: + response = response+"

Your Answer is Wrong!

" + response = response+"Answer: "+answer+"" + 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] -- cgit