Пример #1
0
   def search(self, word):
      """Lookup word"""

      _start = time.time()

      result = meta.SearchResult()

      word_lowered = word.lower()
      
      if self.definitions is None:
         self.definitions = self.dict.getdeflist()
         self.definitions.sort()

      words = []

      for definition in self.definitions:
         if definition.lower().startswith(word_lowered):
            words.append(definition)

      html = []

      html.append("<html><head>")
      html.append("<meta http-equiv=\"Content-Type\" " \
                  "content=\"text/html; charset=%s\">" \
                  % str(self.getEncoding()))
      html.append("<head><body>")

      (orig, translation) = self._getTranslation(word)

      if not translation:
         if len(words):
            debugLog(DEBUG, "Retrying search...")
            _word = words[0]
            orig, translation = self._getTranslation(_word)
            if not translation:
               result.setError(errortype.NOT_FOUND)
         else:
            result.setError(errortype.NOT_FOUND)
            translation = ""

      html.append("<table width=\"100%\"><tr>")
      html.append("<td bgcolor=\"%s\">" % WORD_BG)
      html.append("<b>%s</b></td></tr>" % orig)
      html.append("<tr><td>")
      html.append("<p>%s</p>" % translation)
      html.append("</td></tr></table>")
      html.append("</body></html>")

      result.setTranslation("".join(html))
      result.setWordList(words)

      debugLog(DEBUG, "DictParser: Search took % f seconds" \
            % (time.time() - _start))

      return result
Пример #2
0
   def search(self, word):
      """Lookup word"""

      result = meta.SearchResult()

      try:
         conn = dictclient.Connection(self.server, self.port)
      except:
         result.setError(errortype.CONNECTION_ERROR)
         return result

      html = []
      html.append("<html><head>" \
                  "<meta http-equiv=\"Content-Type\" " \
                  "content=\"text/html; charset=%s\">" \
                  "</head><body>" % self.getEncoding())

      found = False

      try:
         data = conn.define(self.db, word)
      except:
         data = []

      for d in data:
         found = True

         html.append("<p><table width=\"100%\"><tr>")
         html.append("<td bgcolor=\"%s\">" % DICT_BG)
         html.append("<b><i>%s</i></b></td></tr>" % d.getdb().getdescription())

         source = d.getdefstr()
         source = source.replace('<', '&lt;')
         source = source.replace('>', '&gt;')
         orig = source.split("\n", 1)[0]
         
         pron = re.findall("\[(.*?)\]", orig) # 1st comment type
         pronPatt = " [%s]"
         
         if len(pron) == 0:
            pron = re.findall("\/(.*?)\/", orig) # 2nd comment type
            pronPatt = " /%s/"
         if len(pron) == 0:
            pron = re.findall(r"\\(.*?)\\", orig) # 3rd comment type
            pronPatt = " \\%s\\"
         
         if len(pron) > 0:
            orig = "<b>%s</b> [<i>%s</i>]" % \
                   (orig.replace(pronPatt % pron[0], ""), pron[0])
         else:
            orig = "<b>%s</b>" % orig

         html.append("<tr><td bgcolor=\"%s\">" % WORD_BG)
         html.append("%s</td></tr>" % orig)

         source = source.replace('\n\n', '<br><br>')
         
         translation = ' '.join(source.split('\n')[:])
         links = re.findall("{(.*?)}", translation)
         for link in links:
            translation = translation.replace("{%s}" % link,
                              "<a href=\"%s\">%s</a>" % (link, link))
         html.append("<tr><td>%s</td></tr>" % translation)
         html.append("</table></p>")

      html.append("</body></html>")

      result.setTranslation(''.join(html))
      
      if not found:
         result.setError(errortype.NOT_FOUND)

      return result
Пример #3
0
   def search(self, word):
      """Lookup word"""

      _start = time.time()

      word_lowered = word.lower()

      encodedIndex = {}
      for literal in self.index:
         encodedIndex[literal.encode(self.getEncoding())] = \
                      self.index.get(literal)

      #
      # Seek to the beginning of the block
      #
      position = 0L
      if word_lowered[:2] in encodedIndex.keys():
         position = encodedIndex[word_lowered[:2]]

      debugLog(DEBUG, "Index: %s->%d" % (word_lowered[:2], position))
      debugLog(DEBUG, "SlowoParser: Seeking to %d" % position)
      
      self.fd.seek(position)

      html = []

      html.append("<html><head>")
      html.append("<meta http-equiv=\"Content-Type\" " \
                  "content=\"text/html; charset=%s\">" \
                  % str(self.getEncoding()))
      html.append("<head><body>")

      found = False
      words = []

      result = meta.SearchResult()

      # DEBUG
      _linesRead = 0

      for line in self.fd.xreadlines():
         _linesRead += 1
         line = line.strip()
         try:
            orig = ""
            end = ""
            try:
                orig, end = line.split('=', 1)
            except ValueError, e:
                systemLog(ERROR, '%s (line %s)' % (e, line))
            orig = orig.strip()
            chunks = end.split(';')

            translation = ["<ul>"]
            for chunk in chunks:
               comment = []
               trans = chunk.split('//')
               
               if len(trans) > 1:
                  comment = trans[1:]

               trans = trans[:1]
                  
               trans = "".join(trans).strip()
               comment = "".join(comment).strip()
               
               if len(trans) and len(comment) != 0:
                  translation.append("<li>%s (<i>%s</i>)</li>" \
                                     % (trans, comment))
               elif len(trans):
                  translation.append("<li>%s</li>" % trans)

            translation.append("</ul>")

            translation = "".join(translation)

         except:
