-
Notifications
You must be signed in to change notification settings - Fork 11
/
extractTagging.py
executable file
·93 lines (75 loc) · 3.06 KB
/
extractTagging.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
#!/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 __future__ import print_function
import os
import sys
import argparse
from sqlalchemy import *
from sqlalchemy import exc
import re
exec(open(os.path.dirname(os.path.realpath(__file__)) + os.path.sep + 'DataTypes.py').read())
parser = argparse.ArgumentParser(description='Extract tagging from normalised file.')
parser.add_argument('-v', '--verbosity', type=int, default=1)
parser.add_argument('-l', '--limit', type=int, default=0,
help="Limit number of sources to process")
parser.add_argument('-n', '--node', type=str, nargs='?',
help="Name of node")
parser.add_argument('-s', '--source', type=str, nargs='?',
help="Name of source")
parser.add_argument('infile', type=str,
help="Normalised project file to analyse")
args = parser.parse_args()
from textblob import TextBlob
try:
normdb = create_engine('sqlite:///' + args.infile)
normmd = MetaData(bind=normdb)
normNode = Table('Node', normmd, autoload=True)
normSource = Table('Source', normmd, autoload=True)
normTagging = Table('Tagging', normmd, autoload=True)
sel = select([
normTagging.c.Fragment,
normNode.c.Name.label('NodeName'),
normSource.c.Name.label('SourceName'),
normSource.c.Content
]).where(and_(
normTagging.c.Node == normNode.c.Id,
normSource.c.Id == normTagging.c.Source
))
if args.node is not None:
sel = sel.where(
normNode.c.Name == bindparam('NodeName')
)
if args.source is not None:
sel = sel.where(
normSource.c.Name == bindparam('SourceName')
)
taggings = normdb.execute(sel, {
'NodeName': args.node,
'SourceName': args.source
})
for tagging in taggings:
print("Node: " + tagging['NodeName'] + " Source: " + tagging['SourceName'] + "[" + tagging['Fragment'] + "]", file=sys.stderr)
matchfragment = re.match("([0-9]+):([0-9]+)(?:,([0-9]+)(?::([0-9]+))?)?", tagging['Fragment'])
if matchfragment is None:
print("WARNING: Unrecognised tagging fragment", file=sys.stderr)
else:
print(tagging['Content'][int(matchfragment.group(1)):int(matchfragment.group(2))+1], file=sys.stderr)
print("", file=sys.stderr)
normdb.dispose()
except:
raise