-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_excel.py
More file actions
117 lines (101 loc) · 3.95 KB
/
Copy pathcreate_excel.py
File metadata and controls
117 lines (101 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- coding: utf-8 -*-
from add_null import extract_all_sentences
from random import shuffle
from pickle import load
from pandas import DataFrame
from os import remove
import argparse
def add_article_tag(line):
"""
FUNCTION:
---------------
adds a marker to a random article in a line (sentence)
PARAMETERS
---------------
line: a sentence that will have one of its null articles marked
OUTPUT(s)
---------------
a line with a marked null article
"""
articles = []
for i, double in enumerate(line):
if double[1] == "AT0":
articles.append(i)
shuffle(articles)
chosen_article = articles[0]
line[chosen_article] = (f"_{line[chosen_article][0]}_", line[chosen_article][1])
return line
def create_excel(input_file, output_file, target_file="source_sen", sen_markers = ['.','?','!']):
"""
FUNCTION:
---------------
create an excel file with sampled sentences surrounded by their context sentences.
PARAMETERS
---------------
input_file:
a .txt file created by the "extract_all_sentences" function
output_file:
path of the file the output should be written to
target_file:
path to the location of the file with the target sentences (default as source_sen.txt)
OUTPUT(s)
---------------
"""
data_read = (line for line in open("{}.txt".format(input_file), 'r', encoding="utf-8"))
data_dic = {}
targets, sourceIDs, sentences = [], [], []
sen = ""
cur_code = 2
for line in data_read:
line = f"[{line}]"
eval_line = eval(line)
if eval_line[0][0] == "CONTEXTA":
target_code = 1
elif eval_line[0][0] == "CONTEXTB":
target_code = -1
else:
target_code = 0
eval_line = add_article_tag(eval_line)
if cur_code != target_code and cur_code != 2:
targets.append(cur_code)
sourceIDs.append(sourceID)
sentences.append(sen)
sen = ""
sourceID = eval_line[-1][0]
for sen_token in eval_line[1:-1]:
word, tag = sen_token
if "APOST" in word:
word = word.replace("APOST", "'")
if "SPCHMRK" in word:
word = word.replace("SPCHMRK", '"')
if tag in ["PUN", "POS"]:
sen += "" + word
else:
sen += " " + word
cur_code = target_code
sen += "; "
data_dic["TARGET"] = targets
data_dic["ID"] = sourceIDs
data_dic["Sentence"] = sentences
data_df = DataFrame(data_dic)
data_df.to_excel(f"{output_file}.xlsx", index=False)
def run():
parser = argparse.ArgumentParser()
# default is output.txt
parser.add_argument('in_file', type=str, help='INPUT: location of the POS-tagged .txt file with null tags added - stored as two coumns, one for the tag and the other for the token')
# new_output
parser.add_argument('out_file', type=str, help='CREATED: location of the output file. A .xlsx file with rows for each sentence, rows where the sentence is a context sentence will have multiple sentences in the "sentence" column')
# source_sen
parser.add_argument('ta', type=str, default="source_sen", help='INPUT: location of a file that includes all the sampled sentences, one per line.')
# sources_list.pickle
parser.add_argument('sl', type=str, default="sources_list", help='INPUT: location of the .pkl that stores the list of source IDs')
args = parser.parse_args()
aux_file = "temp_sens"
target_file = "source_sen"
with open(f"{args.sl}.pkl", "rb") as sl:
sources = load(sl)
extract_all_sentences(args.in_file, aux_file, sen_markers = sources)
create_excel(aux_file, args.out_file, args.ta)
remove(f"{aux_file}.txt")
if __name__ == "__main__":
run()