-
Notifications
You must be signed in to change notification settings - Fork 2
/
ParseXMLCorpus.py
102 lines (95 loc) · 5.25 KB
/
ParseXMLCorpus.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
import os
import xml.etree.ElementTree as ET
path = 'nnc_updated_ah\\gc\\books\\'
filelist = os.listdir(path)
# for file in filelist:
# filepath = os.path.join(path, file)
# print(filepath)
tags = []
for file in filelist:
filepath = os.path.join(path, file)
print(filepath)
tree = ET.parse(filepath)
root = tree.getroot()
for data in root.findall('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)
for s in sentence:
# print(s.attrib)
if (s.tag == "foreign"):
for words in s.findall('w'):
tags.append(words.attrib['ctag'])
print("%s/%s" % (words.text, words.attrib['ctag']), '', end=''),
# print("\n")
if (s.tag == "w"):
tags.append(s.attrib['ctag'])
print("%s/%s" % (s.text, s.attrib['ctag']), '', end=''),
# countTags(tags,s.attrib['ctag'])
print('\n')
if (subdiv.tag == "p"):
for sentence in subdiv.findall('s'):
print(sentence.attrib)
for s in sentence:
if (s.tag == "foreign"):
for words in s.findall('w'):
tags.append(words.attrib['ctag'])
print("%s/%s" % (words.text, words.attrib['ctag']), '', end=''),
# countTags(tags,words.attrib['ctag'])
# print("\n")
if (s.tag == "w"):
tags.append(s.attrib['ctag'])
print("%s/%s" % (s.text, s.attrib['ctag']), '', end=''),
# countTags(tags,s.attrib['ctag'])
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)
for s in sentence:
# print(s.attrib)
if (s.tag == "foreign"):
for words in s.findall('w'):
tags.append(words.attrib['ctag'])
print("%s/%s" % (words.text, words.attrib['ctag']), '', end=''),
# countTags(tags,words.attrib['ctag'])
# print("\n")
if (s.tag == "w"):
tags.append(s.attrib['ctag'])
print("%s/%s" % (s.text, s.attrib['ctag']), '', end=''),
# countTags(tags,s.attrib['ctag'])
print('\n')
if (subdiv.tag == "p"):
for sentence in subdiv.findall('s'):
print(sentence.attrib)
for s in sentence:
if (s.tag == "foreign"):
for words in s.findall('w'):
tags.append(words.attrib['ctag'])
print("%s/%s" % (words.text, words.attrib['ctag']), '', end=''),
# countTags(tags,words.attrib['ctag'])
# print("\n")
if (s.tag == "w"):
tags.append(s.attrib['ctag'])
print("%s/%s" % (s.text, s.attrib['ctag']), '', end=''),
# countTags(tags,s.attrib['ctag'])
print('\n')
#
# print(tags)
# output = set()
# for x in tags:
# output.add(x)
# print("Number of tags:", len(output))
# print(output)