Exemple #1
0
 def test006(self):
     self.expect(
         "(aa OR bb) AND (cc OR dd)",
         AndNode([
             OrNode([AtomNode("aa"), AtomNode("bb")]),
             OrNode([AtomNode("cc"), AtomNode("dd")])
         ]))
 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")])]))
Exemple #3
0
 def test005(self):
     self.expect(
         "aa AND bb OR cc AnD dd",
         OrNode([
             AndNode([AtomNode("aa"), AtomNode("bb")]),
             AndNode([AtomNode("cc"), AtomNode("dd")])
         ]))
 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 test004(self):
     from Products.ZCTextIndex.ParseTree import AtomNode
     from Products.ZCTextIndex.ParseTree import OrNode
     self.expect("aa OR bb or cc",
                 OrNode([AtomNode("aa"),
                         AtomNode("bb"),
                         AtomNode("cc")]))
Exemple #6
0
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
Exemple #7
0
 def test004(self):
     self.expect("aa OR bb or cc",
                 OrNode([AtomNode("aa"),
                         AtomNode("bb"),
                         AtomNode("cc")]))