75 lines
2.2 KiB
Python
75 lines
2.2 KiB
Python
from plugins import PluginBase
|
|
from events import Message
|
|
|
|
from .Formatter import PhraseCountFormatter
|
|
|
|
|
|
def remove_dups(text):
|
|
last_char = None
|
|
for char in list(text):
|
|
if last_char == char:
|
|
continue
|
|
last_char = char
|
|
yield char
|
|
|
|
|
|
class PhraseCounter(PluginBase):
|
|
def __init__(self, *args, out_path=None, phrases=None, template=None,
|
|
detect_spaced=False, detect_extentions=False, debounce_time=1, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if out_path is None or phrases is None or template is None:
|
|
raise ValueError('Missing config requirements')
|
|
|
|
self.out = out_path
|
|
self.phrases = phrases
|
|
self.template = template
|
|
self.detect_extentions = detect_extentions
|
|
self.detect_spaced = detect_spaced
|
|
self.debounce_time = debounce_time
|
|
|
|
self._counts = {phrase: 0 for phrase in phrases + ['sum', 'match', 'nmatch']}
|
|
self._timer = 0
|
|
self._dirty = True
|
|
self._formatter = PhraseCountFormatter()
|
|
|
|
def normalize(self, text):
|
|
if self.detect_extentions:
|
|
text = ''.join(remove_dups(text.lower()))
|
|
if self.detect_spaced:
|
|
text = ''.join(text.split(' '))
|
|
return text
|
|
|
|
def tick(self, dt):
|
|
self._timer += dt
|
|
if self._timer > self.debounce_time and self._dirty:
|
|
out_string = self._formatter.format(self.template, **self._counts)
|
|
with open(self.out, 'w') as f:
|
|
f.write(out_string)
|
|
self._timer = 0
|
|
self._dirty = False
|
|
|
|
def process_control_message(self, event):
|
|
pass
|
|
|
|
def on_event(self, event):
|
|
if not isinstance(event, Message):
|
|
return event
|
|
|
|
self._dirty = True
|
|
normalized = self.normalize(event.text)
|
|
|
|
has_match = False
|
|
for phrase in self.phrases:
|
|
if phrase in normalized:
|
|
self._counts[phrase] += 1
|
|
self._counts['sum'] += 1
|
|
has_match = True
|
|
if has_match:
|
|
self._counts['match'] += 1
|
|
else:
|
|
self._counts['nmatch'] += 1
|
|
|
|
self.tick(self.debounce_time)
|
|
|
|
return event
|