-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_XML.py
202 lines (161 loc) · 9.49 KB
/
parse_XML.py
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 23 11:54:21 2022
@author: Sagun Shakya
"""
import os
import xml.etree.ElementTree as ET
from tqdm import tqdm
from pandas import DataFrame
import argparse
from utils import get_filenames
from time import time
def get_sent_tags(path):
'''
Gets the list of all the tokens and tags from a directory of Nepali National Corpus.
Parameters
----------
path : str
Path to the XML files under NNC project.
Raises
------
FileNotFoundError
if the directory doesn't exist.
Returns
-------
Pandas DataFrame.
DataFrame containing words (list) and corresponding tags (list).
Columns -- ["words", "tags"]
'''
if os.path.exists(path):
filelist = get_filenames(path, verbose=False)
else:
raise FileNotFoundError
ALLTAG= []
ALLWORD = []
for ii, file in tqdm(enumerate(filelist), desc = "Loop file"):
if file.endswith('.xml'):
filepath = os.path.join(path, file)
#print(filepath)
tree = ET.parse(filepath)
root = tree.getroot()
print(f"\nProcessing the file: {file}")
for data in tqdm(root.findall('text'), desc = "Loop Text"):
for value in data:
# #print(value.tag)
if (value.tag == 'group'):
# for group in value.findall('group'):
for body in value.findall('body'):
for div in body.findall('div'):
for subdiv in div:
# #print(subdiv.tag)
if (subdiv.tag == "head"):
for sentence in subdiv.findall('s'):
# #print(sentence.attrib)
tags_i = []
words_i = []
for s in sentence:
# #print(s.attrib)
if (s.tag == "foreign"):
for words in s.findall('w'):
tags_i.append(words.attrib['ctag'])
words_i.append(words.text)
#print("%s/%s" % (words.text, words.attrib['ctag']), '', end=''),
# #print("\n")
if (s.tag == "w"):
tags_i.append(s.attrib['ctag'])
words_i.append(s.text)
#print("%s/%s" % (s.text, s.attrib['ctag']), '', end=''),
# countTags(tags,s.attrib['ctag'])
if len(words_i) > 4:
ALLTAG.append(tags_i)
ALLWORD.append(words_i)
#print('\n')
if (subdiv.tag == "p"):
for sentence in subdiv.findall('s'):
#print(sentence.attrib)
tags_j = []
words_j = []
for s in sentence:
if (s.tag == "foreign"):
for words in s.findall('w'):
tags_j.append(words.attrib['ctag'])
words_j.append(words.text)
#print("%s/%s" % (words.text, words.attrib['ctag']), '', end=''),
# countTags(tags,words.attrib['ctag'])
# #print("\n")
if (s.tag == "w"):
tags_j.append(s.attrib['ctag'])
words_j.append(s.text)
#print("%s/%s" % (s.text, s.attrib['ctag']), '', end=''),
# countTags(tags,s.attrib['ctag'])
if len(words_j) > 4:
ALLTAG.append(tags_j)
ALLWORD.append(words_j)
#print('\n')
if (value.tag == 'body'):
# for body in value.findall('body'):
for div in value.findall('div'):
for subdiv in div:
# #print(subdiv.tag)
if (subdiv.tag == "head"):
for sentence in subdiv.findall('s'):
#print(sentence.attrib)
words_i, tags_i = [], []
for s in sentence:
# #print(s.attrib)
if (s.tag == "foreign"):
for words in s.findall('w'):
tags_i.append(words.attrib['ctag'])
words_i.append(words.text)
#print("%s/%s" % (words.text, words.attrib['ctag']), '', end=''),
# countTags(tags,words.attrib['ctag'])
# #print("\n")
if (s.tag == "w"):
tags_i.append(s.attrib['ctag'])
words_i.append(s.text)
#print("%s/%s" % (s.text, s.attrib['ctag']), '', end=''),
# countTags(tags,s.attrib['ctag'])
if len(words_i) > 4:
ALLTAG.append(tags_i)
ALLWORD.append(words_i)
#print('\n')
if (subdiv.tag == "p"):
for sentence in subdiv.findall('s'):
#print(sentence.attrib)
words_i, tags_i = [], []
for s in sentence:
if (s.tag == "foreign"):
for words in s.findall('w'):
tags_i.append(words.attrib['ctag'])
words_i.append(words.text)
#print("%s/%s" % (words.text, words.attrib['ctag']), '', end=''),
# countTags(tags,words.attrib['ctag'])
# #print("\n")
if (s.tag == "w"):
tags_i.append(s.attrib['ctag'])
words_i.append(s.text)
#print("%s/%s" % (s.text, s.attrib['ctag']), '', end=''),
# countTags(tags,s.attrib['ctag'])
if len(words_i) > 4:
ALLTAG.append(tags_i)
ALLWORD.append(words_i)
#print('\n')
return DataFrame({"words": ALLWORD,
"tags": ALLTAG})
if __name__ == '__main__':
# Parse Arguments.
parser = argparse.ArgumentParser(description="Parse the XML files of NNC project.")
parser.add_argument("--files_path", "-i", help="Path to the directory containing the XML files." )
parser.add_argument("--out_file_path", "-o", help="Path to the file to store the DataFrame. Must end with .gz" )
args = parser.parse_args()
# Note time.
start = time()
# Parse XML to a DF.
result = get_sent_tags(path = args.files_path)
# Save the DF to a compressed .gz file.
out_filename = args.out_file_path
#result.to_csv(out_filename, index = None, compression = 'gzip')
end = time()
print(f"Completed!\nFile saved as: {out_filename}.")
print(f"Time elapsed: {end - start : 5.2f} seconds.")