Example #1
0
class Main_Poetry_maker:
    def __init__(self):
        self.planner = Planner()
        self.predictor = Seq2SeqPredictor()
        self.Judge = MatchUtil()

    def predict(self, input_ustr):
        input_ustr = input_ustr.strip()
        keywords = self.planner.plan(input_ustr)
        lines = self.predictor.predict(keywords)
        result = self.Judge.eval_rhyme(lines)
        while (result == False):
            lines = self.predictor.predict(keywords)
            result = self.Judge.eval_rhyme(lines)
        return lines[0] + '   ' + lines[1]
Example #2
0
def main(args, cangtou=False):
    planner = Planner()
    with Seq2SeqPredictor() as predictor:
        # Run loop
        terminate = False
        Judge = MatchUtil()
        while not terminate:
            try:
                input = args.Input.decode('utf-8').strip()
                if not input:
                    print 'Input cannot be empty!'
                elif input.lower() in ['quit', 'exit']:
                    terminate = True
                else:
                    if cangtou:
                        keywords = get_cangtou_keywords(input)
                    else:
                        # Generate keywords
                        keywords = planner.plan(input)

                    # Generate poem
                    lines = predictor.predict(keywords)

                    # whether the couplet is in accordance with the rules
                    result = Judge.eval_rhyme(lines)

                    if result == True:
                        # Print keywords and poem
                        print 'Keyword:\t\tPoem:'
                        for line_number in xrange(2):
                            punctuation = u',' if line_number % 2 == 0 else u'。'
                            print u'{keyword}\t\t{line}{punctuation}'.format(
                                keyword=keywords[line_number],
                                line=lines[line_number],
                                punctuation=punctuation)
                            terminate = True

            except EOFError:
                terminate = True
            except KeyboardInterrupt:
                terminate = True
    print '\nTerminated.'
class Main_Poetry_maker:
    def __init__(self):
        self.planner = Planner()
        self.predictor = Seq2SeqPredictor()
        self.Judge = MatchUtil()

    def predict(self, input_ustr):
        input_ustr = input_ustr.strip()
        keywords = self.planner.plan(input_ustr)
        lines = self.predictor.predict(keywords)
        result = self.Judge.eval_rhyme(lines)
        while (result == False):
            lines = self.predictor.predict(keywords)
            result = self.Judge.eval_rhyme(lines)

        # for line_number in range(2):
        #     punctuation = u',' if line_number % 2 == 0 else u'。'
        #     (u'{keyword}\t\t{line}{punctuation}'.format(
        #             keyword=keywords[line_number],
        #             line=lines[line_number],
        #             punctuation=punctuation
        #     ))
        return ','.join(lines)
Example #4
0
 def __init__(self):
     self.planner = Planner()
     self.predictor = Seq2SeqPredictor()
     self.Judge = MatchUtil()