-
Notifications
You must be signed in to change notification settings - Fork 0
/
costituzione.py
165 lines (140 loc) · 5.3 KB
/
costituzione.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
# -*- coding: utf-8 -*-
from google.appengine.ext import ndb
import csv
import re
COSTITUZIONE_ICON = '📜'
class CostituzioneSentence(ndb.Model):
#id = sentence number
chapter = ndb.IntegerProperty()
article = ndb.IntegerProperty()
sentence = ndb.IntegerProperty()
word_text = ndb.StringProperty()
emoji_text = ndb.StringProperty()
def getEmojiText(self):
return self.emoji_text.encode('utf-8')
def getWordText(self):
return self.word_text.encode('utf-8')
def getSentenceUniqueId(chapter, article, sentence):
return "{}:{}:{}".format(chapter, article, sentence)
def addSentence(chapter, article, sentence, word_text, emoji_text, put=False):
cs = CostituzioneSentence(
id=getSentenceUniqueId(chapter, article, sentence),
chapter = chapter,
article = article,
sentence = sentence,
word_text = word_text,
emoji_text = emoji_text
)
if put:
cs.put()
return cs
def getSentenceByUniqueId(id):
return CostituzioneSentence.get_by_id(id)
def splitUniqueId(idString):
chapter, article, sentence = idString.split(':')
return int(chapter), int(article), int(sentence)
def getSentenceEmojiString(id):
cs = CostituzioneSentence.get_by_id(id)
header = "{} Art. {}.{}".format(COSTITUZIONE_ICON,cs.article,cs.sentence)
return "{}\n\n{}\n\n{}".format(header, cs.getWordText(), cs.getEmojiText())
#return "{}\n\n{}".format(cs.getWordText(), cs.getEmojiText())
def getNextSentenceId(id):
if id == "0:0:0":
return "1:1:1"
chapter, article, sentence = splitUniqueId(id)
new_id = getSentenceUniqueId(chapter, article, sentence + 1)
entry = getSentenceByUniqueId(new_id)
if entry == None:
new_id = getSentenceUniqueId(chapter, article+1, 1)
entry = getSentenceByUniqueId(new_id)
if entry != None:
return entry.key.id()
return None
def getPrevSentenceId(id):
if id == "1:1:1":
return "0:0:0"
chapter, article, sentence = splitUniqueId(id)
new_id = getSentenceUniqueId(chapter, article, sentence - 1)
entry = getSentenceByUniqueId(new_id)
if entry == None or entry.sentence==0:
entry = getLastSentenceInChapterArticle(chapter, article-1)
if entry != None:
return entry.key.id()
return None
def getLastSentenceInChapterArticle(chapter,article):
return CostituzioneSentence.query(
CostituzioneSentence.chapter == chapter,
CostituzioneSentence.article == article,
).order(-CostituzioneSentence.sentence).get()
# ================================
# import functions
# ================================
COSTITUZIONE_DOC_KEY = '1HFE12mO2CsBQOxg-XG2IKUTnT69B3bgy7PwqqJ8n7SQ'
GDOC_TSV_BASE_URL = "https://docs.google.com/spreadsheets/d/{0}/export?format=tsv&gid=0".format(COSTITUZIONE_DOC_KEY)
def getCostituzioneFromGdoc():
import requests
result = []
url = GDOC_TSV_BASE_URL
r = requests.get(url)
spreadSheetTsv = r.content.split('\n')
spreadSheetReader = csv.reader(spreadSheetTsv, delimiter='\t', quoting=csv.QUOTE_NONE)
for row in spreadSheetReader:
chapter = int(row[0])
article = int(row[1])
sentence = int(row[2])
words_sentence = row[3]
emojis_sentence = row[4]
tuple = (chapter, article, sentence, words_sentence, emojis_sentence)
result.append(tuple)
return result
def populateSentences(put=True, write_to_file = False):
from pinocchio import formatEmojiText
tuples = getCostituzioneFromGdoc()
to_add = []
file_out = open('EmojiData/Costituzione.txt', 'w')
for t in tuples:
chapter, article, sentence, words_sentence, emojis_sentence = t
emojis_sentence = formatEmojiText(emojis_sentence, ch_num=article, line_num=sentence)
ps = addSentence(chapter, article, sentence, words_sentence, emojis_sentence, put=False)
to_add.append(ps)
if write_to_file:
file_out.write(emojis_sentence + "\n")
if put:
ndb.put_multi(to_add)
return "Successfully added {} sentences.".format(len(tuples))
def deleteAllSentences():
delete_futures = ndb.delete_multi_async(
CostituzioneSentence.query().fetch(keys_only=True)
)
ndb.Future.wait_all(delete_futures)
# to be called as print(costituzione.checkNormalization())
def checkNormalization():
exceptions = []
tuples = getCostituzioneFromGdoc()
for l, line in enumerate(tuples, start=1):
emojiLine = line[4]
try:
splitEmojiLine(emojiLine)
except Exception as error:
msg = str(error) + " in line " + str(l)
print msg
#if len(exceptions)==10:
# return '\n'.join(exceptions)
if exceptions:
return '\n'.join(exceptions)
#return exceptions
print "All OK!"
def splitEmojiLine(emojiLine):
import emojiUtil
result = []
emoji_punct_list = [x.strip() for x in re.split("([\s_,;.:!?\"'])",emojiLine) if x.strip() not in ['']]
for i, e in enumerate(emoji_punct_list):
if e in ['_',"'",'.',',',';',':']:
result.append(e)
else:
eList = emojiUtil.splitEmojis(e)
if eList:
result.extend(eList)
else:
raise Exception('Problem in splitting {} in position {}'.format(e, i))
return result