def test005(self): from Products.ZCTextIndex.ParseTree import AndNode from Products.ZCTextIndex.ParseTree import AtomNode from Products.ZCTextIndex.ParseTree import OrNode self.expect("aa AND bb OR cc AnD dd", OrNode([AndNode([AtomNode("aa"), AtomNode("bb")]), AndNode([AtomNode("cc"), AtomNode("dd")])]))
def test005(self): self.expect( "aa AND bb OR cc AnD dd", OrNode([ AndNode([AtomNode("aa"), AtomNode("bb")]), AndNode([AtomNode("cc"), AtomNode("dd")]) ]))
def test003(self): from Products.ZCTextIndex.ParseTree import AndNode from Products.ZCTextIndex.ParseTree import AtomNode self.expect("aa and bb AND cc", AndNode([AtomNode("aa"), AtomNode("bb"), AtomNode("cc")]))
def test025(self): # Split by Unicode fullwidth space from Products.ZCTextIndex.ParseTree import AndNode from Products.ZCTextIndex.ParseTree import AtomNode self.expect(u"foo\u3000bar", AndNode([AtomNode(u"foo"), AtomNode(u"bar")]))
def test006(self): from Products.ZCTextIndex.ParseTree import AndNode from Products.ZCTextIndex.ParseTree import AtomNode from Products.ZCTextIndex.ParseTree import OrNode self.expect("(aa OR bb) AND (cc OR dd)", AndNode([OrNode([AtomNode("aa"), AtomNode("bb")]), OrNode([AtomNode("cc"), AtomNode("dd")])]))
def test006(self): self.expect( "(aa OR bb) AND (cc OR dd)", AndNode([ OrNode([AtomNode("aa"), AtomNode("bb")]), OrNode([AtomNode("cc"), AtomNode("dd")]) ]))
def test007(self): from Products.ZCTextIndex.ParseTree import AndNode from Products.ZCTextIndex.ParseTree import AtomNode from Products.ZCTextIndex.ParseTree import NotNode self.expect("aa AND NOT bb", AndNode([AtomNode("aa"), NotNode(AtomNode("bb"))]))
def test016(self): from Products.ZCTextIndex.ParseTree import AndNode from Products.ZCTextIndex.ParseTree import AtomNode from Products.ZCTextIndex.ParseTree import NotNode self.expect("-foo bar", AndNode([AtomNode("bar"), NotNode(AtomNode("foo"))]))
def test018(self): from Products.ZCTextIndex.ParseTree import AndNode from Products.ZCTextIndex.ParseTree import AtomNode from Products.ZCTextIndex.ParseTree import NotNode from Products.ZCTextIndex.ParseTree import PhraseNode self.expect('booh -"foo bar"', AndNode([AtomNode("booh"), NotNode(PhraseNode(["foo", "bar"]))]))
def test021(self): from Products.ZCTextIndex.ParseTree import AndNode from Products.ZCTextIndex.ParseTree import AtomNode self.expect( 'foo"bar"blech', AndNode([AtomNode("foo"), AtomNode("bar"), AtomNode("blech")]))
def test023(self): self.expect("foo* bar", AndNode([GlobNode("foo*"), AtomNode("bar")]))
def test021(self): self.expect( 'foo"bar"blech', AndNode([AtomNode("foo"), AtomNode("bar"), AtomNode("blech")]))
def test020(self): self.expect('"foo"bar', AndNode([AtomNode("foo"), AtomNode("bar")]))
def test019(self): self.expect('foo"bar"', AndNode([AtomNode("foo"), AtomNode("bar")]))
def test018(self): self.expect( 'booh -"foo bar"', AndNode([AtomNode("booh"), NotNode(PhraseNode(["foo", "bar"]))]))
def test017(self): self.expect( "booh -foo-bar", AndNode([AtomNode("booh"), NotNode(PhraseNode(["foo", "bar"]))]))
def test013(self): self.expect("((foo bar))", AndNode([AtomNode("foo"), AtomNode("bar")]))
def test011(self): self.expect("foo bar", AndNode([AtomNode("foo"), AtomNode("bar")]))
def test007(self): self.expect("aa AND NOT bb", AndNode([AtomNode("aa"), NotNode(AtomNode("bb"))]))
def test024(self): # Split by UTF-8 fullwidth space from Products.ZCTextIndex.ParseTree import AndNode from Products.ZCTextIndex.ParseTree import AtomNode self.expect("foo\xe3\x80\x80bar", AndNode([AtomNode("foo"), AtomNode("bar")]))
def test023(self): from Products.ZCTextIndex.ParseTree import AndNode from Products.ZCTextIndex.ParseTree import AtomNode from Products.ZCTextIndex.ParseTree import GlobNode self.expect("foo* bar", AndNode([GlobNode("foo*"), AtomNode("bar")]))
def test003(self): self.expect("aa and bb AND cc", AndNode([AtomNode("aa"), AtomNode("bb"), AtomNode("cc")]))
def test016(self): self.expect("-foo bar", AndNode([AtomNode("bar"), NotNode(AtomNode("foo"))]))
def replaceWordsQuery(tree, parseQuery, gtool, gloss_items, excluded): """ Change the tree query: all Atom or Phrase found in the glossary (term or variant) are replaced by an"OR" query between all this terms, i.e if glossary contains the term 'lorem' with variant 'ipsum', then replaceWordsQuery(AtomNode('lorem')) returns OrNode([AtomNode('lorem'), AtomNode('ipsum')]) @param tree: the current node to process @param gtool: a PloneGlossaryTool instance @param gloss_items: the list of glossary item to search within query @param excluded: dict of words (as keys) to skip """ if isinstance(tree, AtomNode): nodes = [tree] else: nodes = tree.getValue() for node_idx in range(len(nodes)): subnode = nodes[node_idx] nodetype = subnode.nodeType() if nodetype in ('OR', 'AND'): nodes[node_idx] = replaceWordsQuery(subnode, parseQuery, gtool, gloss_items, excluded) continue elif nodetype == 'NOT': continue elif nodetype == 'GLOB': continue # flatten is needed because PhraseNode.terms => [['term1', 'term2']] text = ' '.join(flatten(subnode.terms())) terms = gtool._getTextRelatedTermItems(text, gloss_items) final_terms = [] for t in terms: t_list = (t['title'],) + t['variants'] exclude_term = False for item in t_list: exclude_term |= excluded.has_key(item) if exclude_term: continue # parseQuery will choose for us AtomNode or PhraseNode final_terms.append([parseQuery('"%s"' % i) for i in t_list]) final_gloss_query = [(len(i) > 1 and OrNode(i)) or i[0] for i in final_terms if len(i)] term_count = len(final_gloss_query) if term_count == 0: final_gloss_query = None elif term_count > 1: final_gloss_query = AndNode(final_gloss_query) else: final_gloss_query = final_gloss_query[0] if final_gloss_query is not None: nodes[node_idx] = final_gloss_query if len(nodes) == 1: return nodes[0] elif len(nodes) > 1: if isinstance(tree, AtomNode): return AndNode(nodes) else: tree._value = nodes return tree
def test013(self): from Products.ZCTextIndex.ParseTree import AndNode from Products.ZCTextIndex.ParseTree import AtomNode self.expect("((foo bar))", AndNode([AtomNode("foo"), AtomNode("bar")]))