def eval_generated_data(num=4000): evaluator = RhymeEvaluator() planner = Planner() predictor = Seq2SeqPredictor() poems = [] sentences = [] i = 1 with open("./data/samples/default.txt") as f: for text in f.readlines(): sentences.append(text.strip()[:-1]) if i % 4 == 0: poems.append(sentences) sentences = [] i += 1 # for _ in range(num): # keywords = planner.plan(u'') # assert 4 == len(keywords) # # sentences = predictor.predict(keywords) # poems.append(sentences) print("Testing {} quatrains generated by model.".format(num)) eval_poems(evaluator, poems)
def main(args, cangtou=False): #planner = Planner() with Seq2SeqPredictor() as predictor: # Run loop terminate = False while not terminate: try: input = raw_input('Input Text:\n').decode('utf-8').strip() if not input: print 'Input cannot be empty!' elif input.lower() in ['quit', 'exit']: terminate = True else: lines = predictor.predict(input) lines = lines.replace('UNK', '江') lines = lines[:-1] + rhyme_boosting( lines[-1], get_rhyme(input[-1])) print lines except EOFError: terminate = True except KeyboardInterrupt: terminate = True print '\nTerminated.'
def main(): with Seq2SeqPredictor() as predictor: # Run loop terminate = False while not terminate: try: input = raw_input('Input Text, use | to separate keywords: \n' ).decode('utf-8').strip() if not input: print 'Input cannot be empty!' elif input.lower() in ['quit', 'exit']: terminate = True else: # Generate poems keywords = input.split('|') lines = predictor.predict(keywords) # Print keywords and poems print 'Keyword:\tSentence:' for line_number in xrange(len(lines)): punctuation = u',' if line_number == len(lines) - 1: punctuation = u'。' print u'{keyword}\t{line}{punctuation}'.format( keyword=keywords[line_number], line=lines[line_number], punctuation=punctuation) except EOFError: terminate = True except KeyboardInterrupt: terminate = True print '\nTerminated.'
def generate_rnn_samples(sampled_poems): planner = Planner() with Seq2SeqPredictor() as predictor: with open(rnn_samples_path, 'w+') as fout: for poem_idx, poem in enumerate(sampled_poems): input = string.join(poem).strip() keywords = planner.plan(input) print 'Predicting poem {}.'.format(poem_idx) lines = predictor.predict(keywords) for idx, sentence in enumerate(lines): punctuation = u'\uff0c' if idx % 2 == 0 else u'\u3002' line = (sentence + punctuation + '\n').encode('utf-8') fout.write(line)
def eval_generated_data(num=100): evaluator = RhymeEvaluator() planner = Planner() predictor = Seq2SeqPredictor() poems = [] for _ in range(num): keywords = planner.plan(u'') assert 4 == len(keywords) sentences = predictor.predictor(keywords) poems.append(sentences) print("Testing {} quatrains generated by model.".format(num)) eval_poems(evaluator, poems)
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.'
def main(args, cangtou=False): planner = Planner() with Seq2SeqPredictor() as predictor: # Run loop terminate = False while not terminate: try: input = raw_input('Input Text:\n').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 if args.nocouplet: lines = predictor.predict(keywords) else: lines = predictor.predict_with_couplet(keywords) # Print keywords and poem print 'Keyword:\t\tPoem:' for line_number in xrange(4): 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) except EOFError: terminate = True except KeyboardInterrupt: terminate = True print '\nTerminated.'
def main(cangtou=False): planner = Planner() with Seq2SeqPredictor() as predictor: # Run loop terminate = False while not terminate: try: import ipdb ipdb.set_trace() input = input('Input Text:\n').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) # Print keywords and poem print('Keyword:\t\tPoem:') for line_number in range(4): punctuation = ',' if line_number % 2 == 0 else '。' print('{keyword}\t\t{line}{punctuation}'.format( keyword=keywords[line_number], line=lines[line_number], punctuation=punctuation)) except EOFError: terminate = True except KeyboardInterrupt: terminate = True print('\nTerminated.')
def main(cangtou=False): planner = Planner() with Seq2SeqPredictor() as predictor: # Run loop terminate = False while not terminate: try: inputs =input('Input Text:\n').strip() if not inputs: print( 'Input cannot be empty!') elif inputs.lower() in ['quit', 'exit']: terminate = True else: if cangtou: keywords = get_cangtou_keywords(inputs) else: # Generate keywords #将输入的句子切词并按照textrank 值进行降序排列,并且选择前四个词作为keyword keywords = planner.plan(inputs) print(keywords) # Generate poem lines = predictor.predict(keywords) # Print keywords and poem print( 'Keyword:\t\tPoem:') for line_number in range(4): 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 )) except EOFError: terminate = True except KeyboardInterrupt: terminate = True print ('\nTerminated.')
def __init__(self): self.planner = Planner() self.predictor = Seq2SeqPredictor() self.Judge = MatchUtil()
def main(args): #planner = Planner() with Seq2SeqPredictor() as predictor: def client_thread(conn, ip, port, MAX_BUFFER_SIZE=4096): # the input is in bytes, so decode it input_from_client_bytes = conn.recv(MAX_BUFFER_SIZE) # MAX_BUFFER_SIZE is how big the message can be # this is test if it's sufficiently big import sys siz = sys.getsizeof(input_from_client_bytes) if siz >= MAX_BUFFER_SIZE: print( "The length of input is probably too long: {}".format(siz)) # decode input and strip the end of line input_from_client = input_from_client_bytes.decode("utf8").rstrip() ryhme_sentence, input_sentence = input_from_client[:len( input_from_client ) / 2], input_from_client[len(input_from_client) / 2:] res = predictor.predict(input_sentence) res = res.replace('UNK', '江') if args.fixrhyme: res = res[:-1] + \ rhyme_boosting(res[-1], ryhme_sentence[-1]) print("Result of processing {} is: {}".format(input_sentence, res)) vysl = res.encode("utf8") # encode the result string conn.sendall(vysl) # send it to client conn.close() # close connection print('Connection ' + ip + ':' + port + " ended") def start_server(): import socket soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # this is for easy starting/killing the app soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) print('Socket created') try: soc.bind(("127.0.0.1", 12345)) print('Socket bind complete') except socket.error as msg: import sys print('Bind failed. Error : ' + str(sys.exc_info())) sys.exit() # Start listening on socket soc.listen(10) print('Socket now listening') # for handling task in separate jobs we need threading from threading import Thread # this will make an infinite loop needed for # not reseting server for every client while True: conn, addr = soc.accept() ip, port = str(addr[0]), str(addr[1]) print('Accepting connection from ' + ip + ':' + port) try: Thread(target=client_thread, args=(conn, ip, port)).start() except: print("Terible error!") import traceback traceback.print_exc() soc.close() start_server()
def main(): #planner = Planner() with Seq2SeqPredictor() as predictor: def client_thread(conn, ip, port, MAX_BUFFER_SIZE = 4096): # the input is in bytes, so decode it input_from_client_bytes = conn.recv(MAX_BUFFER_SIZE) # MAX_BUFFER_SIZE is how big the message can be # this is test if it's sufficiently big import sys siz = sys.getsizeof(input_from_client_bytes) if siz >= MAX_BUFFER_SIZE: print("The length of input is probably too long: {}".format(siz)) # decode input and strip the end of line input_from_client = input_from_client_bytes.decode("utf8").rstrip() res = predictor.predict(input_from_client) print("Result of processing {} is: {}".format(input_from_client, res)) vysl = res.encode("utf8") # encode the result string conn.sendall(vysl) # send it to client conn.close() # close connection print('Connection ' + ip + ':' + port + " ended") def start_server(): import socket soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # this is for easy starting/killing the app soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) print('Socket created') try: soc.bind(("127.0.0.1", 12345)) print('Socket bind complete') except socket.error as msg: import sys print('Bind failed. Error : ' + str(sys.exc_info())) sys.exit() #Start listening on socket soc.listen(10) print('Socket now listening') # for handling task in separate jobs we need threading from threading import Thread # this will make an infinite loop needed for # not reseting server for every client while True: conn, addr = soc.accept() ip, port = str(addr[0]), str(addr[1]) print('Accepting connection from ' + ip + ':' + port) try: Thread(target=client_thread, args=(conn, ip, port)).start() except: print("Terible error!") import traceback traceback.print_exc() soc.close() start_server() # Run loop terminate = False while not terminate: try: input = raw_input('Input Text:\n').decode('utf-8').strip() if not input: print 'Input cannot be empty!' elif input.lower() in ['quit', 'exit']: terminate = True else: lines = predictor.predict(input) print lines except EOFError: terminate = True except KeyboardInterrupt: terminate = True print '\nTerminated.'
import codecs from predict import Seq2SeqPredictor if __name__ == '__main__': if len(sys.argv) < 4: print "Usage: python", sys.argv[0], "<input> <count> <output>" exit(-1) else: fn_in = sys.argv[1] num = int(sys.argv[2]) fn_out = sys.argv[3] fr = codecs.open(fn_in, 'r', 'utf-8') fw = codecs.open(fn_out, 'w', 'utf-8') with Seq2SeqPredictor() as predictor: for line in fr: line = line.strip() # topic examples # 时尚教主_喜悦 潮人 气派 时尚教主 高兴 为之一喜 欢呼雀跃 # 时尚教主_喜悦 潮人 兴高彩烈 教主 可贺 惊喜万分 手舞足蹈 [label, topic] = line.split('\t') keywords = topic.split('|') # predict poetry lines, totally num poems for k in range(0, num): # output label and keywords first fw.write(label + '\n')