icsemantic.catalog.indexes.synonym

Package platecom :: Package ontocatalog :: Package indexes :: Module synonym |
[hide private]
[frames] | no frames]

# Source Code for Module icsemantic.catalog.indexes.synonym

      1  """SynonymIndex is an index for the catalog that does not index
      2  anything.
      3
      4  Instead, the _apply_index performs a query to a thesaurus object for
      5  synonymous of the keyword parameters and then queries the site catalog
      6  for the result of the thesaurus query.
      7  """
      8
      9  from logging import getLogger
     10
     11  from zope.interface import implements
     12  from zope.component import queryUtility
     13  from zope.i18n.interfaces import IUserPreferredLanguages
     14
     15  from Globals import Persistent, DTMLFile
     16  from OFS.SimpleItem import SimpleItem
     17  from BTrees.IIBTree import IITreeSet, IISet, intersection, union
     18
     19  from Products.PluginIndexes import PluggableIndex
     20  from Products.PluginIndexes.common.util import parseIndexRequest
     21  from Products.PluginIndexes.interfaces import IPluggableIndex
     22
     23  from Products.CMFCore.utils import getToolByName
     24
     25  from platecom.ontocatalog.indexes import utils
     26
     27
     28 -class SynonymIndex(Persistent, SimpleItem):
     29      """An index for synonymous that does not index anything"""
     30
     31      __implements__ = (PluggableIndex.PluggableIndexInterface,)
     32      implements(IPluggableIndex)
     33
     34      meta_type = "SynonymIndex"
     35      manage_workspace = DTMLFile('dtml/manageFakeIndex', globals())
     36
     37 -    def __init__(self, id, extra=None, caller=None):
     38          """Creates a new index"""
     39          self.id = id
     40          self.catalog = caller
     41
     42 -    def index_object(self, docid, obj ,threshold=100):
     43          """Hook for (Z)Catalog. Since this is a fake index, nothing
     44          is done here.
     45          """
     46          return 1
     47
     48 -    def unindex_object(self, docid):
     49          """Hook for (Z)Catalog. Since this is a fake index, nothing
     50          is done here.
     51          """
     52          return
     53
     54 -    def _apply_index(self, request, cid=''):
     55          """Apply the index to query parameters given in the argument,
     56          request.
     57
     58          The argument should be a mapping object.
     59
     60          If the request does not contain the needed parameters, then
     61          None is returned.
     62
     63          Otherwise two objects are returned.  The first object is a
     64          ResultSet containing the record numbers of the matching
     65          records.  The second object is a tuple containing the names of
     66          all data fields used.
     67          """
     68          portal = getToolByName(self, 'portal_url').getPortalObject()
     69          query_options = ('query')
     70          record = parseIndexRequest(request, self.id, query_options)
     71          if record.keys is None:
     72              return None
     73
     74          #Languages dance
     75          langutil = queryUtility(IUserPreferredLanguages,
     76                                  name='platecom_preferred_languages')
     77          user_languages = tuple(langutil.getPreferredLanguages(request=self.REQUEST))
     78
     79          thesaurus_results = []
     80          catalog_results = ()
     81          for k in record.keys:
     82              for lang in  user_languages:
     83                  key = '%s@%s' % (k, lang)
     84                  thesaurus_results += utils.get_equivalent(portal, key,
     85                                                            lang=[lang])
     86
     87                  if thesaurus_results != []:
     88                      tuples = [tl.split('@') for tl in thesaurus_results]
     89                      synonyms = ['\"%s\"' % (t,) for (t,l) in tuples]
     90                      search_text = ' OR '.join(synonyms)
     91                      query = {'SearchableText': search_text,
     92                               'Language': user_languages}
     93                      catalog_results += tuple(self.catalog(query))
     94
     95          result = utils.build_catalog_results(self.id, self.catalog._catalog,
     96                                               catalog_results)
     97          return result
     98
     99
    100  manage_addSynonymIndexForm = DTMLFile('dtml/addFakeIndex', globals())
    101
    102 -def manage_addSynonymIndex(self, id, extra=None, REQUEST=None, RESPONSE=None,
    103                             URL3=None):
    104      """Add a fake index"""
    105      return self.manage_addIndex(id, 'SynonymIndex', extra=extra,
    106                  REQUEST=REQUEST, RESPONSE=RESPONSE, URL1=URL3)
    107