<s> from whoosh . analysis import NgramAnalyzer <EOL> from whoosh . query import * <EOL> class SimpleNgramParser ( object ) : <EOL> """<STR_LIT>""" <EOL> __inittypes__ = dict ( fieldname = str , minchars = int , maxchars = int , <EOL> discardspaces = bool , analyzerclass = type ) <EOL> def __init__ ( self , fieldname , minchars , maxchars , discardspaces = False , <EOL> analyzerclass = NgramAnalyzer ) : <EOL> """<STR_LIT>""" <EOL> self . fieldname = fieldname <EOL> self . minchars = minchars <EOL> self . maxchars = maxchars <EOL> self . discardspaces = discardspaces <EOL> self . analyzerclass = analyzerclass <EOL> def parse ( self , input ) : <EOL> required = [ ] <EOL> optional = [ ] <EOL> gramsize = max ( self . minchars , min ( self . maxchars , len ( input ) ) ) <EOL> if gramsize > len ( input ) : <EOL> return NullQuery ( input ) <EOL> discardspaces = self . discardspaces <EOL> for t in self . analyzerclass ( gramsize ) ( input ) : <EOL> gram = t . text <EOL> if "<STR_LIT:U+0020>" in gram : <EOL> if not discardspaces : <EOL> optional . append ( gram ) <EOL> else : <EOL> required . append ( gram ) <EOL> if required : <EOL> fieldname = self . fieldname <EOL> andquery = And ( [ Term ( fieldname , g ) for g in required ] ) <EOL> if optional : <EOL> orquery = Or ( [ Term ( fieldname , g ) for g in optional ] ) <EOL> return AndMaybe ( [ andquery , orquery ] ) <EOL> else : <EOL> return andquery <EOL> else : <EOL> return NullQuery </s>
