Create compactness_measurements.py
Browse files- compactness_measurements.py +111 -0
compactness_measurements.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import stanza
|
| 3 |
+
|
| 4 |
+
nlp = stanza.Pipeline(lang='en', processors='tokenize,pos,lemma,depparse')
|
| 5 |
+
articles = ['a', 'an', 'the']
|
| 6 |
+
|
| 7 |
+
# input file is the compactIE output extractions on a set of sentences. set this variable accordingly.
|
| 8 |
+
INPUT_FILE = 'compactIE_predictions.txt'
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def verb_count(part, sent):
|
| 12 |
+
s_doc = nlp(sent)
|
| 13 |
+
text2token = {}
|
| 14 |
+
for i in range(len(s_doc.sentences)):
|
| 15 |
+
tokens = [word.to_dict() for word in s_doc.sentences[i].words]
|
| 16 |
+
for t in tokens:
|
| 17 |
+
text2token[t["text"]] = t
|
| 18 |
+
doc = nlp(part)
|
| 19 |
+
doc = doc.sentences[0]
|
| 20 |
+
tokens = [word.to_dict() for word in doc.words]
|
| 21 |
+
verbs = 0
|
| 22 |
+
for token in tokens:
|
| 23 |
+
if (token['upos'] == 'VERB' and (token['deprel'] not in ['xcomp', 'amod', 'case', 'obl'])) or (token['upos'] == "AUX" and token['deprel'] == 'cop'):
|
| 24 |
+
try:
|
| 25 |
+
if text2token[token["text"]]["deprel"] == token['deprel']:
|
| 26 |
+
# print(token["text"], token["deprel"])
|
| 27 |
+
verbs += 1
|
| 28 |
+
except:
|
| 29 |
+
continue
|
| 30 |
+
return verbs
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def clausal_constituents(extraction):
|
| 34 |
+
clausal_consts = 0
|
| 35 |
+
if extraction["predicate"].strip() != "":
|
| 36 |
+
pred_count = verb_count(extraction["predicate"], extraction["sentence"])
|
| 37 |
+
if pred_count > 1:
|
| 38 |
+
clausal_consts += pred_count - 1
|
| 39 |
+
|
| 40 |
+
if extraction["subject"].strip() != "":
|
| 41 |
+
clausal_consts += verb_count(extraction["subject"], extraction["sentence"])
|
| 42 |
+
|
| 43 |
+
if extraction["object"].strip() != "":
|
| 44 |
+
clausal_consts += verb_count(extraction["object"], extraction["sentence"])
|
| 45 |
+
# if clausal_consts > 0:
|
| 46 |
+
# print("clausal consts within extraction: ", extraction["subject"], extraction["predicate"], extraction["object"], clausal_consts)
|
| 47 |
+
return clausal_consts
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
extractions_df = pd.DataFrame(columns=["sentence", "subject", "predicate", "object"])
|
| 52 |
+
|
| 53 |
+
with open(INPUT_FILE, 'r') as f:
|
| 54 |
+
lines = f.readlines()
|
| 55 |
+
|
| 56 |
+
sentences = set()
|
| 57 |
+
for line in lines:
|
| 58 |
+
sentence, ext, score = line.split('\t')
|
| 59 |
+
sentences.add(sentence)
|
| 60 |
+
try:
|
| 61 |
+
arg1 = ext[ext.index('<arg1>') + 6:ext.index('</arg1>')]
|
| 62 |
+
except:
|
| 63 |
+
arg1 = ""
|
| 64 |
+
try:
|
| 65 |
+
rel = ext[ext.index('<rel>') + 5:ext.index('</rel>')]
|
| 66 |
+
except:
|
| 67 |
+
rel = ""
|
| 68 |
+
try:
|
| 69 |
+
arg2 = ext[ext.index('<arg2>') + 6:ext.index('</arg2>')]
|
| 70 |
+
except:
|
| 71 |
+
arg2 = ""
|
| 72 |
+
row = pd.DataFrame(
|
| 73 |
+
{"sentence": [sentence],
|
| 74 |
+
"subject": [arg1],
|
| 75 |
+
"predicate": [rel],
|
| 76 |
+
"object": [arg2]}
|
| 77 |
+
)
|
| 78 |
+
extractions_df = pd.concat([extractions_df, row])
|
| 79 |
+
|
| 80 |
+
# overlapping arguments
|
| 81 |
+
grouped_df = extractions_df.groupby("sentence")
|
| 82 |
+
total_number_of_arguments = 0
|
| 83 |
+
number_of_unique_arguments = 0
|
| 84 |
+
num_of_sentences = len(grouped_df.groups.keys())
|
| 85 |
+
for sent in grouped_df.groups.keys():
|
| 86 |
+
per_sentence_argument_set = set()
|
| 87 |
+
sen_group = grouped_df.get_group(sent).reset_index(drop=True)
|
| 88 |
+
extractions_list = list(sen_group.T.to_dict().values())
|
| 89 |
+
for extr in extractions_list:
|
| 90 |
+
if extr["subject"] not in ['', 'nan']:
|
| 91 |
+
total_number_of_arguments += 1
|
| 92 |
+
per_sentence_argument_set.add(extr["subject"])
|
| 93 |
+
if extr["object"] not in ['', 'nan']:
|
| 94 |
+
total_number_of_arguments += 1
|
| 95 |
+
per_sentence_argument_set.add(extr["object"])
|
| 96 |
+
number_of_unique_arguments += len(per_sentence_argument_set)
|
| 97 |
+
|
| 98 |
+
print("average # repetitions per argument: {}".format(total_number_of_arguments/number_of_unique_arguments))
|
| 99 |
+
print("average # extractions per sentence: {}".format(extractions_df.shape[0]/len(sentences)))
|
| 100 |
+
avg_arguments_size = 0.0
|
| 101 |
+
for sent in sentences:
|
| 102 |
+
extractions_per_sent = extractions_df[extractions_df["sentence"] == sent]
|
| 103 |
+
|
| 104 |
+
sent_extractions = extractions_per_sent.shape[0]
|
| 105 |
+
extractions_per_sent["avg_arg_length"] = extractions_per_sent.apply(lambda r: (len(str(r["subject"]).split(' ')) + len(str(r["predicate"]).split(' ')) + len(str(r["object"]).split(' ')))/3, axis=1)
|
| 106 |
+
avg_arguments_size += sum(extractions_per_sent["avg_arg_length"].values.tolist()) / extractions_per_sent.shape[0]
|
| 107 |
+
|
| 108 |
+
print("average length of constituents (per sentence, per extraction): ", avg_arguments_size/len(sentences))
|
| 109 |
+
extractions_df["clause_counts"] = extractions_df.apply(lambda r: clausal_constituents(r), axis=1)
|
| 110 |
+
avg_clause_count = sum(extractions_df["clause_counts"].values.tolist()) / len(sentences)
|
| 111 |
+
print("number of verbal clauses within arguments: ", avg_clause_count)
|