Beispiel #1
0
def complete_trigger(server, message, query):
    """
    - Syntax: [!@](complete|suggest) 03query
    - Description: Ask Google for similar search queries.
    """
    result = suggest(query)
    q = query.lower().strip().strip("\"'")

    if result:
        if message.prefix == ".":
            parsed = []
            for i in result:
                if i.startswith(q):
                    i = i.replace(
                        q, "" if i.startswith(q) else "\x0315%s\x03" % q,
                        1).rstrip()
                    if not i.startswith(" "):
                        i = "\x0315%s\x03%s" % (q, i)
                    else:
                        i = i.strip()
                else:
                    i = i.replace(q, "\x0315%s\x03" % q)
                parsed.append(i)
            line = parsed.pop(0)
            while parsed and striplen(line + parsed[0]) + 3 < 60:
                line += " \x0312·\x03 " + parsed.pop(0)
            yield "12│ %s 12│ %s" % (query, line)
        else:
            result = [i.replace(q, "\x0315%s\x03" % q) for i in result]
            table = namedtable(
                result,
                size=72,
                rowmax=4 if message.text.startswith("@") else None,
                header="12G04o08o12g03l04e12 suggest  ",
                rheader="'%s'" % query)
            for line in table:
                yield line
    else:
        yield "05Google suggest│  No results."
Beispiel #2
0
def complete_trigger(server, message, query):
    """
    - Syntax: [!@](complete|suggest) 03query
    - Description: Ask Google for similar search queries.
    """
    result = suggest(query)
    q = query.lower().strip().strip("\"'")

    if result:
        if message.prefix == ".":
            parsed = []
            for i in result:
                if i.startswith(q):
                    i = i.replace(q, "" if i.startswith(q) else "\x0315%s\x03" % q, 1).rstrip()
                    if not i.startswith(" "):
                        i = "\x0315%s\x03%s" % (q, i)
                    else:
                        i = i.strip()
                else:
                    i = i.replace(q, "\x0315%s\x03" % q) 
                parsed.append(i)
            line = parsed.pop(0)
            while parsed and striplen(line + parsed[0]) + 3 < 60:
                line += " \x0312·\x03 " + parsed.pop(0)
            yield "12│ %s 12│ %s" % (query, line)
        else:
            result = [i.replace(q, "\x0315%s\x03" % q) for i in result]
            table = namedtable(result, 
                               size    = 72, 
                               rowmax  = 4 if message.text.startswith("@") else None, 
                               header  = "12G04o08o12g03l04e12 suggest  ", 
                               rheader = "'%s'" % query)
            for line in table:
                yield line
    else:
        yield "05Google suggest│  No results."
Beispiel #3
0
    def wolfram_format(self,
                       query,
                       category=None,
                       h_max=None,
                       user=None,
                       wasettings={}):
        try:
            if self.last is not None:
                query = query.replace("$_", self.last)
            answer, url = parallelise([
                lambda: self.wolfram(query, **wasettings),
                lambda: URL.shorten("http://www.wolframalpha.com/input/?i=%s" %
                                    urllib.parse.quote_plus(query))
            ])
            if "Result" in answer and "(" not in answer["Result"]:
                self.last = answer["Result"]
            else:
                self.last = "(%s)" % query
        except urllib.error.URLError:
            return "05Wolfram08Alpha failed to respond. Try again later or go to " + URL.format(
                url)

        if not answer:
            return "05Wolfram08Alpha returned no results for '07%s'" % query

        for i in self.input_categories:
            if i in answer:
                header = answer[i]
                remove = i
                break
        else:
            header = "'%s'" % query
            remove = False

        header = str.join(" ", header.split())

        h_max = h_max or self.h_max

        if not category:
            results = answer
            if remove:
                results = collections.OrderedDict([(k, v)
                                                   for k, v in answer.items()
                                                   if k != remove])

            if category is not None:
                # Guess the category
                for i in self.results:
                    if i in answer:
                        results = {i: results[i]}
                        break
                else:
                    results = {
                        list(results.keys())[0]: list(results.values())[0]
                    }
        else:
            results = max(answer,
                          key=lambda x: difflib.SequenceMatcher(
                              None, category, x).ratio())
            results = {results: answer[results]}

        output = [
            spacepad("05Wolfram08Alpha 04 ", " %s" % header, self.t_max)
        ]
        t_max = striplen(output[0])

        results = collections.OrderedDict([
            (k, self.breakdown(v.split("\n"), t_max - 3))
            for k, v in results.items()
        ])

        total_lines = sum([min(len(results[x]), h_max) for x in results])

        if total_lines > self.t_lines:
            # Too many lines, print the available categories instead.
            for i in justifiedtable(
                    sorted(results.keys(), key=len) + ["05Categories"],
                    t_max - 3):
                output.append(" 04⎪ %s" % i)
            output[-1] = " 04⎩" + output[-1][5:]
        elif results:
            if len(results) == 1 and len(list(results.values())[0]) == 1:
                # Single line: Shorten output
                catname = list(results.keys())[0]
                if catname in self.results:
                    output = ["08│ %s " % list(results.values())[0][0]]
                else:
                    output = [
                        spacepad("08│ %s " % list(results.values())[0][0],
                                 "07%s" % catname, self.t_max)
                    ]
            else:
                for category in results:
                    lines = [x.rstrip() for x in results[category]]

                    output.append(
                        spacepad(" 08⎨ %s " % lines.pop(0),
                                 " 07%s" % category, t_max))
                    truncated = lines[:h_max]
                    for line in truncated:
                        output.append(" 08⎪ " + line)

                    if len(truncated) < len(lines):
                        omission = "%d more lines" % (len(lines) - h_max + 1)
                        length = t_max - len(omission) - 6 - len(url)
                        output[-1] = " 08� " + url + " " + (
                            "-" * int(length)) + " 07%s" % omission
        else:
            output.append(" 08‣ 05No plaintext results. See " + URL.format(
                URL.shorten("http://www.wolframalpha.com/input/?i=%s" %
                            urllib.parse.quote_plus(query))))
        return "\n".join(i.rstrip() for i in output)
