Example #1
0
    def test_itime(self):
        seq = ["one", "two", "three"]

        # Surely the unit test harness can yield the three above items in 1s
        self.assertSequenceEqual(seq, list(utils.itime(seq, 1000)))

        # Give a negative time and make sure at least one item is
        # returned
        self.assertSequenceEqual(["one"], list(utils.itime(seq, -1)))

        # Create a generator that yields one item every quarter second.
        def delay(items):
            for item in items:
                time.sleep(0.10)
                yield item

        # 0.00s: sleep
        # 0.10s: yield "one"
        # 0.20s: yield "two"
        # 0.30s: yield "three"

        # Make sure the right number of elements from seq are yielded
        # for various lengths of time.
        self.assertSequenceEqual(["one"],
                                 list(utils.itime(delay(seq), 0.05)))
        self.assertSequenceEqual(["one"],
                                 list(utils.itime(delay(seq), 0.15)))
        self.assertSequenceEqual(["one", "two"],
                                 list(utils.itime(delay(seq), 0.25)))
        self.assertSequenceEqual(["one", "two", "three"],
                                 list(utils.itime(delay(seq), 0.35)))
        self.assertSequenceEqual(["one", "two", "three"],
                                 list(utils.itime(delay(seq), 1.00)))
Example #2
0
    def test_itime(self):
        seq = ["one", "two", "three"]

        # Surely the unit test harness can yield the three above items in 1s
        self.assertSequenceEqual(seq, list(utils.itime(seq, 1000)))

        # Give a negative time and make sure at least one item is
        # returned
        self.assertSequenceEqual(["one"], list(utils.itime(seq, -1)))

        # Create a generator that yields one item every quarter second.
        def delay(items):
            for item in items:
                time.sleep(0.10)
                yield item

        # 0.00s: sleep
        # 0.10s: yield "one"
        # 0.20s: yield "two"
        # 0.30s: yield "three"

        # Make sure the right number of elements from seq are yielded
        # for various lengths of time.
        self.assertSequenceEqual(["one"], list(utils.itime(delay(seq), 0.05)))
        self.assertSequenceEqual(["one"], list(utils.itime(delay(seq), 0.15)))
        self.assertSequenceEqual(["one", "two"],
                                 list(utils.itime(delay(seq), 0.25)))
        self.assertSequenceEqual(["one", "two", "three"],
                                 list(utils.itime(delay(seq), 0.35)))
        self.assertSequenceEqual(["one", "two", "three"],
                                 list(utils.itime(delay(seq), 1.00)))
Example #3
0
File: brain.py Project: wodim/cobe
    def reply(self, text):
        # Create a search query from the input
        query = self.analyzer.query(text, self.model)

        # Track (and don't re-score) replies that have already been
        # seen. These are expected when using a random walk searcher,
        # but they're also useful when debugging searches.
        seen = set()

        join = self.analyzer.join
        entropy = self.model.entropy

        def score(reply):
            joined = join(reply)
            if joined in seen:
                return -1.0, joined

            seen.add(joined)
            n_tokens = len(reply)

            # Penalize longer replies (cobe 2.x compatibility)
            penalty = 1.0
            if n_tokens > 24:
                penalty = math.sqrt(n_tokens)
            elif n_tokens > 48:
                penalty = n_tokens

            joined = join(reply)
            return entropy(joined) / penalty, joined

        # This search is a generator; it doesn't start evaluating until read
        search = itime(self.searcher.search(query), 0.5)

        # Generate and score the search results.
        results = sorted(itertools.imap(score, search))

        if log.isEnabledFor(logging.DEBUG):
            for score, text in results:
                log.debug("%.4f %s", score, text)

            log.debug("made %d replies (%d unique)", len(results), len(seen))

        score, reply = results[-1]
        return reply
Example #4
0
    def reply(self, text):
        # Create a search query from the input
        query = self.analyzer.query(text, self.model)

        # Track (and don't re-score) replies that have already been
        # seen. These are expected when using a random walk searcher,
        # but they're also useful when debugging searches.
        seen = set()

        join = self.analyzer.join
        entropy = self.model.entropy

        def score(reply):
            joined = join(reply)
            if joined in seen:
                return -1.0, joined

            seen.add(joined)
            n_tokens = len(reply)

            # Penalize longer replies (cobe 2.x compatibility)
            penalty = 1.0
            if n_tokens > 16:
                penalty = math.sqrt(n_tokens)
            elif n_tokens > 32:
                penalty = n_tokens

            joined = join(reply)
            return entropy(joined) / penalty, joined

        # This search is a generator; it doesn't start evaluating until read
        search = itime(self.searcher.search(query), 0.5)

        # Generate and score the search results.
        results = sorted(itertools.imap(score, search))

        if log.isEnabledFor(logging.DEBUG):
            for score, text in results:
                log.debug("%.4f %s", score, text)

            log.debug("made %d replies (%d unique)", len(results), len(seen))

        score, reply = results[-1]
        return reply