Пример #4
0
   def search(self, word):
      """Lookup word"""

      _start = time.time()

      word_lowered = word.lower()

      encodedIndex = {}
      for literal in self.index:
         encodedIndex[literal.encode(self.getEncoding())] = \
                      self.index.get(literal)

      #
      # Seek to the beginning of the block
      #
      position = 0L
      if word_lowered[:2] in encodedIndex.keys():
         position = encodedIndex[word_lowered[:2]]

      debugLog(DEBUG, "Index: %s->%d" % (word_lowered[:2], position))
      debugLog(DEBUG, "MovaParser: Seeking to %d" % position)
      self.fd.seek(position)

      html = []

      html.append("<html><head>")
      html.append("<meta http-equiv=\"Content-Type\" " \
                  "content=\"text/html; charset=%s\">" \
                  % str(self.getEncoding()))
      html.append("<head><body>")

      found = False
      words = []

      result = meta.SearchResult()

      # DEBUG
      _linesRead = 0

      for line in self.fd.xreadlines():
         _linesRead += 1
         line = line.strip()
         try:
            orig, trans = line.split("  ", 1)
         except:
            continue

         if line.lower().startswith(word_lowered):   
            
            if not orig.lower().startswith(word_lowered):
               break
            
            if orig.lower() == word_lowered and not found:
               found = True
               self._appendTranslation(html, orig, trans)               
               
            words.append(orig)
            if len(words) == 1:
               suggestedWord = orig
               suggestedTrans = trans
         elif len(words):
            break

      debugLog(DEBUG, "%d lines scanned" % _linesRead)
      
      if not found:
         if words:
            self._appendTranslation(html, suggestedWord, suggestedTrans)
         else:
            result.setError(errortype.NOT_FOUND)

      html.append("</font></body></html>")

      try:
         translation = "".join(html)
      except:
         result.setError(errortype.INVALID_ENCOFING)
         translation = ""
      
      result.setTranslation(translation)
      result.setWordList(words)

      debugLog(DEBUG, "MovaParser: Search took %f seconds" \
               % (time.time() - _start))

      return result
Пример #5
0
    def search(self, word):
        """Lookup word"""

        _start = time.time()

        word_lowered = word.lower()

        encodedIndex = {}
        for literal in self.index:
            encodedIndex[literal.encode(self.getEncoding())] = \
                         self.index.get(literal)

        #
        # Seek to the beginning of the block
        #
        position = 0
        if word_lowered[:2] in list(encodedIndex.keys()):
            position = encodedIndex[word_lowered[:2]]

        debugLog(DEBUG, "Index: %s->%d" % (word_lowered[:2], position))
        debugLog(DEBUG, "SlowoParser: Seeking to %d" % position)

        self.fd.seek(position)

        html = []

        html.append("<html><head>")
        html.append("<meta http-equiv=\"Content-Type\" " \
                    "content=\"text/html; charset=%s\">" \
                    % str(self.getEncoding()))
        html.append("<head><body>")

        found = False
        words = []

        result = meta.SearchResult()

        # DEBUG
        _linesRead = 0

        for line in self.fd:
            _linesRead += 1
            line = line.strip()
            try:
                orig = ""
                end = ""
                try:
                    orig, end = line.split('=', 1)
                except ValueError as e:
                    systemLog(ERROR, '%s (line %s)' % (e, line))
                orig = orig.strip()
                chunks = end.split(';')

                translation = ["<ul>"]
                for chunk in chunks:
                    comment = []
                    trans = chunk.split('//')

                    if len(trans) > 1:
                        comment = trans[1:]

                    trans = trans[:1]

                    trans = "".join(trans).strip()
                    comment = "".join(comment).strip()

                    if len(trans) and len(comment) != 0:
                        translation.append("<li>%s (<i>%s</i>)</li>" \
                                           % (trans, comment))
                    elif len(trans):
                        translation.append("<li>%s</li>" % trans)

                translation.append("</ul>")

                translation = "".join(translation)

            except:
                traceback.print_exc()
                continue

            if line.lower().startswith(word_lowered):

                if not orig.lower().startswith(word_lowered):
                    break

                if orig.lower() == word_lowered and not found:
                    found = True
                    self._appendTranslation(html, orig, translation)

                words.append(orig)
                if len(words) == 1:
                    suggestedWord = orig
                    suggestedTrans = translation
            elif len(words):
                break

        debugLog(DEBUG, "%d lines scanned" % _linesRead)

        if not found:
            if words:
                self._appendTranslation(html, suggestedWord, suggestedTrans)
            else:
                result.setError(errortype.NOT_FOUND)

        html.append("</font></body></html>")

        try:
            translation = "".join(html)
        except:
            result.setError(errortype.INVALID_ENCOFING)
            translation = ""

        result.setTranslation(translation)
        result.setWordList(words)

        debugLog(DEBUG, "SlowoParser: search took %f seconds" \
              % (time.time() - _start))

        return result