summaryrefslogtreecommitdiffstats
path: root/silpa/modules/anagram/anagram.py
blob: 0147c430d4368746f21ce83e20e03cc170b00fe6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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]