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/css/main.css | 1 + silpa/modules/__init__.py | 1 + silpa/modules/anagram/__init__.py | 4 ++ silpa/modules/anagram/anagram.py | 105 ++++++++++++++++++++++++++++++++++++++ silpa/silpa.conf | 1 + silpa/templates/base.py | 6 +-- 6 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 silpa/modules/anagram/__init__.py create mode 100644 silpa/modules/anagram/anagram.py diff --git a/silpa/css/main.css b/silpa/css/main.css index 70658e6..851efae 100644 --- a/silpa/css/main.css +++ b/silpa/css/main.css @@ -24,6 +24,7 @@ a:hover,a:active {color:#069} /* FORMS */ form {margin: 0 0 1.5em} input {font-family: arial,tahoma,verdana,sans-serif;margin: 2px 0} +textarea {font-family: arial,tahoma,verdana,sans-serif;margin: 2px 0;width: 100%;height:300px} fieldset {border: none} label {display:block;padding: 5px 0} label br {clear:left} diff --git a/silpa/modules/__init__.py b/silpa/modules/__init__.py index c46904b..4fe98d1 100644 --- a/silpa/modules/__init__.py +++ b/silpa/modules/__init__.py @@ -9,4 +9,5 @@ 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 = """ +

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] diff --git a/silpa/silpa.conf b/silpa/silpa.conf index 7e47cad..068112c 100644 --- a/silpa/silpa.conf +++ b/silpa/silpa.conf @@ -25,5 +25,6 @@ SILPA_ACTION.Syllabalize=modules.syllabalizer #Syllabalizer module SILPA_ACTION.Hyphenate=modules.hyphenator #Syllabalizer module SILPA_ACTION.Find_Meaning=modules.dictionary #English Malayalam Module SILPA_ACTION.Fortune=modules.fortune #Syllabalizer module +SILPA_ACTION.Anagram=modules.anagram #Anagram module SILPA_ACTION.Approximate_Search=modules.inexactsearch #Approximate search #End of Silpa configuration file diff --git a/silpa/templates/base.py b/silpa/templates/base.py index c4be9c4..00d6586 100644 --- a/silpa/templates/base.py +++ b/silpa/templates/base.py @@ -63,7 +63,7 @@ http://creativecommons.org/licenses/GPL/2.0/