Exemple #1
0
def search(sentence):
    result = {}
    words = Preprocessing.Clean(sentence)

    for word in words:
        q = db.search(qr.Word == word)
        rating = []

        try:
            for i in range(len(q[0]['Count'])):
                rating.append(q[0]['Place'][i] - q[0]['Count'][i])
            documents = q[0]['Documents']
            rating, documents = (list(t)
                                 for t in zip(*sorted(zip(rating, documents))))
            result[word] = documents
        except:
            pass
    if sentence.startswith('"') and sentence.endswith('"'):
        res = ()
        if len(words) > 1 and result:
            for r in result:
                if res:
                    res = res.intersection(result[r])
                else:
                    res = set(result[r])
            return {'result': list(res)}
        else:
            return result
    else:
        return result
Exemple #2
0
    def do_GET(self):
        if self.path == '/':
            self.success()
            with open('base.html') as fp:
                page = fp.read()
                self.wfile.write(page.encode())

        elif self.path.startswith('/?search='):
            self.success()
            query = unquote_plus(self.path[9:])
            with open('base.html') as fp:
                page = fp.read().split('<!--##PEWPEW##-->')
                self.wfile.write(page[0].encode())
                self.wfile.write(b'<ul>\n\n')

                # Processing query
                results = Search.search(query)
                if not results:
                    self.wfile.write(b'<p>Did you mean ?</p>')

                    sw = Spell.SearchWord()
                    words = Preprocessing.Clean(query)
                    spelled = []
                    for word in words:
                        spelled.append(sw.spell(word))
                    for list in spelled:
                        for document in list:
                            self.wfile.write(
                                make_list(make_href(document)).encode())
                else:
                    for r in results:
                        list = results[r]
                    for document in list:
                        self.wfile.write(make_list(document).encode())

                # End processing
                self.wfile.write(b'\n</ul>\n')
                self.wfile.write(page[1].encode())