-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_tagged.py
More file actions
54 lines (46 loc) · 1.88 KB
/
Copy pathadd_tagged.py
File metadata and controls
54 lines (46 loc) · 1.88 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
import sys
standardfile = sys.argv[1] # filename
standard_idcolumn = int(sys.argv[2]) # column of the identifier per document
tagged_bool = int(sys.argv[3])
t_column = int(sys.argv[4])
tagged_file = sys.argv[5] # filename
tagged_idcolumn = int(sys.argv[6]) # column of the identifier per document
tagged_column = int(sys.argv[7]) # column of the tagged information
tagged_type = int(sys.argv[8]) # 0 for token, 1 for lemma, 2 for pos, 3 for sentence index
outfile = sys.argv[9]
standard_lines = []
tagged_dict = {}
with open(standardfile, 'r', encoding = 'utf-8') as sf:
for line in sf.readlines():
columns = line.strip().split('\t')
idcolumn = columns[standard_idcolumn]
standard_lines.append((idcolumn, columns))
with open(tagged_file, 'r', encoding = 'utf-8') as tf:
for line in tf.readlines():
columns = line.strip().split('\t')
idcolumn = columns[tagged_idcolumn]
try:
taggedcol = columns[tagged_column]
tags = taggedcol.split()
tagged = []
for tag in tags:
taglist = ['-'] * 4
taglist[tagged_type] = tag
tagged.append(taglist)
tagged_dict[idcolumn] = tagged
except:
continue
with open(outfile, 'w', encoding = 'utf-8') as out:
for line in standard_lines:
try:
new_tags = tagged_dict[line[0]]
if tagged_bool:
tagged_col = [x.split('|') for x in line[1][t_column].split()]
for i, tag in enumerate(tagged_col):
tag[tagged_type] = new_tags[i][tagged_type]
line[1][t_column] = ' '.join(['|'.join(taglist) for taglist in tagged_col])
else:
line[1].append(' '.join(['|'.join(taglist) for taglist in new_tags]))
out.write('\t'.join(line[1]) + '\n')
except:
continue