Commit 1c20b8416dcd6971bf8bc85d0667521ab3c91ad1

Authored by Erickson Silva
1 parent cb471491
Exists in devel

[TranslationServer] Corrige trie

Showing 2 changed files with 74 additions and 5 deletions   Show diff stats
src/TranslationServer.py 100644 → 100755
... ... @@ -16,6 +16,7 @@ RUN_MODE=None
16 16 DICT_VERSION=None
17 17 BUNDLES_PATH=None
18 18 BUNDLES_LIST={}
  19 +TRIE=None
19 20 conn=None
20 21 lock = Lock()
21 22 app=Flask(__name__, static_url_path="", static_folder="/var/www/")
... ... @@ -42,6 +43,7 @@ def dict_mode():
42 43 BUNDLES_PATH={"IOS":IOS_SIGNS_PATH, "ANDROID":ANDROID_SIGNS_PATH, "STANDALONE":STANDALONE_SIGNS_PATH, "WEBGL":WEBGL_SIGNS_PATH}
43 44 check_version()
44 45 list_bundles()
  46 + generate_trie()
45 47  
46 48 def connect_database():
47 49 import MySQLdb as mysql
... ... @@ -111,6 +113,11 @@ def list_files(path):
111 113 files.append(fname)
112 114 return files
113 115  
  116 +def generate_trie():
  117 + global TRIE
  118 + signs = list(BUNDLES_LIST["DEFAULT"])
  119 + TRIE = json.dumps(Trie.gen(signs))
  120 +
114 121 def check_platform_files():
115 122 android = set(list_files(BUNDLES_PATH["ANDROID"]))
116 123 ios = set(list_files(BUNDLES_PATH["IOS"]))
... ... @@ -267,7 +274,7 @@ def load_statistics_page():
267 274 return php_output
268 275  
269 276 @app.route("/update", methods=['GET'])
270   -def update_list_bundles():
  277 +def update():
271 278 try:
272 279 password = request.args.get('password').encode('utf-8')
273 280 except AttributeError:
... ... @@ -276,6 +283,7 @@ def update_list_bundles():
276 283 Popen('sudo -S <<< '+password+' apt-get update > /dev/null 2>&1', shell=True, stdin=None, stdout=None, stderr=None, executable="/bin/bash").wait()
277 284 check_version()
278 285 list_bundles()
  286 + generate_trie()
279 287 return "Successfully updated list.", 200
280 288  
281 289 @app.route("/version", methods=['GET'])
... ... @@ -284,10 +292,7 @@ def get_version():
284 292  
285 293 @app.route("/signs", methods=['GET'])
286 294 def get_signs_list():
287   - signs = list(BUNDLES_LIST["DEFAULT"])
288   - #return json.dumps(signs), 200
289   - trie = Trie.gen(signs)
290   - return json.dumps(trie), 200
  295 + return TRIE, 200
291 296  
292 297 @app.route("/<platform>/<sign>", methods=['GET'])
293 298 @check_run_mode
... ...
src/Trie.py 0 → 100755
... ... @@ -0,0 +1,64 @@
  1 +#!/usr/bin/python
  2 +# -*- coding: utf-8 -*-
  3 +
  4 +
  5 +class Node:
  6 + def __init__(self, end = False):
  7 + self.end = end
  8 + self.children = {}
  9 +
  10 +class Trie:
  11 + def __init__(self, characters):
  12 + self.characters = characters
  13 + self._parse_characters()
  14 + self.root = Node()
  15 +
  16 + def _parse_characters(self):
  17 + self.characters_keys = {}
  18 + for i in xrange(len(self.characters)):
  19 + self.characters_keys[self.characters[i]] = i
  20 +
  21 + def _key_of(self, item, i):
  22 + return self.characters_keys[item[i].encode('utf-8')]
  23 +
  24 + def _add(self, item):
  25 + node = self.root
  26 + item = list(item.decode('utf-8'))
  27 + for i in xrange(len(item)):
  28 + key = self._key_of(item, i)
  29 + if not node.children.has_key(key):
  30 + node.children[key] = Node()
  31 + node = node.children[key]
  32 + node.end = True
  33 +
  34 + def _to_json(self, node):
  35 + keys = [];
  36 + children = {}
  37 + for key, value in node.children.iteritems():
  38 + keys.append(key)
  39 + children[key] = self._to_json(value)
  40 + keys.sort()
  41 + return { 'end': node.end, 'keys': keys, 'children': children }
  42 +
  43 + def add(self, data):
  44 + if type(data) is list:
  45 + for d in data:
  46 + if not d in ignore:
  47 + self._add(d)
  48 + else:
  49 + self._add(data)
  50 +
  51 + def to_json(self):
  52 + return self._to_json(self.root)
  53 +
  54 +chars = ["'", '$', ',', '_', '%', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  55 + 'A', 'Á', 'Â', 'Ã', 'À', 'B', 'C', 'Ç', 'D', 'E', 'É', 'Ê', 'F', 'G', 'H',
  56 + 'I', 'Í', 'J', 'K', 'L', 'M', 'N', 'O', 'Ó', 'Ô', 'Õ', 'P', 'Q', 'R', 'S',
  57 + 'T', 'U', 'Ú', 'V', 'W', 'X', 'Y', 'Z']
  58 +
  59 +ignore = ["[INTERROGAÇÃO]", "[EXCLAMAÇÃO]", "[PONTO]"]
  60 +
  61 +def gen(data):
  62 + trie = Trie(chars)
  63 + trie.add(data)
  64 + return { 'characters': chars, 'keys': trie.characters_keys, 'trie': trie.to_json() }
0 65 \ No newline at end of file
... ...