forked from endpnt/andoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection.py
More file actions
143 lines (125 loc) · 4.95 KB
/
selection.py
File metadata and controls
143 lines (125 loc) · 4.95 KB
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
import re, string
from urlparse import urlsplit
NEXT_TXT_SELECTION = 'next.doc.%s.text.selection'
TXT_SELECTIONS = 'doc:%s:text.selections'
TXT_SELECTION_START = 'doc:%s:text.selection:%s:start'
TXT_SELECTION_END = 'doc:%s:text.selection:%s:end'
TXT_SELECTION_REF = 'doc:%s:text.selection:%s:ref'
NEXT_HTML_SELECTION = 'next.doc.%s.html.selection'
HTML_SELECTIONS = 'doc:%s:html.selections'
HTML_SELECTION_NODE = 'doc:%s:html.selection:%s:node'
HTML_SELECTION_START = 'doc:%s:html.selection:%s:start'
HTML_SELECTION_END = 'doc:%s:html.selection:%s:end'
HTML_SELECTION_REF = 'doc:%s:html.selection:%s:ref'
NAMESPACE = { 'http://www.w3.org/1999/xhtml/#h1': 'h1',
'http://www.w3.org/1999/xhtml/#h2': 'h2',
'http://www.w3.org/1999/xhtml/#h3': 'h3',
'http://www.w3.org/1999/xhtml/#h4': 'h4',
'http://www.w3.org/1999/xhtml/#p': 'p',
'http://www.w3.org/1999/xhtml/#ul': 'ul',
'http://www.w3.org/1999/xhtml/#div': 'div',
'http://www.w3.org/1999/xhtml/#li': 'li',
'http://www.w3.org/1999/xhtml/#span':'span' }
MATCH_NODE = r'^(%s)\.s([0-9]+)e([0-9]+)$' % string.join(NAMESPACE.values(), '|')
class TextSelection(object):
def __init__(self, docid = None, start = None, end = None, ref = None):
self.docid = int(docid)
self.start = start
self.end = end
self.ref = ref
def _valid(self):
# TODO
return True
def save(self, redis):
if self._valid():
selection_id = redis.incr(NEXT_TXT_SELECTION % self.docid)
pipe = redis.pipeline()
pipe.set(TXT_SELECTION_START % \
(self.docid, selection_id), self.start)
pipe.set(TXT_SELECTION_END % \
(self.docid, selection_id), self.end)
pipe.set(TXT_SELECTION_REF % \
(self.docid, selection_id), self.ref)
pipe.lpush(TXT_SELECTIONS % \
self.docid, selection_id)
result = pipe.execute()
print result
return selection_id
else:
return False
def from_url(self, url):
scheme, host, path, query, param = urlsplit(url)
p = re.compile(MATCH_NODE)
m = re.search(p, param)
if m is not None:
self.rel = m.group(1)
self.start = int(m.group(2))
self.end = int(m.group(3))
class HtmlSelection(object):
def __init__(self, docid = None, node = None,
start = None, end = None, ref = None):
self.docid = docid
self.node = node
self.start = start
self.end = end
self.ref = ref
def _valid(self):
#TODO
return True
def save(self, redis):
if self._valid():
selection_id = redis.incr(NEXT_HTML_SELECTION % self.docid)
pipe = redis.pipeline()
pipe.set(HTML_SELECTION_NODE % \
(self.docid, selection_id), self.node)
pipe.set(HTML_SELECTION_START % \
(self.docid, selection_id), self.start)
pipe.set(HTML_SELECTION_END % \
(self.docid, selection_id), self.end)
pipe.set(HTML_SELECTION_REF % \
(self.docid, selection_id), self.ref)
pipe.lpush(HTML_SELECTIONS % \
self.docid, selection_id)
result = pipe.execute()
print result
return selection_id
else:
return False
return True
class TextSelections():
def __init__(self, redis):
self._redis = redis
pass
def from_document_id(self, docid):
result = []
selections = self._redis.lrange(TXT_SELECTIONS % docid, 0, -1)
if len(selections) == 0:
return result
pipe = self._redis.pipeline()
for selection_id in selections:
pipe.get(TXT_SELECTION_START % (docid, selection_id))
pipe.get(TXT_SELECTION_END % (docid, selection_id))
pipe.get(TXT_SELECTION_REF % (docid, selection_id))
start,end,ref = pipe.execute()
t = TextSelection(docid, int(start), int(end), ref)
result.append(t)
return result
class HtmlSelections():
def __init__(self, redis):
self._redis = redis
pass
def from_document_id(self, docid):
result = []
selections = self._redis.lrange(HTML_SELECTIONS % docid, 0, -1)
if len(selections) == 0:
return result
pipe = self._redis.pipeline()
for selection_id in selections:
pipe.get(HTML_SELECTION_NODE % (docid, selection_id))
pipe.get(HTML_SELECTION_START % (docid, selection_id))
pipe.get(HTML_SELECTION_END % (docid, selection_id))
pipe.get(HTML_SELECTION_REF % (docid, selection_id))
node,start,end,ref = pipe.execute()
t = HtmlSelection(docid, node, int(start), int(end), ref)
result.append(t)
return result