# Copyright 2013 John Reese
# Licensed under the MIT license

import re

from ..lib import is_bot
from .base import Plugin


class Highbrow(Plugin):
    """Gathers metrics related to how intelligent the conversation (or users)
    may be, like swear words used, etc."""

    # a mapping of swears to regexes that match the variants of the word
    swears = {
        'crap': r'crap(s|py|ped)',
        'shit': r'shits?',
        'fuck': r'fuck(s?|ing|ed)',
        'damn': r'damn(n?it|ed)',
        'hell': r'hell',
    }

    # this regex fragment wraps every swear regex
    swear_regex = r'\b%s\b'

    def process_message(self, message):
        if is_bot(message) or not message.content:
            return

        nick = message.nick
        content = message.content.lower()

        if self._swears is None:
            self._swears = {swear: re.compile(self.swear_regex % regex)
                            for swear, regex in self.swears.items()}

        swears = {swear: len(regex.findall(content))
                  for swear, regex in self._swears.items()}
        swears['total'] = sum(swears.values())

        self.inc_shared_stats(nick, **swears)

    # cache compiled regexes
    _swears = None

@@ -91,16 +91,81 @@
  is_bot%0A
+from ..graphs import NetworkKeyComparison, NetworkUserComparison%0A
 from .ba
@@ -417,16 +417,49 @@
 ars = %7B%0A
+        'ass': r'ass(es%7Chole)?',%0A
         
@@ -482,16 +482,17 @@
 %7Cpy%7Cped)
+?
 ',%0A     
@@ -542,24 +542,25 @@
 k(s?%7Cing%7Ced)
+?
 ',%0A        '
@@ -568,16 +568,22 @@
 amn': r'
+(god)?
 damn(n?i
@@ -587,16 +587,17 @@
 n?it%7Ced)
+?
 ',%0A     
@@ -1267,16 +1267,465 @@
 wears)%0A%0A
+    def generate_graphs(self):%0A        return %5B%0A            NetworkKeyComparison(title='Swears Used',%0A                                 style='bar',%0A                                 keys=%7Bk: k for k in self.swears%7D,%0A                                 ),%0A            NetworkUserComparison(title='Potty Mouths',%0A                                  style='bar',%0A                                  key='total',%0A                                  ),%0A        %5D%0A%0A
     # ca

Add graphs to highbrow plugin