def test_extract_eventualities(): client = ASERClient(port=ASER_PORT) print("=" * 50) print("Testing extract struct...") ans1 = json.dumps(client.extract_eventualities(s1)) ans2 = json.dumps(client.extract_eventualities(s2)) client.close() results_list = [] n_workers = 10 n_iter = 50 pool = Pool(n_workers) for i in range(n_workers // 2): r = pool.apply_async(fn1, args=( s1, n_iter, ans1, "`ext_eventuality_1`({})".format(i), )) results_list.append(r) r = pool.apply_async(fn1, args=( s2, n_iter, ans2, "`ext_eventuality_2`({})".format(i), )) results_list.append(r) pool.close() pool.join() avg_t = np.mean([r.get() for r in results_list]) / n_workers print("`ext_eventuality_struct` {:.2f} ms / call in average".format(avg_t))
def test_all_apis(): client = ASERClient(port=ASER_PORT) eventuality_info_1 = client.extract_eventualities(s1) eventuality1 = eventuality_info_1["eventualities"][0] eventuality_info_2 = client.extract_eventualities(s2) eventuality2 = eventuality_info_2["eventualities"][0] eventuality_info_3 = client.extract_eventualities(s3) eventuality3 = eventuality_info_3["eventualities"][0] rel1 = client.predict_relation(eventuality1, eventuality2) rel2 = client.predict_relation(eventuality1, eventuality3) related_events1 = client.fetch_related_eventualities(eventuality1) related_events2 = client.fetch_related_eventualities(eventuality2) client.close() st = time.time() n_workers = 10 n_iter = 50 pool = Pool(n_workers) pool.apply_async(fn1, args=(s1, n_iter, json.dumps(eventuality_info_1), "`ext_eventuality_0`")) pool.apply_async(fn1, args=(s2, n_iter, json.dumps(eventuality_info_2), "`ext_eventuality_1`")) pool.apply_async(fn2, args=(eventuality1, eventuality2, n_iter, json.dumps(rel1), "`match_relation_2`")) pool.apply_async(fn2, args=(eventuality1, eventuality3, n_iter, json.dumps(rel2), "`match_relation_3`")) pool.apply_async(fn3, args=(eventuality1, n_iter, json.dumps(related_events1), "`get_related_events_4`")) pool.apply_async(fn3, args=(eventuality2, n_iter, json.dumps(related_events2), "`get_related_events_5`")) pool.close() pool.join() end = time.time() print("Overall duration: {:.2f} s".format(end - st))
def fn3(e, n_iter, ans, prefix): client = ASERClient(port=ASER_PORT) st = time.time() for _ in range(n_iter): try: tmp = client.fetch_related_eventualities(e) assert json.dumps(tmp) == ans, "[{}] Wrong answer".format(prefix) except Exception: print("[{}] Wrong answer".format(prefix)) print(traceback.format_exc()) client.close() return end = time.time() print("[{}] passed, {} ms / call".format(prefix, (end - st) / n_iter * 1000)) client.close() return (end - st) / n_iter * 1000
def fn1(s, n_iter, ans, prefix): client = ASERClient(port=ASER_PORT) st = time.time() for _ in range(n_iter): try: tmp = client.extract_eventualities(s) assert json.dumps(tmp) == ans, "[{}] Wrong answer".format(prefix) except Exception: print("[{}] Wrong answer".format(prefix)) print(traceback.format_exc()) client.close() return end = time.time() duration = (end - st) / n_iter * 1000 print("[{}] passed, {:.2f} ms / call".format(prefix, duration)) client.close() return (end - st) / n_iter * 1000
def test_predict_relation(): client = ASERClient(port=ASER_PORT) print("=" * 50) print("Testing match relation...") eventuality1 = client.extract_eventualities(s1, only_eventualities=True)[0] eventuality2 = client.extract_eventualities(s2, only_eventualities=True)[0] eventuality3 = client.extract_eventualities(s3, only_eventualities=True)[0] ans1 = json.dumps(client.predict_relation(eventuality1, eventuality2)) ans2 = json.dumps(client.predict_relation(eventuality1, eventuality3)) client.close() results_list = [] n_workers = 10 n_iter = 50 pool = Pool(n_workers) for i in range(n_workers // 2): r = pool.apply_async(fn2, args=( eventuality1, eventuality2, n_iter, ans1, "`predict_relation_1`({})".format(i), )) results_list.append(r) r = pool.apply_async(fn2, args=( eventuality1, eventuality3, n_iter, ans2, "`predict_relation_2`({})".format(i), )) results_list.append(r) pool.close() pool.join() avg_t = np.mean([r.get() for r in results_list]) / n_workers print("`match_relation` {:.2f} ms / call in average".format(avg_t))
from aser.client import ASERClient from pprint import pprint as print if __name__ == "__main__": client = ASERClient(port=20097, port_out=20098) # s1 = 'they yucca look leggy. they african violet refuse to flower' # s2 = 'they african violet refuse to flower' s1 = 'I am hungry' s2 = 'Evert said' eventuality1 = client.extract_eventualities(s1) eventuality2 = client.extract_eventualities(s2) print("Event 1: ") print(eventuality1) # print(eventuality1[0][0].to_dict()) print("Event 2: ") print(eventuality2) print("Relation: ") rel = client.predict_relation(eventuality1[0][0], eventuality2[0][0]) print(rel) # print("Related events: ") related_events = client.fetch_related_eventualities(eventuality1[0][0]) print(related_events)
def fn1(s, iter_num): client = ASERClient(port=8000) for i in range(iter_num): client.extract_eventualities(s) client.close()
def fn3(e, iter_num): client = ASERClient(port=8000) for i in range(iter_num): client.fetch_related_eventualities(e) client.close()
def fn2(e1, e2, iter_num): client = ASERClient(port=8000) for i in range(iter_num): client.predict_relation(e1, e2) client.close()
def fn2(e1, e2, iter_num): client = ASERClient(port=8000) for i in range(iter_num): client.predict_relation(e1, e2) client.close() def fn3(e, iter_num): client = ASERClient(port=8000) for i in range(iter_num): client.fetch_related_eventualities(e) client.close() if __name__ == "__main__": client = ASERClient(port=8000) # s1 = 'they yucca look leggy' # s2 = 'they african violet refuse to flower' s1 = 'I am hungry' s2 = 'I am in the kitchen' e1 = client.extract_eventualities(s1, only_eventualities=True)[0] e2 = client.extract_eventualities(s2, only_eventualities=True)[0] client.close() total_num = 10000 n_workers = 1 batch_size = total_num // n_workers st = time.time() pool = Pool(n_workers)
import gc import time from tqdm import tqdm from aser.client import ASERClient if __name__ == "__main__": gc.collect() client = ASERClient(port=8000) # s1 = 'they yucca look leggy' # s2 = 'they african violet refuse to flower' s1 = 'I am hungry' s2 = 'I am in the kitchen' eventuality2 = client.extract_eventualities(s2, only_eventualities=True)[0] gc.collect() st = time.time() for i in range(1000): eventuality1 = client.extract_eventualities(s1, only_eventualities=True)[0] print("`extract_eventualities`: {:.2f}ms / call".format( (time.time() - st))) gc.collect() st = time.time() for i in range(1000): rel = client.predict_relation(eventuality1, eventuality2) print("`get_exact_match_relation`: {:.2f}ms / call".format( (time.time() - st))) gc.collect()