Example #1
0
def get_completions(args):
    tries = corpusTries[args["corpus"]]
    mode = args["trie_mode"]
    if mode in ("words", "pattern"):
        trie = tries[0]
        prefix = args["prefix"]
    else:
        trie = tries[1]
        prefix = tuple(args["prefix"].split())
    max_results = args["max_results"]
    if max_results == 0:
        max_results = None
    if mode == "pattern":
        r = [
            word for word, freq in lab.word_filter(trie, prefix)[:max_results]
        ]
    elif args["autocorrect"] and mode == "words":
        r = lab.autocorrect(trie, prefix, max_results)
    else:
        if mode == "sentences":
            r = [
                ' '.join(result)
                for result in lab.autocomplete(trie, prefix, max_results)
            ]
        else:
            r = lab.autocomplete(trie, prefix, max_results)
    return r
Example #2
0
def run_test(input_data):
    trie = lab.generate_trie(input_data["words"])
    if input_data["f"] == "autocorrect":  # Tests for correct autocorrect
        return lab.autocorrect(trie, input_data["prefix"], input_data["N"])
    if input_data["f"] == "autocomplete":  # Tests for correct autocomplete
        return lab.autocomplete(trie, input_data["prefix"], input_data["N"])
    else:  # Tests just for producing the trie
        return trie
Example #3
0
def run_test( input_data ):
  trie = lab.generate_trie(input_data["words"])
  if input_data["f"] == "autocorrect": # Tests for correct autocorrect
      return lab.autocorrect(trie, input_data["prefix"], input_data["N"])
  if input_data["f"] == "autocomplete": # Tests for correct autocomplete
      return lab.autocomplete(trie, input_data["prefix"], input_data["N"])
  else: # Tests just for producing the trie
      return trie
Example #4
0
def autocorrect(input_data):
    global trie
    if trie is None:
        words = []
        print "LOADING CORPUS"
        with open("resources/words.json", "r") as f:
            words = json.load(f)
            trie = lab.generate_trie(words)
    return lab.autocorrect(trie, input_data["prefix"], input_data["N"])
Example #5
0
def autocorrect( input_data ):
   global trie
   if trie is None:
       words = []
       print "LOADING CORPUS"
       with open("resources/words.json", "r") as f:
           words = json.load(f)
           trie = lab.generate_trie(words)
   return lab.autocorrect(trie, input_data["prefix"], input_data["N"])
Example #6
0
 def test_01_autocorrect(self):
     # Autocorrect on cat in small corpus
     trie = lab.make_word_trie("cats cattle hat car act at chat crate act car act")
     result = lab.autocorrect(trie, 'cat',4)
     self.assertIsInstance(result,list,"result not a list.")
     for w in result:
         self.assertIsInstance(w,str,"expecting list of strings.")
     result.sort()
     expect = ["act", "car", "cats", "cattle"]
     self.assertEqual(expect,result,msg="incorrect result from autocorrect.")
Example #7
0
 def test_02_big_autocorrect(self):
     nums = {'thin': [0, 8, 10, None],
             'tom': [0, 2, 4, None],
             'mon': [0, 2, 15, 17, 20, None]}
     with open(os.path.join(TEST_DIRECTORY, 'testing_data', 'frankenstein.txt'), encoding='utf-8') as f:
         text = f.read()
     w = lab.make_word_trie(text)
     for i in sorted(nums):
         for n in nums[i]:
             result = lab.autocorrect(w, i, n)
             expected = read_expected('frank_autocorrect_%s_%s.pickle' % (i, n))
             self.assertEqual(len(result), len(expected), msg='wrong autocorrect of '+repr(i)+' with maxcount = '+str(n))
             self.assertEqual(set(result), set(expected), msg='wrong autocorrect of '+repr(i)+' with maxcount = '+str(n))
Example #8
0
 def test_02_big_autocorrect(self):
     nums = {
         'thin': [0, 8, 10, None],
         'tom': [0, 2, 4, None],
         'mon': [0, 2, 15, 17, 20, None]
     }
     with open(os.path.join(TEST_DIRECTORY, 'resources', 'testing_data',
                            'frankenstein.txt'),
               encoding='utf-8') as f:
         text = f.read()
     w = lab.make_word_trie(text)
     for i in sorted(nums):
         for n in nums[i]:
             result = lab.autocorrect(w, i, n)
             expected = read_expected('frank_autocorrect_%s_%s.pickle' %
                                      (i, n))
             self.assertEqual(len(result), len(expected), msg=('missing' if len(result) < len(expected)\
                 else 'too many') + ' autocorrect results for ' + repr(i) + ' with macount = ' + str(n))
             self.assertEqual(set(result), set(expected), msg='autocorrect included ' + repr(set(result) - set(expected))\
                 + ' instead of ' + repr(set(expected) - set(result)) + ' for ' + repr(i) + ' with maxcount = '+str(n))
Example #9
0
def test_autocorrect_small():
    # Autocorrect on cat in small corpus
    trie = lab.make_word_trie(
        "cats cattle hat car act at chat crate act car act")
    result = lab.autocorrect(trie, 'cat', 4)
    assert set(result) == {"act", "car", "cats", "cattle"}