Пример #1
0
    def match(self, pattern, that, topic):
        """Return the template which is the closest match to pattern. The
        'that' parameter contains the bot's previous response. The 'topic'
        parameter contains the current topic of conversation.

        Returns None if no template is found.
        
        """
        if len(pattern) == 0:
            return None
        print '未分词:', pattern
        #TODO CHINESE seg

        pattern = str(pattern.encode('utf-8'))
        #input=ChineseWordSegmentation.newICT.ICTSeg(pattern)
        input = ChineseWordSegmentation.seg(pattern)
        print '分词后:', input
        #原来的分词
        #pattern = self._lang_support(pattern)

        # Mutilate the input.  Remove all punctuation and convert the
        # text to all caps.
        #seg=ChineseWordSegmentation()
        #seg.seg(pattern)

        #input=ChineseWordSegmentation.seg(pattern)

        #原来的分词
        #input = string.upper(pattern)
        #input = self._puncStripRE.sub("", input)
        #input = self._upuncStripRE.sub(u"", input)
        #print input

        if that.strip() == u"":
            that = u"ULTRABOGUSDUMMYTHAT"  # 'that' must never be empty
        thatInput = string.upper(that)
        thatInput = re.sub(self._whitespaceRE, " ", thatInput)
        thatInput = re.sub(self._puncStripRE, "", thatInput)
        if topic.strip() == u"":
            topic = u"ULTRABOGUSDUMMYTOPIC"  # 'topic' must never be empty
        topicInput = string.upper(topic)
        topicInput = re.sub(self._puncStripRE, "", topicInput)

        # Pass the input off to the recursive call
        patMatch, template = self._match(input.split(), thatInput.split(),
                                         topicInput.split(), self._root)
        return template
Пример #2
0
    def match(self, pattern, that, topic):
        """Return the template which is the closest match to pattern. The
        'that' parameter contains the bot's previous response. The 'topic'
        parameter contains the current topic of conversation.

        Returns None if no template is found.
        
        """
        if len(pattern) == 0:
            return None
        print '未分词:',pattern
        #TODO CHINESE seg

        pattern=str(pattern.encode('utf-8'))
        #input=ChineseWordSegmentation.newICT.ICTSeg(pattern)
        input=ChineseWordSegmentation.seg(pattern)
        print '分词后:',input
        #原来的分词
        #pattern = self._lang_support(pattern)

        # Mutilate the input.  Remove all punctuation and convert the
        # text to all caps.
        #seg=ChineseWordSegmentation()
        #seg.seg(pattern)


        #input=ChineseWordSegmentation.seg(pattern)

        #原来的分词
        #input = string.upper(pattern)
        #input = self._puncStripRE.sub("", input)
        #input = self._upuncStripRE.sub(u"", input)
        #print input

        if that.strip() == u"": that = u"ULTRABOGUSDUMMYTHAT" # 'that' must never be empty
        thatInput = string.upper(that)
        thatInput = re.sub(self._whitespaceRE, " ", thatInput)
        thatInput = re.sub(self._puncStripRE, "", thatInput)
        if topic.strip() == u"": topic = u"ULTRABOGUSDUMMYTOPIC" # 'topic' must never be empty
        topicInput = string.upper(topic)
        topicInput = re.sub(self._puncStripRE, "", topicInput)
        
        # Pass the input off to the recursive call
        patMatch, template = self._match(input.split(), thatInput.split(), topicInput.split(), self._root)
        return template
Пример #3
0
    def star(self, starType, pattern, that, topic, index):
        """Returns a string, the portion of pattern that was matched by a *.

        The 'starType' parameter specifies which type of star to find.
        Legal values are:
         - 'star': matches a star in the main pattern.
         - 'thatstar': matches a star in the that pattern.
         - 'topicstar': matches a star in the topic pattern.

        """
        # Mutilate the input.  Remove all punctuation and convert the
        # text to all caps.
        #pattern = self._lang_support(pattern)
        #pattern = re.sub(self._upuncStripRE, "", pattern)
        #input = string.upper(pattern)
        #input = re.sub(self._puncStripRE, "", input)
        input=ChineseWordSegmentation.seg(pattern)
        if that.strip() == u"": that = u"ULTRABOGUSDUMMYTHAT" # 'that' must never be empty
        thatInput = string.upper(that)
        thatInput = re.sub(self._whitespaceRE, " ", thatInput)
        thatInput = re.sub(self._puncStripRE, "", thatInput)
        if topic.strip() == u"": topic = u"ULTRABOGUSDUMMYTOPIC" # 'topic' must never be empty
        topicInput = string.upper(topic)
        topicInput = re.sub(self._puncStripRE, "", topicInput)

        # Pass the input off to the recursive pattern-matcher
        patMatch, template = self._match(input.split(), thatInput.split(), topicInput.split(), self._root)
        if template == None:
            return ""
        # Extract the appropriate portion of the pattern, based on the
        # starType argument.
        words = None
        if starType == 'star':
            patMatch = patMatch[:patMatch.index(self._THAT)]
            words = input.split()
        elif starType == 'thatstar':
            patMatch = patMatch[patMatch.index(self._THAT)+1 : patMatch.index(self._TOPIC)]
            words = thatInput.split()
        elif starType == 'topicstar':
            patMatch = patMatch[patMatch.index(self._TOPIC)+1 :]
            words = topicInput.split()
        else:
            # unknown value
            raise ValueError, "starType must be in ['star', 'thatstar', 'topicstar']"
        # compare the input string to the matched pattern, word by word.
        # At the end of this loop, if foundTheRightStar is true, start and
        # end will contain the start and end indices (in "words") of
        # the substring that the desired star matched.
        foundTheRightStar = False
        start = end = j = numStars = k = 0
        for i in range(len(words)):
            # This condition is true after processing a star
            # that ISN'T the one we're looking for.
            if i < k:
                continue
            # If we're reached the end of the pattern, we're done.
            if j == len(patMatch):
                break
            if not foundTheRightStar:
                if patMatch[j] in [self._STAR, self._UNDERSCORE]: #we got a star
                    numStars += 1
                    if numStars == index:
                        # This is the star we care about.
                        foundTheRightStar = True
                    start = i
                    # Iterate through the rest of the string.
                    for k in range (i, len(words)):
                        # If the star is at the end of the pattern,
                        # we know exactly where it ends.
                        if j+1  == len (patMatch):
                            end = len (words)
                            break
                        # If the words have started matching the
                        # pattern again, the star has ended.
                        if patMatch[j+1] == words[k]:
                            end = k - 1
                            i = k
                            break
                # If we just finished processing the star we cared
                # about, we exit the loop early.
                if foundTheRightStar:
                    break
            # Move to the next element of the pattern.
            j += 1
            
        # extract the star words from the original, unmutilated input.

        #pattern = self._lang_support.output(pattern)
        if foundTheRightStar:
            #print string.join(pattern.split()[start:end+1])
            if starType == 'star': return self._lang_support.output(string.join(input.split()[start:end+1]))
            elif starType == 'thatstar': return string.join(that.split()[start:end+1])
            elif starType == 'topicstar': return string.join(topic.split()[start:end+1])
        else: return ""