def test_word_with_three_should_return_max_result_6(self): result_one = anagram.returnAnagram("cat") result_two = anagram.returnAnagram("dry") expected_one = ['cat', 'cta', 'act', 'atc', 'tac', 'tca'] expected_two = ['dry', 'dyr', 'ryd', 'rdy', 'yrd', 'ydr'] self.assertCountEqual(result_one, expected_one) self.assertCountEqual(result_two, expected_two)
def test_letter_should_return_itself(self): result_one = anagram.returnAnagram("a") result_two = anagram.returnAnagram("B") self.assertEqual(["a"], result_one) self.assertEqual(["B"], result_two) self.assertEqual(len(result_one), 1) self.assertEqual(len(result_two), 1)
def test_word_with_length_two_should_return_max_result_2(self): result_one = anagram.returnAnagram("de") result_two = anagram.returnAnagram("fk") expected_one = ['de', 'ed'] expected_two = ['fk', 'kf'] self.assertCountEqual(result_one, expected_one) self.assertCountEqual(result_two, expected_two)
def post(self): self.response.headers["Content-Type"] = "text/html" file = self.request.get("textfile") openFile = open(file) readLine = openFile.readline() while readLine: text_word = (readLine.strip('\n\r')).lower() sorted_key = Services().sorted_key(word=text_word) list_word = Anagram.query() list_word = list_word.filter( Anagram.anagram_key == sorted_key, Anagram.user_id == Services().get_current_user_id()) list_word = list_word.fetch() valid_permutation = Services().validpermutations(text=sorted_key) if len(valid_permutation) > 0: if len(list_word) > 0: anagram = list_word[0] if text_word not in anagram.inputed_words: inputed_words = anagram.inputed_words inputed_words.append(text_word) anagram.inputed_words = inputed_words anagram.inputed_words_count = anagram.inputed_words_count + 1 anagram.put() else: new_anagram = Anagram( anagram_key=sorted_key, anagram_words=Services().possiblepermutations( text=sorted_key), inputed_words=[text_word], inputed_words_count=1, word_length=len(text_word), user_id=Services().get_current_user_id()) new_anagram.put() readLine = openFile.readline() openFile.close() message = "Upload completed" template_values = {'message': message} template = JINJA_ENVIRONMENT.get_template("importdata.html") self.response.write(template.render(template_values))
def test_word_with_repeated_letter_should_not_return_duplicate_result( self): result_one = anagram.returnAnagram("dd") result_two = anagram.returnAnagram("ebb") expected_one = ['dd'] expected_two = ['ebb', 'beb', 'bbe'] self.assertCountEqual(result_one, expected_one) self.assertCountEqual(result_two, expected_two)
def test_insert_request(self): anagram = Anagram() anagram.insertRequest("abc", "bca") anagram.insertRequest("abc", "bca") anagram.insertRequest("bca", "abc") anagram.insertRequest("tyyy", "yyyt") anagram.insertRequest("yyyt", "tyyy") # insert other 2 other requests anagram.insertRequest("kuk", "kku") anagram.insertRequest("rrr", "rrr") self.assertEqual(len(anagram.requests.values()), 4, "Should have 4 different requests")
def post(self): self.response.headers["Content-Type"] = "text/html" if self.request.get("addword") == "Add Word": user_word = self.request.get("word").lower() if Services().get_current_user() == None or user_word == None or user_word == "" : self.redirect("/addword") return current_user_id = Services().get_current_user_id() sorted_key = Services().sorted_key(word =user_word) list_word = Anagram.query() list_word = list_word.filter(Anagram.anagram_key == sorted_key,Anagram.user_id == current_user_id) list_word = list_word.fetch() valid_permutation = Services().validpermutations(text=sorted_key) if len(valid_permutation) == 0: self.redirect("/addword") return if len(list_word) > 0: anagram = list_word[0] if user_word in anagram.inputed_words: self.redirect("/addword") return inputed_words = anagram.inputed_words inputed_words.append(user_word) anagram.inputed_words = inputed_words anagram.inputed_words_count = anagram.inputed_words_count + 1 anagram.put() else: new_anagram = Anagram(anagram_key=sorted_key, anagram_words = Services().possiblepermutations(text=sorted_key), inputed_words = [user_word], inputed_words_count = 1, word_length = len(user_word), user_id = current_user_id) new_anagram.put() self.redirect("/")
def addanagram_new(my_user, text, words, anagram_id, anagram_key): if englishword(text): anagram = Anagram(id=anagram_id) anagram.words.append(text) anagram.sorted_word = generateid_for_users(text) anagram.length = len(text) anagram.words_count = len(anagram.words) anagram.sub_words = words anagram.user_id = my_user.key.id() anagram.put() my_user.anagrams.append(anagram_key) my_user.put()
def get(self): self.response.headers['Content-Type'] = 'text/html' user = users.get_current_user() if user == None: template_values = { 'login_url': users.create_login_url(self.request.uri) } template = JINJA_ENVIRONMENT.get_template('mainpage_guest.html') self.response.write(template.render(template_values)) return myuser_key = ndb.Key('MyUser', user.user_id()) myuser = myuser_key.get() if myuser == None: myuser = MyUser(username=user.email(), id=user.user_id(), myAnagram=0, myWordCount=0) myuser.put() ana = Anagram.query().fetch() # c = Counter(); template_values = { 'logout_url': users.create_logout_url(self.request.uri), 'user': user, 'myuser': myuser, 'ana': ana # , # 'counter': getAnaCount() } template = JINJA_ENVIRONMENT.get_template('main.html') self.response.write(template.render(template_values))
def post(self): self.response.headers['Content-Type'] = 'text/html' action = self.request.get('button') if action == 'SEARCH': user = users.get_current_user() myuser_key = ndb.Key('MyUser', user.user_id()) myuser = myuser_key.get() searchText = (self.request.get('word')).lower() if searchText == "": message = "Word not found" template_values = { 'user': user, 'message': "Word not found", 'ana': Anagram.query().fetch() } template = JINJA_ENVIRONMENT.get_template('main.html') self.response.write(template.render(template_values)) else: search_key = lexi(searchText) ana_key = ndb.Key("Anagram", user.email() + search_key) ana = ana_key.get() template_values = {'ana': ana, 'user': user, 'myuser': myuser} template = JINJA_ENVIRONMENT.get_template('main.html') self.response.write(template.render(template_values))
def all_perms(): data = request.json anagram_output = Anagram.return_anagrams(data['word']) return { 'status': 200, 'anagrams': anagram_output, }
def post(self): self.response.headers["Content-Type"] = "text/html" # checking whether the key is add word if self.request.get("newdevice") == "Add Word": inputed_word = self.request.get("word").lower() # checking whether the inputed word is null or not and redirecting to the add word paeg if Services().get_current_user( ) == None or inputed_word == None or inputed_word == "": self.redirect("/newdevice") return current_user_id = Services().get_current_user_id() sorted_key = Services().sorted_key(word=inputed_word) list_word = Anagram.query() list_word = list_word.filter(Anagram.anagram_key == sorted_key, Anagram.user_id == current_user_id) list_word = list_word.fetch() valid_permutation = Services().validpermutations(text=sorted_key) # checking whether the inputed word has valid permutations or not if len(valid_permutation) == 0: self.redirect("/newdevice") return # checking whether the there are any word in the list or redirecting to the add word page if len(list_word) > 0: anagram = list_word[0] if inputed_word in anagram.input_words: self.redirect("/newdevice") return inputed_words = anagram.input_words inputed_words.append(inputed_word) anagram.input_words = inputed_words anagram.input_words_count = anagram.input_words_count + 1 anagram.put() else: new_anagram = Anagram( anagram_key=sorted_key, anagram_words=Services().permutations(text=sorted_key), input_words=[inputed_word], input_words_count=1, word_length=len(inputed_word), user_id=current_user_id) new_anagram.put() self.redirect("/")
def post(self): self.response.headers['Content-Type'] = 'text/html' action = self.request.get('button') user = users.get_current_user() user_key = ndb.Key('MyUser', user.user_id()) user_info = user_key.get() if action == 'Add': original_word = (self.request.get('Word')).lower() if not re.match("^[a-z]*$", original_word.lower()): message = "Please enter alphabets only" else: Word = lexi(self.request.get('Word')) anagram_key = ndb.Key('Anagram', user.email() + Word) anagram = anagram_key.get() # & anagram.User != user if anagram == None: new_anagram = Anagram(id=user.email() + Word, AnagramKey=Word) new_anagram.User = user.email() new_anagram.WordList.append(original_word) new_anagram.WordCount = 1 new_anagram.LetterCount = len(original_word) new_anagram.put() user_info.myAnagram = user_info.myAnagram + 1 user_info.myWordCount = user_info.myWordCount + 1 user_info.put() message = "Word added" else: flag = False for word in anagram.WordList: if word == original_word: flag = True break else: flag = False if flag: message = "Word already exists" else: # anagram.User=user.email() anagram.WordList.append(original_word) anagram.WordCount = anagram.WordCount + 1 anagram.put() message = "Word added" user_info.myWordCount = user_info.myWordCount + 1 user_info.put() template_values = {'message': message} template = JINJA_ENVIRONMENT.get_template('add.html') self.response.write(template.render(template_values))
def get(self): self.response.headers["Content-Type"] = "text/html" user = Services().get_current_user() word_model = Anagram.query() anagram_model = None if user: url = users.create_logout_url(self.request.uri) myuser_key = ndb.Key("MyUser", Services().get_current_user_id()) myuser = myuser_key.get() if myuser == None: myuser = MyUser(id=user.user_id()) myuser.put() anagram_query = Anagram.query(Anagram.user_id == Services().get_current_user_id()) inputed_word = self.request.get("input_word").lower() if len(inputed_word) > 0: if self.request.GET.get("search") == "Search Word": anagram_query = anagram_query.filter(Anagram.inputed_words.IN([inputed_word])) anagram_model = anagram_query.fetch() else: url = users.create_login_url(self.request.uri) template_values = { "url": url, "user": user, "anagram_model":anagram_model } template = JINJA_ENVIRONMENT.get_template("main.html") self.response.write(template.render(template_values))
def print_all_anagrams_in_word_list(phrase_str, word_list): """Prints all anagrams of a phrase that its words found in word list. Args: phrase_str (str) - phrase word_list (WordList) - WordList object already created Returns: none """ phrase = Anagram(phrase_str) next_anagram = list() print("anagrams found:") while phrase.find_next_anagram() == NextAnagram.NEXT_FOUND: next_anagram = phrase.get_next_anagram() all_anagram_words_found = True for word in next_anagram: if word_list.find_word(word) != FindWord.WORD_FOUND: all_anagram_words_found = False break if all_anagram_words_found: print(" ".join(next_anagram))
def add_new_anagram(my_user, text, anagram_id, anagram_key): if is_english_word(text): anagram = Anagram(id=anagram_id) anagram.words.append(text) anagram.sorted_word = generate_id(text) anagram.length = len(text) anagram.user_id = my_user.key.id() anagram.put() # add key of the new anagram to the users KeyProperty my_user.anagrams.append(anagram_key) my_user.put()
def check_anagram(): firstWord = request.args.get("firstWord", "") secondWord = request.args.get("secondWord", "") if Anagram.checkIfAnagram(firstWord, secondWord): return json.dumps({ "result": True, "message": "Yes! They are anagrams" }) else: return json.dumps({ "result": False, "message": "No! They are NOT anagrams" })
def get(self): self.response.headers["Content-Type"] = "text/html" user = Services().get_current_user() total_count = 0 unique_anagrams = 0 # word_model = Anagram.query() anagram_model = None # checking the user if user: url = users.create_logout_url(self.request.uri) user_key = ndb.Key("MyUser", Services().get_current_user_id()) user = user_key.get() # creating user and storing in data base if user == None: user = MyUser(id=Services().get_current_user_id()) user.put() # storing th word in the anagram query and converting them to lower case letters anagram_query = Anagram.query( Anagram.user_id == Services().get_current_user_id()) anagram_count_query = anagram_query.fetch() unique_anagrams = len(anagram_count_query) for inputed_word in anagram_count_query: for word in inputed_word.input_words: total_count += 1 input_word = self.request.get("input_word").lower() # checking lenth of the word if len(input_word) > 0: # checking whether the button is search or not if self.request.GET.get("search_button") == "Search Word": # filtering the anagram query anagram_query = anagram_query.filter( Anagram.input_words.IN([input_word])) # fetching the filterd anagram query anagram_model = anagram_query.fetch() # creating login for user else: url = users.create_login_url(self.request.uri) # tempate values storing all the required values template_values = { "url": url, "user": user, "anagram_model": anagram_model, "unique_anagrams": unique_anagrams, "total_count": total_count, } # environment for the templatr values template = JINJA_ENVIRONMENT.get_template("main.html") self.response.write(template.render(template_values))
def generate(self, input_text, my_user): permutations = utilities.a_permutations(input_text) anagrams = Anagram.query().fetch() sorted_list = [] result = [] for i in range(len(anagrams)): sorted_list.append(anagrams[i].sorted_word) for i in permutations: for j in sorted_list: if i == j: anagram_id = my_user.key.id() + '/' + j anagram = ndb.Key(Anagram, anagram_id).get() for x in anagram.words: result.append(str(x)) if input_text in result: result.remove(input_text) return result
def add_new_anagram(my_user, text, anagram_id, anagram_key): if text: anagram = Anagram(id=anagram_id) anagram.words.append(text) anagram.sorted_word = generate_id(text) anagramList = list(text) anagram.sub_anagram = generateSubAnagrams(anagramList) anagram.length = len(text) anagram.user_id = my_user.key.id() anagram.put() my_user.anagrams.append(anagram_key) if anagram.sorted_word is not None: my_user.anagram_count += 1 my_user.anagram_count if anagram.words is not None: my_user.words_count += 1 my_user.words_count my_user.put()
def test_check_if_anagram(self): # check the words that are anagrams self.assertTrue(Anagram.checkIfAnagram("", ""), "Both empty string, they are anagrams") self.assertTrue(Anagram.checkIfAnagram("wolf", "flow"), "They are anagrams") self.assertTrue(Anagram.checkIfAnagram("WoLf", "FlOw"), "Case insensitive test, they are anagrams") self.assertTrue(Anagram.checkIfAnagram("restful", "fluster"), "They are anagrams") self.assertTrue( Anagram.checkIfAnagram("loooooooooooooooogword", "wordloooooooooooooooog"), "They are anagrams") self.assertTrue( Anagram.checkIfAnagram("TomMarvoloRiddle", "IamLordVoldemort"), "They are anagrams") # check the words that are not anagrams self.assertFalse(Anagram.checkIfAnagram(None, None), "None should just return false") self.assertFalse(Anagram.checkIfAnagram("abc", "test"), "They are NOT anagrams") self.assertFalse(Anagram.checkIfAnagram("grove", "groove"), "They are NOT anagrams")
def get(self): self.response.headers["Content-Type"] = "text/html" inputed_word = self.request.GET.get("input_word") sorted_key = Services().sorted_key(word=inputed_word) list_sorted_keys = [] anagram_query = Anagram.query( Anagram.user_id == Services().get_current_user_id()) possible_combination_key = Services().possibleAllCountPermutations( text=sorted_key) anagram_models = [] for word in possible_combination_key: query = anagram_query.filter(Anagram.anagram_key == word).fetch( projection=[Anagram.inputed_words]) if len(query) > 0: for anagram in query: anagram_models.extend(anagram.inputed_words) dictionary_words = {} for word in anagram_models: if len(word) in dictionary_words: dict_words = dictionary_words[len(word)] dict_words.append(word) dictionary_words[len(word)] = dict_words else: dictionary_words[len(word)] = [word] template_values = { "inputword": inputed_word, "dictionary_words": dictionary_words } template = JINJA_ENVIRONMENT.get_template("subanagram.html") self.response.write(template.render(template_values))
def get(self): # getting the data from the inputed word and storing in the anagram model which has all the words self.response.headers["Content-Type"] = "text/html" input_word = self.request.GET.get("input_word") sorted_key = Services().sorted_key(word=input_word) anagram_query = Anagram.query( Anagram.user_id == Services().get_current_user_id()) possible_combination_key = Services().AllPermutations(text=sorted_key) anagram_models = [] # filtering the words which has same key values for word in possible_combination_key: query = anagram_query.filter(Anagram.anagram_key == word).fetch( projection=[Anagram.input_words]) # checking whether the query has more than one word if len(query) > 0: for anagram in query: anagram_models.extend(anagram.input_words) dictionary_words = {} # doing appending operation adn storing in dict word for word in anagram_models: if len(word) in dictionary_words: dict_words = dictionary_words[len(word)] dict_words.append(word) dictionary_words[len(word)] = dict_words else: dictionary_words[len(word)] = [word] # template value store the required feilds of data to access template_values = { "inputword": input_word, "dictionary_words": dictionary_words } # environment for connect to the info.html page template = JINJA_ENVIRONMENT.get_template("info.html") self.response.write(template.render(template_values))
class Test_Anagram(unittest.TestCase): def setUp(self): self.anagram=Anagram() def tearDown(self): self.anagram=None def test_not_receive_string(self): self.assertRaises(AttributeError,self.anagram.recv,123) def test_ltrim(self): self.assertEqual('abc',self.anagram.recv(' abc').str) def test_rtrim(self): self.assertEqual('abc',self.anagram.recv('abc ').str) def test_trimspace(self): self.assertEqual('abc',self.anagram.recv(' a bc ').str) def test_output_list(self): self.assertEqual(['ab','ba'],self.anagram.recv(' a b ').ana()) def test_output_list_3_chars(self): self.assertEqual(6,len(self.anagram.recv(' a bc ').ana())) def test_output_list_4_chars(self): self.assertEqual(24,len(self.anagram.recv('abcd').ana()))
def test_is_anagram_with_oneparameter(self): anagram = Anagram() self.assertFalse(anagram.is_Anagram("", "bcca"))
def test__ana_7(self): self.assertTrue(Anagram.is_anagram('aa bb', 'bb aa'))
def test_ana_6(self): self.assertTrue(Anagram.is_anagram('aabb', 'ba ba'))
def test_ana_5(self): self.assertTrue(Anagram.is_anagram('aa bb', 'aabb'))
def main(): test = TestAnagram() anagram = Anagram() test.test_anagram(anagram.anagram) test.test_anagram(anagram.anagram2)
def setUp(self): self.anagram = Anagram()
def test_is_anagram(self): anagram = Anagram() self.assertTrue(anagram.is_Anagram("abc", "bca"))
def post(self): self.response.headers['Content-Type'] = 'text/html' action = self.request.get('button') user = users.get_current_user() myuser_key = ndb.Key('MyUser', user.user_id()) myuser = myuser_key.get() file = self.request.get('myFile') if action == 'UPLOAD': f = open(file) line = f.readline() # count = 1 while line: original_word = (line.strip('\n\r')).lower() lexi_word = lexi(original_word) anagram_key = ndb.Key('Anagram',user.email()+lexi_word) anagram = anagram_key.get() if anagram == None: new_anagram = Anagram(id=user.email()+lexi_word,AnagramKey=lexi_word) new_anagram.User=user.email() new_anagram.WordList.append(original_word) new_anagram.WordCount = 1 new_anagram.LetterCount = len(original_word) new_anagram.put() myuser.myAnagram = myuser.myAnagram + 1 myuser.myWordCount = myuser.myWordCount + 1 myuser.put() else: flag = False for word in anagram.WordList: if word == original_word: flag = True break else: flag = False if flag: message = "Word already exists" else: # anagram.User=user.email() anagram.WordList.append(original_word) anagram.WordCount = anagram.WordCount + 1 anagram.put() # message = "Word added" myuser.myWordCount = myuser.myWordCount + 1 myuser.put() # count = count + 1 line = f.readline() f.close() template_values={ 'message':"File Uploaded" } template = JINJA_ENVIRONMENT.get_template('upload.html') self.response.write(template.render(template_values))
def test_isnot_anagram01(self): anagram = Anagram() self.assertFalse(anagram.is_Anagram("abc", "bcca"))
def test_ana_1(self): self.assertFalse(Anagram.is_anagram('aabb', 'aabb'))
class TestAnagram(unittest.TestCase): def setUp(self): self.anagram = Anagram() def test_one_letter(self): self.anagram.set_input("a") output = self.anagram.get_output() self.assertEquals(set([]), output) def test_two_letters(self): self.anagram.set_input("ac") output = self.anagram.get_output() self.assertEquals(set([]), output) def test_two_equal_letters(self): self.anagram.set_input("aa") output = self.anagram.get_output() self.assertEquals(set(["aa"]), output) def test_three_equal_and_four_letters(self): self.anagram.set_input("sses") output = self.anagram.get_output() self.assertEquals(set(["sess"]), output) def test_three_different_letters(self): self.anagram.set_input("abc") output = self.anagram.get_output() self.assertEquals(set(["bac"]), output)
def test__ana_3(self): self.assertFalse(Anagram.is_anagram('aabb', 'bbba'))
def test_ana_4(self): self.assertFalse(Anagram.is_anagram('abcd', 'edca'))
from flask import Flask, render_template, json, request from anagram import Anagram app = Flask(__name__) anagram = Anagram() @app.route('/') def index(): return render_template('index.html') @app.route('/check_anagram') def check_anagram(): firstWord = request.args.get("firstWord", "") secondWord = request.args.get("secondWord", "") if Anagram.checkIfAnagram(firstWord, secondWord): return json.dumps({ "result": True, "message": "Yes! They are anagrams" }) else: return json.dumps({ "result": False, "message": "No! They are NOT anagrams" }) @app.route('/refresh_topten')
def question1(s, t): anagram_obj = Anagram(s,t) return anagram_obj.findAnagram()
def test_anagram(self): anagram = Anagram() self.assertTrue(anagram.is_anagram("god", "dog"))