import os import sys import time import json import numpy as np import cgi from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler import ssl import math import tensorflow as tf import coref_model as cm import util import coref_mermaid_model as cmm import nltk nltk.download("punkt") from nltk.tokenize import sent_tokenize, word_tokenize from demo import make_predictions, create_example sentence = 'Phi4 Ceti is a solitary, orange-hued star in the equatorial constellation Cetus.\ It is faintly visible to the naked eye with an apparent visual magnitude of 5.61.\ Based upon an annual parallax shift of 10.20 mas as seen from Earth, it is located roughly 320 light years from the Sun.\ At that distance, the visual magnitude of the star is diminished by an extinction factor of 0.10 due to interstellar dust.[4]' sentence = 'Large Electric said the Postal Service connected the Large Electric. Large Electric is great' #sentence = 'In the summer of 2005 , a picture that people have long been looking forward to started emerging with frequency in various major Hong Kong media .With their unique charm , these well - known cartoon images once again caused Hong Kong to be a focus of worldwide attention .' #sentence = 'Strains of anarchism have often been divided into the categories of social and individualist anarchism or similar dual classifications,\ # Many types and traditions of anarchism exist , not all of which are mutually exclusive .' example = create_example(sentence) name = 'best' print "Running experiment: {}.".format(name) config = util.get_config("experiments.conf")[name] config["log_dir"] = util.mkdirs(os.path.join(config["log_root"], name)) util.print_config(config) model = cmm.CorefMermaidModel(config) print('execueted before session') saver = tf.train.Saver() log_dir = config["log_dir"] with tf.Session() as session: checkpoint_path = os.path.join(log_dir, "model.max.ckpt") saver.restore(session, checkpoint_path) tensorized_example = model.tensorize_example(example, is_training=False) feed_dict = {i: t for i, t in zip(model.input_tensors, tensorized_example)} print('prepared to run session!') _, _, _, mention_starts, mention_ends, antecedents, antecedent_scores, head_scores = session.run( model.predictions + [model.head_scores], feed_dict=feed_dict) mention_dict = {} mention_dict.setdefault('mention_starts', mention_starts) mention_dict.setdefault('mention_ends',mention_ends) mention_dict.setdefault('antecedent_scores', antecedent_scores) file1 = file('antecedent.npz','wb') file2 = file('mention_starts.npy','ab') file3 = file('mention_ends.npy','ab') np.savez(file2, mention_starts) np.savez(file3, mention_ends) np.savez(file1, antecedent_scores) wow = np.arange(10) np.savez(file1, wow) #print('mention scores is: ', mention_scores) predicted_antecedents = model.get_predicted_antecedents(antecedents, antecedent_scores) print('antecedent_scores is: \n', antecedent_scores) print('antecedent_scores shape is : \n', len(antecedent_scores), len(antecedent_scores[0])) print('antecedents is: \n', antecedents) example["predicted_clusters"], _ = model.get_predicted_clusters(mention_starts, mention_ends, predicted_antecedents) example["top_spans"] = zip((int(i) for i in mention_starts), (int(i) for i in mention_ends)) example["head_scores"] = head_scores.tolist() print('predicted_antecedentts: ', predicted_antecedents) print('mention start is {},\n, mention end is {}, \n, antecedent scores is {}, \n, head scores is {}'.format( mention_starts, mention_ends, antecedent_scores, head_scores)) print(example) for cluster in example["predicted_clusters"]: words = util.flatten(example["sentences"]) print(u"Predicted cluster: {}".format([" ".join(words[m[0]:m[1]+1]) for m in cluster]))