summaryrefslogtreecommitdiffstats
path: root/pinolo/plugins/quotes2.py
blob: 80ba25d56f07db6d26dfd81bf7fca16c87f42e20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# -*- coding: utf-8 -*-
import os
import re
import logging
import unicodedata
from datetime import datetime
from sqlalchemy import (Column, Integer, Unicode, DateTime, func)
from sqlalchemy.orm.exc import NoResultFound
from whoosh import index
from whoosh.fields import (Schema, TEXT, KEYWORD, NUMERIC, DATETIME)
from whoosh.analysis import PyStemmerFilter, StemmingAnalyzer
from whoosh.qparser import QueryParser
from pinolo.plugins import Plugin
from pinolo.database import Base, Session


log = logging.getLogger(__name__)

# RegexTokenizer LowercaseFilter StopFilter StemmingFilter
from whoosh.analysis import (RegexTokenizer, LowercaseFilter, StopFilter, PyStemmerFilter, CharsetFilter)
from whoosh.support.charset import accent_map

stoplist = []
stem_lang = "italian"
my_analyzer = RegexTokenizer() | LowercaseFilter() | StopFilter(stoplist=stoplist) | CharsetFilter(accent_map) | PyStemmerFilter(stem_lang)


r_search_page = re.compile(r"--(?P<page>\d+)\s+")


class Quote(Base):
    __tablename__ = 'quotes'

    id = Column(Integer, primary_key=True)
    quote = Column(Unicode(10000))
    author = Column(Unicode(1000))
    creation_date = Column(DateTime)
    karma = Column(Integer)

    def __init__(self, quote, author, creation_date=None, karma=0):
        self.quote = quote
        self.author = author
        if creation_date is None:
            self.creation_date = datetime.now()
        else:
            self.creation_date = creation_date
        self.karma = karma

    def __repr__(self):
        return u"<Quote('%r', '%r', '%r', '%i')>" % (self.author, self.quote,
                                                     self.creation_date, self.karma)


class QuotesPlugin(Plugin):

    COMMAND_ALIASES = {
        'addq': 'addquote',
        'q': 'quote',
        'qq': 'search',
        's': 'search',
    }
    
    def __init__(self, bot, config, enabled=True):
        super(QuotesPlugin, self).__init__(bot, config, enabled)
        self.to_be_indexed = False
        self.init_whoosh()

    def init_whoosh(self):
        log.info("Opening Whoosh database %s" % self.config["db_path"])
        if os.path.exists(self.config["db_path"]):
            self.ix = index.open_dir(self.config["db_path"])
        else:
            schema = Schema(author=TEXT(), 
                            quote=TEXT(analyzer=my_analyzer),
                            creation_date=DATETIME(),
                            id=NUMERIC(stored=True))
            os.mkdir(self.config["db_path"])
            self.ix = index.create_in(self.config["db_path"], schema)
            self.to_be_indexed = True

    def activate(self):
        if not self.to_be_indexed:
            return

        log.info("Indexing the quotes database for the first time")
        session = Session()
        writer = self.ix.writer()
        for quote in Session.query(Quote).all():
            self.index_quote(quote, writer, commit=False)
        writer.commit()

    def deactivate(self):
        pass

    def index_quote(self, quote, writer=None, commit=True):
        if writer is None:
            writer = self.ix.writer()
        writer.add_document(id=quote.id, author=quote.author,
                            quote=quote.quote, 
                            creation_date=quote.creation_date)
        if commit:
            writer.commit()

    def on_cmd_addquote(self, event):
        if not event.text:
            event.reply(u"NO!")
            return

        quote = Quote(quote=event.text, author=event.user.nickname)
        session = Session()
        session.add(quote)
        session.commit()

        self.index_quote(quote)
        event.reply(u"Ho aggiunto la %d" % quote.id)

    def on_cmd_search(self, event):
        limit = 5
        
        if not event.text:
            event.reply(u"Eh sì, stocazzo")
            return

        match = r_search_page.match(event.text)
        if match:
            page = int(match.group(1))
        else:
            page = 1

        session = Session()
        results_id = []
        with self.ix.searcher() as searcher:
            qp = QueryParser("quote", self.ix.schema)
            query = qp.parse(event.text)
            # results = searcher.search(query, limit=limit)
            try:
                results = searcher.search_page(query, page, pagelen=limit)
            except ValueError:
                event.reply(u"La tua ricerca mi ha inibito il cervello")
                return

            found = results.scored_length()
            if not found:
                event.reply(u"Non ho trovato un cazzo!")
                return
            
            event.reply(u"Risultati pagina %d di %d, risultati %d-%d di %d" % (
                results.pagenum, results.pagecount, results.offset + 1,
                results.offset + results.pagelen + 1, len(results)))

            for result in results:
                results_id.append(result['id'])

        for quote in Session.query(Quote).filter(Quote.id.in_(results_id)).all():
            event.reply(u"(%i) %s" % (quote.id, quote.quote))
            

    def on_cmd_quote(self, event):
        if event.text:
            try:
                req = int(event.text)
                quote = self.quote_by_id(req)
            except (NoResultFound, ValueError), e:
                event.reply(u"Te stai a sbajà")
                return
        else:
            quote = self.random_quote()

        if quote is not None:
            event.reply(u"(%i) %s" % (quote.id, quote.quote))

    def quote_by_id(self, id):
        return Quote.query.filter_by(id=id).one()

    def random_quote(self):
        return Quote.query.order_by(func.random()).first()