def process(request): """ Main computation of words entered by user """ if request.method != "POST": return redirect("/") text = request.POST.get('text', "") three_letters = request.POST.get('threeLetters') == 'true' only_base = request.POST.get('onlyBase') == 'true' if request.user.is_authenticated(): known_words = UserDictionary.objects.values_list( 'word', flat=True).filter(user=request.user) else: known_words = [] words_list = text_to_words(text) _, cnt = words_analysis(words_list, three_letters, only_base, known_words) known_in_text, _ = words_analysis(words_list, three_letters, only_base, known_words) return JsonResponse({'success': True, 'analysis': render_to_string( 'analysis.html', { "words": sorted(cnt.items()), "known_words": sorted(known_words), "known_in_text": sorted(known_in_text) })})
def test_words_analysis3(self): """ Tests if function correctly returns list of known words and Counter dictionary with words and their frequency in list of words; Words less than 3 letters will be ignored; '3 letters' extra feature selected; Some words in user's dictionary; :return: """ test_words = [ 'hello', 'hello', 'forever', 'i', 'young', 'young', 'i', 'am', 'i', 'going', 'spells', 'attachments', 'killed', 'diving', 'fitness', 'situated', 'starts' ] result = ['hello', 'going', 'spells'], Counter({ 'young': 2, 'forever': 1, 'attachments': 1, 'starts': 1, 'situated': 1, 'fitness': 1, 'diving': 1, 'killed': 1 }) known_words = ['hello', 'spells', 'going'] self.assertEqual( words_analysis(test_words, three_letters=True, only_base=False, known_words=known_words), result)
def test_words_analysis3(self): """ Tests if function correctly returns list of known words and Counter dictionary with words and their frequency in list of words; Words less than 3 letters will be ignored; '3 letters' extra feature selected; Some words in user's dictionary; :return: """ test_words = ['hello', 'hello', 'forever', 'i', 'young', 'young', 'i', 'am', 'i', 'going', 'spells', 'attachments', 'killed', 'diving', 'fitness', 'situated', 'starts'] result = ['hello', 'going', 'spells'], Counter( {'young': 2, 'forever': 1, 'attachments': 1, 'starts': 1, 'situated': 1, 'fitness': 1, 'diving': 1, 'killed': 1}) known_words = ['hello', 'spells', 'going'] self.assertEqual(words_analysis( test_words, three_letters=True, only_base=False, known_words=known_words), result)