-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathqueryTagging.py
executable file
·184 lines (156 loc) · 7.46 KB
/
queryTagging.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2016 Jonathan Schultz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from argrecord import ArgumentHelper, ArgumentRecorder
import os
import sys
import argparse
from NVivoNorm import NVivoNorm
from sqlalchemy import *
import re
import csv
import shutil
def queryTagging(arglist=None):
parser = ArgumentRecorder(description="Query taggings in a normalised file.")
generalgroup = parser.add_argument_group('General')
generalgroup.add_argument( 'file', type=str,
help='Normalised NVivo (.nvpn) file')
generalgroup.add_argument('-o', '--outfile', type=str,
help='Output CSV file')
generalgroup.add_argument('-s', '--source', type=str)
generalgroup.add_argument('-sc', '--source-category', type=str)
generalgroup.add_argument('-n', '--node', nargs='*', type=str)
generalgroup.add_argument('-nc', '--node-category', type=str)
advancedgroup = parser.add_argument_group('Advanced')
advancedgroup.add_argument('-v', '--verbosity', type=int, default=1, private=True)
advancedgroup.add_argument('-l', '--limit', type=int, default=0,
help="Limit number of sources to process")
advancedgroup.add_argument('--no-comments', action='store_true',
help='Do not output comments in header of output file')
args = parser.parse_args(arglist)
try:
norm = NVivoNorm(args.file)
norm.begin()
sourcesel = select([
norm.Source.c.Name.label('Source'),
norm.Node.c.Name.label('Node'),
norm.Source.c.Content,
norm.Tagging.c.Fragment,
norm.Tagging.c.Memo
]).where(
norm.Source.c.Id == norm.Tagging.c.Source,
).select_from(
norm.Tagging.outerjoin(
norm.Node,
norm.Tagging.c.Node == norm.Node.c.Id)
)
params = {}
if args.source:
sourcesel = sourcesel.where(
norm.Source.c.Name == bindparam('Source')
)
params.update({'Source': args.source})
if args.source_category:
sourcesel = sourcesel.where(and_(
norm.Source.c.Category == norm.SourceCategory.c.Id,
norm.SourceCategory.c.Name == bindparam('SourceCategory')
))
params.update({'SourceCategory': args.source_category})
tagginglist = []
if args.node_category:
sourceselnodecat = sourcesel.where(and_(
norm.Node.c.Category == norm.NodeCategory.c.Id,
norm.NodeCategory.c.Name == bindparam('NodeCategory')
))
params.update({'NodeCategory': args.node_category})
tagginglist.append([dict(row) for row in norm.con.execute(sourceselnodecat, params)])
if args.node:
sourceselnode = sourcesel.where(and_(
norm.Tagging.c.Node == norm.Node.c.Id,
norm.Node.c.Name == bindparam('Node')
))
for nodeiter in args.node:
params.update({'Node': nodeiter})
tagginglist.append([dict(row) for row in norm.con.execute(sourceselnode, params)])
elif not args.node_category:
tagginglist = [[dict(row) for row in norm.con.execute(sourcesel, params)]]
fragmentregex = re.compile(r'(?P<start>[0-9]+):(?P<end>[0-9]+)')
for taggings in tagginglist:
for tagging in taggings:
matchfragment = fragmentregex.match(tagging['Fragment'])
tagging['Start'] = int(matchfragment.group('start'))
tagging['End'] = int(matchfragment.group('end'))
def sortandmergetagginglist(tagginglist):
tagginglist.sort(key = lambda tagging: (tagging['Source'], tagging['NodeTuple'], tagging['Start'], tagging['End']))
idx = 0
while idx < len(tagginglist) - 1:
if tagginglist[idx]['Source'] == tagginglist[idx+1]['Source'] \
and tagginglist[idx]['NodeTuple'] == tagginglist[idx+1]['NodeTuple'] \
and tagginglist[idx]['End'] >= tagginglist[idx+1]['Start']:
tagginglist[idx]['End'] = max(tagginglist[idx]['End'], tagginglist[idx+1]['End'])
del tagginglist[idx+1]
else:
idx += 1
intersection = tagginglist[0]
for tagging in intersection:
if tagging.get('Node'):
tagging['NodeTuple'] = (tagging['Node'],)
else:
tagging['NodeTuple'] = ()
sortandmergetagginglist(intersection)
for taggings in tagginglist[1:]:
idx = 0
newintersection = []
for intagging in intersection:
for tagging in taggings:
if tagging['Source'] == intagging['Source']:
newstart = max(tagging['Start'], intagging['Start'])
newend = min(tagging['End'], intagging['End'])
if newend >= newstart:
newintersection.append({'Source': tagging['Source'],
'NodeTuple': (tagging['Node'],) + intagging['NodeTuple'],
'Content': tagging['Content'],
'Start': newstart,
'End': newend})
intersection = newintersection
sortandmergetagginglist(intersection)
if args.outfile:
if os.path.exists(args.outfile):
shutil.move(args.outfile, args.outfile + '.bak')
csvfile = open(args.outfile, 'w')
else:
csvfile = sys.stdout
if not args.no_comments:
parser.write_comments(args, csvfile, incomments=ArgumentHelper.separator())
csvwriter = csv.DictWriter(csvfile,
fieldnames=['Source', 'Node', 'Memo', 'Text', 'Fragment'],
extrasaction='ignore',
lineterminator=os.linesep,
quoting=csv.QUOTE_NONNUMERIC)
csvwriter.writeheader()
for tagging in intersection:
tagging['Fragment'] = str(tagging['Start']) + ':' + str(tagging['End'])
tagging['Text'] = tagging['Content'][tagging['Start']-1:tagging['End']]
tagging['Node'] = os.linesep.join(nodeiter for nodeiter in tagging['NodeTuple'])
csvwriter.writerows(intersection)
csvfile.close()
except:
raise
finally:
del norm
if __name__ == '__main__':
queryTagging(None)