-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathperson.py
202 lines (171 loc) · 5.36 KB
/
person.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# -*- coding: utf-8 -*-
from google.appengine.ext import ndb
import sentAnno
import logging
from bitmap import BitMap
MARKDOWN_OPTIONS = {
'bold':'*',
'italic':'_',
'fixed-width':'`',
}
def containsMarkdownSymbols(text):
for key, markdownChar in MARKDOWN_OPTIONS.iteritems():
if markdownChar in text:
return True
return False
class Person(ndb.Model):
chat_id = ndb.IntegerProperty()
state = ndb.IntegerProperty()
last_mod = ndb.DateTimeProperty(auto_now=True)
last_state = ndb.IntegerProperty()
name = ndb.StringProperty()
last_name = ndb.StringProperty()
username = ndb.StringProperty()
language = ndb.StringProperty() #current language
enabled = ndb.BooleanProperty(default=True)
languageSentenceIndex = ndb.PickleProperty(default={}) # for each language current sentence index [zero based]
languageProgress = ndb.PickleProperty(default={}) # for each langauge number of annotated sentences
selectedTokens = ndb.BooleanProperty(repeated=True)
highlightingMode = ndb.StringProperty(default='fixed-width')
maxCharsPerLine = ndb.IntegerProperty(default=22)
selectedMwes = ndb.PickleProperty()
currentMweTmp = ndb.IntegerProperty(repeated=True)
def updateUsername(p, username, put=False):
if (p.username!=username):
p.username = username
if put:
p.put()
def addPerson(chat_id, name, last_name, username):
p = Person(
id = str(chat_id),
name = name,
chat_id = chat_id,
last_name = last_name,
username = username,
)
initLangaugeCounters(p, False)
p.put()
return p
def reInitializePerson(p, chat_id, name, last_name, username):
p.populate(
name = name,
chat_id = chat_id,
last_name = last_name,
username = username,
enabled = True
)
p.put()
def initLangaugeCounters(p, put=True):
for lang in sentAnno.getLanguages():
p.languageSentenceIndex[lang] = 0
size = sentAnno.totalSentLang(lang)
p.languageProgress[lang] = BitMap(size)
if put:
p.put()
def getCurrentSentenceIndex(p):
return p.languageSentenceIndex[p.language]
def getLanguageProgress(p, lang=None):
if not lang:
lang = p.language
return p.languageProgress[lang].count()
def setCurrentSentenceAnnotated(p, put=False):
index = p.languageSentenceIndex[p.language]
p.languageProgress[p.language].set(index)
if put:
p.put()
def getSelectedTokensIndexTuple(p):
result = []
for i in range(0,len(p.selectedTokens)):
if p.selectedTokens[i]:
result.append(i)
return tuple(result)
def setSelectedTokenFromIndexList(p, indexes):
size = len(p.selectedTokens)
p.selectedTokens = [False] * size
for i in range(0,size):
if i in indexes:
p.selectedTokens[i] = True
def goToSentece(p, n, put=False):
#p.currentSentence = n
p.languageSentenceIndex[p.language] = n
if put:
p.put()
def goToNextSentence(p, put=False):
goToSentece(p, p.languageSentenceIndex[p.language]+1, put)
def goToPrevSentence(p, put=False):
goToSentece(p, p.languageSentenceIndex[p.language]-1, put)
def hasNextSentece(p):
#logging.debug("current: " + str(getCurrentSentenceIndex(p)))
#logging.debug("last: " + str(sentenceAnnotation.totalSentences(p) - 1))
return getCurrentSentenceIndex(p)<(sentAnno.totalSentPersonCurrentLang(p) - 1)
def hasPreviousSentece(p):
return getCurrentSentenceIndex(p)>0
def getNextNonAnnSentIndex(p):
bm = p.languageProgress[p.language]
start = getCurrentSentenceIndex(p) + 1
end = sentAnno.totalSentPersonCurrentLang(p)
for index in range(start,end):
if not bm.test(index):
return index
return None
def getPrevNonAnnSentIndex(p):
bm = p.languageProgress[p.language]
start = 0
end = getCurrentSentenceIndex(p)
for index in reversed(range(start,end)):
if not bm.test(index):
return index
return None
def enable_user(p):
p.enabled = True
p.put()
def setState(p, newstate, put=True):
p.last_state = p.state
p.state = newstate
if put:
p.put()
def setLanguage(p, language, put=False):
p.language = language
if put:
p.put()
def getMarkdownChar(p):
return MARKDOWN_OPTIONS[p.highlightingMode]
def setHighlightingMode(p, mode, put=False):
p.highlightingMode = mode
if put:
p.put()
def setMaxCharsPerLine(p, maxCharsPerLine, put=False):
p.maxCharsPerLine = maxCharsPerLine
if put:
p.put()
def getHighlightedString(p, text):
markdownChar = getMarkdownChar(p)
return markdownChar + text + markdownChar
def importSelectedMwe(p, put=False):
p.selectedMwes = sentAnno.getMwes(p)
#logging.debug("original MWE: " + str(sentenceAnnotation.getMwes(p)))
#logging.debug("imported MWE: " + str(p.selectedMwes))
#logging.debug("sentence index: " + str(getCurrentSentenceIndex() + 1))
if put:
p.put()
'''
def appendMwe(p, mweLabel, put=False):
if not p.selectedMwes:
p.selectedMwes = {}
indexTuple = getSelectedTokensIndexTuple(p)
p.selectedMwes[indexTuple] = mweLabel
if put:
p.put()
'''
'''
def removeMwe(p, indexArray, put=False):
del p.selectedMwes[indexArray]
if put:
p.put()
'''
'''
def setEmptyMwes(p, put=False):
p.selectedMwes = {}
if put:
p.put()
'''