Beispiel #4
0
            def wolfram_format(self, query, category=None, h_max=None):
                try:
                    answer = self.wolfram(query)
                except urllib.error.URLError:
                    return "05Wolfram08Alpha failed to respond. Try again later or go to " + URL.format(URL.shorten("http://www.wolframalpha.com/input/?i=%s" % urllib.parse.quote_plus(query)))
                    
                if not answer:
                    return "05Wolfram08Alpha returned no results for '07%s'" % query
                
                for i in self.input_categories:
                    if i in answer:
                        header = answer[i]
                        remove = i
                        break
                else: 
                    header ="'%s'"%query
                    remove = False
                
                header = str.join(" ", header.split())
                
                h_max = h_max or self.h_max
                
                if not category:
                    results = answer
                    if remove:
                        results = collections.OrderedDict([(k, v) for k, v in answer.items() if k != remove])
                    
                    if category is not None:
                        # Guess the category
                        for i in self.results:
                            if i in answer:
                                results = {i: results[i]}
                                break
                        else:
                            results = {list(results.keys())[0]: list(results.values())[0]}
                else:
                    results = max(answer, key=lambda x:difflib.SequenceMatcher(None, category, x).ratio())
                    results = {results:answer[results]}
                
                output = [spacepad("05Wolfram08Alpha 04 ", " %s" % header, self.t_max)]
                t_max = striplen(output[0])
                
                results = collections.OrderedDict([(k, self.breakdown(v.split("\n"), t_max - 3)) for k, v in results.items()])
                
                total_lines = sum([min(len(results[x]), h_max) for x in results])
                
                if total_lines > self.t_lines:
                    # Too many lines, print the available categories instead.
                    for i in justifiedtable(sorted(results.keys(), key=len) + ["05Categories"], t_max-3):
                        output.append(" 04⎪ %s" % i)
                    output[-1] = " 04⎩" + output[-1][5:]
                elif results:
                    if len(results) == 1 and len(list(results.values())[0]) == 1:
                        # Single line: Shorten output
                        catname = list(results.keys())[0]
                        if catname in self.results:
                            output = ["08│ %s " % list(results.values())[0][0]]
                        else:
                            output = [spacepad("08│ %s " % list(results.values())[0][0], "07%s" % catname, t_max)]
                    else:
                        for category in results:
                            lines = [x.rstrip() for x in results[category]]

                            output.append(spacepad(" 08⎨ %s " % lines.pop(0), " 07%s" % category, t_max))
                            truncated = lines[:h_max]
                            for line in truncated:
                                output.append(" 08⎪ " + line)

                            if len(truncated) < len(lines):
                                omission = "%d more lines" % (len(lines) - h_max)
                                length = t_max - len(omission) - 5
                                output[-1] = " 08⎬�" + ("-"*int(length)) + " 07%s" % omission
                else:
                    output.append(" 08‣ 05No plaintext results. See " + URL.format(URL.shorten("http://www.wolframalpha.com/input/?i=%s" % urllib.parse.quote_plus(query))))
                return "\n".join(i.rstrip() for i in output)