-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetdata.py
183 lines (128 loc) · 6.19 KB
/
getdata.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
#!/usr/bin/env python
#
# getdata.py
#
# Copyright 2017 Alicia González Martínez
# COBHUNI Project, Universität Hamburg
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# TODO:
# ----
# * check correctness of original|commentary types of text in docs with only one text
#
#######################################################################################
import re
import ujson as json
import util
from util import CorpusDoc
#FIXME not sure if this will be included in the worflow in the end
def getdata_altafsir_annotated(cfg):
""" Read each input file and yield data.
Args:
cfg (namedtuple): configuration data.
Yields:
str, CorpusDoc: filename and data.
"""
# get all selected altafsir files from sources
sourcesfnames = util.getfiles(cfg.get('altafsir', 'prepared path'))
# get {filename : { hadith : [{ini:int, end:int}, ...], aya = [...], verse = [...] }}
sourcesdata = {fn : json.load(open(fpath))[cfg.get('annotation', 'annotations key')] for fpath, fn in sourcesfnames}
data = ((fn, json.load(open(fpath))) for fpath, fn in util.getfiles(cfg.get('altafsir', 'annotated path')))
for fname, fobj in data:
doc = CorpusDoc()
doc.text = fobj[cfg.get('annotation', 'text key input')]
annotation = {cfg.get('annotation', 'persons key') : fobj[cfg.get('annotation', 'persons key')],
cfg.get('annotation', 'motives key') : fobj[cfg.get('annotation', 'motives key')],
cfg.get('annotation', 'metamotives key') : fobj[cfg.get('annotation', 'metamotives key')]}
annotation.update(sourcesdata[fname])
doc.annotation = annotation
yield fname, doc
def getdata_altafsir_complete(cfg):
""" Read each input file and yield data.
Args:
cfg (namedtuple): configuration data.
Yields:
str, CorpusDoc: filename and data.
"""
# get all selected altafsir files from sources
sourcesfnames = util.getfiles(cfg.get('altafsir', 'sources path'))
data = {fn : json.load(open(fpath)) for fpath, fn in sourcesfnames}
for fname, fobj in data.items():
text_key = cfg.get('annotation', 'text key input') if cfg.get('annotation', 'text key input') in fobj \
else cfg.get('annotation', 'text key output')
yield fname, CorpusDoc(fobj[text_key], fobj[cfg.get('annotation', 'annotations key')])
#DEBUG
def getdata_test(cfg):
""" Read each input file and yield data.
Args:
cfg (namedtuple): configuration data.
Yields:
str, CorpusDoc: filename and data.
"""
# get all selected altafsir files from sources
sourcesfnames = util.getfiles(cfg.get('test', 'sources path'))
data = {fn : json.load(open(fpath)) for fpath, fn in sourcesfnames}
for fname, fobj in data.items():
text_key = cfg.get('annotation', 'text key input') if cfg.get('annotation', 'text key input') in fobj \
else cfg.get('annotation', 'text key output')
yield fname, CorpusDoc(fobj[text_key], fobj[cfg.get('annotation', 'annotations key')])
#FIXME not sure if this will be included in the worflow in the end
#def getdata_hadith_annotated():
# return None
def getdata_hadith_complete(cfg):
""" Read each input file and yield data.
Args:
cfg (namedtuple): configuration data.
Yields:
str, namedtuple, CorpusDoc, str: filename, ids of metadata, data and (type of) text key.
The metadata contains 6 attributes: PIDSTART(str), PIDEND(str) BOOKID(str), CHAPTERID(int),
SUBCHAPTERID(int), SECTIONID(int).
"""
fnames = ((fpath, util.parse_hadith_fname(fn), fn)for fpath,fn in util.getfiles(cfg.get('hadith', 'sources path')))
for fpath, fmeta, fname in fnames:
# get only files corresponding to commentaries
if re.match(r'^3[3-9]$', fmeta.BOOKID):
jsonObj = json.load(open(fpath))
if cfg.get('annotation', 'hadith original key') in jsonObj:
yield fname, fmeta, CorpusDoc(jsonObj[cfg.get('annotation', 'hadith original key')], {}), \
cfg.get('annotation', 'hadith original key')
if cfg.get('annotation', 'hadith commentary key') in jsonObj:
yield fname, fmeta, CorpusDoc(jsonObj[cfg.get('annotation', 'hadith commentary key')], {}), \
cfg.get('annotation', 'hadith commentary key')
#if len(jsonObj)==1: #DEBUG
#
# if cfg.get('annotation', 'hadith original key') in jsonObj: #DEBUG
# print(cfg.get('annotation', 'hadith original key'), fmeta.BOOKID, fname) #DEBUG
#
# elif cfg.get('annotation', 'hadith commentary key') in jsonObj: #DEBUG
# print(cfg.get('annotation', 'hadith original key'), fmeta.BOOKID, fname) #DEBUG
#
# else:
# print('0 0 0') #DEBUG
def getdata_ocred(cfg):
""" Read each input file and yield data.
Args:
cfg (namedtuple): configuration data.
Yields:
str, CorpusDoc: filename and data.
"""
sourcesfnames = util.getfiles(cfg.get('ocred', 'annotated path'))
data = {fn : json.load(open(fpath)) for fpath, fn in sourcesfnames}
for fname, fobj in data.items():
doc = CorpusDoc()
doc.text = fobj[cfg.get('annotation', 'text key input')]
doc.annotation = {cfg.get('annotation', 'persons key') : fobj[cfg.get('annotation', 'persons key')],
cfg.get('annotation', 'motives key') : fobj[cfg.get('annotation', 'motives key')],
cfg.get('annotation', 'metamotives key') : fobj[cfg.get('annotation', 'metamotives key')]}
yield fname, doc