Commits

Matt Chaput  committed 6238d14

Minor work on spelling interface.

  • Participants
  • Parent commits debdee2
  • Branches dawg

Comments (0)

Files changed (3)

File src/whoosh/filedb/filereading.py

                                       LengthReader, TermVectorReader)
 from whoosh.matching import FilterMatcher, ListMatcher
 from whoosh.reading import IndexReader, TermNotFound
-from whoosh.support.dawg import DawgReader, within
+from whoosh.support.dawg import DawgReader
 from whoosh.util import protected
 
 SAVE_BY_DEFAULT = True
             raise Exception("No word graph for field %r" % fieldname)
         return self.dawg.field_root(fieldname)
 
-    def terms_within(self, fieldname, word, maxdist, prefix=0, seen=None):
-        if not self.has_word_graph(fieldname):
-            sup = super(SegmentReader, self)
-            return sup.terms_within(fieldname, word, maxdist, prefix=prefix)
-        
-        node = self.word_graph(fieldname)
-        return within(node, word, maxdist, prefix=prefix, seen=seen)
-    
     def _field_root(self, fieldname):
         if not self.has_word_graph(fieldname):
             raise Exception("No word graph for field %r" % fieldname)

File src/whoosh/reading.py

 from bisect import bisect_right
 from heapq import heapify, heapreplace, heappop, nlargest
 
+from whoosh.support.dawg import within
 from whoosh.support.levenshtein import distance
 from whoosh.util import ClosableMixin
 from whoosh.matching import MultiMatcher
             the list of words.
         """
         
-        # The default implementation uses brute force to scan the entire word
-        # list and calculate the edit distance for each word. Backends that
-        # store a word graph can override this with something more elegant.
-        
-        for word in self.expand_prefix(fieldname, text[:prefix]):
-            if word == text:
-                yield text
-            elif distance(word, text, limit=maxdist) <= maxdist:
+        if self.has_word_graph(fieldname):
+            node = self.word_graph(fieldname)
+            for word in within(node, word, maxdist, prefix=prefix, seen=seen):
                 yield word
+        else:
+            for word in self.expand_prefix(fieldname, text[:prefix]):
+                if word == text:
+                    yield text
+                elif distance(word, text, limit=maxdist) <= maxdist:
+                    yield word
     
     def most_frequent_terms(self, fieldname, number=5, prefix=''):
         """Returns the top 'number' most frequent terms in the given field as a
         is atomic.
         """
         
-        return False
+        return None
     
     #
     

File src/whoosh/spelling.py

 from heapq import heappush, heapreplace
 
 from whoosh import analysis, fields, query, scoring
+from whoosh.support.dawg import within
 from whoosh.support.levenshtein import distance
 
 
     
     heap = []
     seen = set()
+    root = reader.word_graph(fieldname)
     for k in xrange(1, maxdist+1):
-        for sug in reader.terms_within(fieldname, text, k, prefix=prefix):
-            if sug in seen:
-                continue
-            seen.add(sug)
-            
+        for sug in within(root, text, k, prefix=prefix, seen=seen):
             item = (ranking(reader, fieldname, sug, k), sug)
             if len(heap) < limit:
                 heappush(heap, item)
     """This class allows you to generate suggested corrections for mis-typed
     words based on a word list. Note that if you want to generate suggestions
     based on the content of a field in an index, you should turn spelling on
-    for the field and use :meth:`whoosh.searching.Searcher.suggest` instead of
-    this object.
+    for the field and use :func:`suggest` instead of this object.
     
     """