Commit cc4de742076f2a578cb11669add67fcbd4da0fd1

Authored by Mateus Lustosa
1 parent dd84c126

Implements trie prefix getter and use in list manager

Assets/Scripts/Animation List/ListManager.cs
@@ -18,10 +18,12 @@ public class ItemData { @@ -18,10 +18,12 @@ public class ItemData {
18 18
19 public class ListManager : MonoBehaviour { 19 public class ListManager : MonoBehaviour {
20 20
  21 + private PlayerManager manager;
  22 +
21 public GameObject sampleItemObject; 23 public GameObject sampleItemObject;
22 public GameObject sampleLoadingItemObject; 24 public GameObject sampleLoadingItemObject;
23 25
24 - public string[] itemList; 26 + public List<string> itemList;
25 private int index = 0; 27 private int index = 0;
26 private const int OFFSET = 20; 28 private const int OFFSET = 20;
27 private int size = 0; 29 private int size = 0;
@@ -38,10 +40,9 @@ public class ListManager : MonoBehaviour { @@ -38,10 +40,9 @@ public class ListManager : MonoBehaviour {
38 40
39 private float itemHeight = 30F; 41 private float itemHeight = 30F;
40 42
41 - TrieST<string> trie;  
42 -  
43 void Start() 43 void Start()
44 { 44 {
  45 + this.manager = this.gameObject.GetComponent<PlayerManager>();
45 this.scrollView.onValueChanged.AddListener(checkScrollPosition); 46 this.scrollView.onValueChanged.AddListener(checkScrollPosition);
46 this.input.onValueChanged.AddListener(inputChanged); 47 this.input.onValueChanged.AddListener(inputChanged);
47 48
@@ -61,35 +62,11 @@ public class ListManager : MonoBehaviour { @@ -61,35 +62,11 @@ public class ListManager : MonoBehaviour {
61 62
62 public IEnumerator load() 63 public IEnumerator load()
63 { 64 {
64 - WWW www = new WWW("http://150.165.205.9/anims/sinais.txt");  
65 - yield return www;  
66 -  
67 - string[] lines = new string[0];  
68 -  
69 - if (string.IsNullOrEmpty(www.error))  
70 - {  
71 - lines = System.Text.Encoding.UTF8.GetString(www.bytes, 0, www.bytes.Length).Split('\n');  
72 - }  
73 - else  
74 - {  
75 - TextAsset file = Resources.Load("sinais") as TextAsset;  
76 - lines = System.Text.Encoding.UTF8.GetString(file.bytes, 0, file.bytes.Length).Split('\n');  
77 - // lines = file.text.Split("\n"[0]);  
78 - } 65 + while (manager.signs == null) yield return new WaitForEndOfFrame();
79 66
80 - this.trie = new TrieST<string>();  
81 -  
82 - foreach (string line in lines)  
83 - {  
84 - string word = line.Trim();  
85 -  
86 - if ( ! String.IsNullOrEmpty(word))  
87 - this.trie.put(word, word);  
88 - }  
89 -  
90 - this.itemList = getNamesByPrefix(""); 67 + this.itemList = this.manager.signs.StartsWith("");
91 this.index = 0; 68 this.index = 0;
92 - this.size = itemList.Length; 69 + this.size = itemList.Count;
93 70
94 populateList(); 71 populateList();
95 } 72 }
@@ -102,9 +79,9 @@ public class ListManager : MonoBehaviour { @@ -102,9 +79,9 @@ public class ListManager : MonoBehaviour {
102 79
103 public void inputChanged(string text) 80 public void inputChanged(string text)
104 { 81 {
105 - this.itemList = getNamesByPrefix(text.ToUpper()); 82 + this.itemList = this.manager.signs.StartsWith(text.ToUpper());
106 this.index = 0; 83 this.index = 0;
107 - this.size = itemList.Length; 84 + this.size = itemList.Count;
108 85
109 this.contentPanel.DetachChildren(); 86 this.contentPanel.DetachChildren();
110 foreach(GameObject go in GameObject.FindGameObjectsWithTag("clone")) 87 foreach(GameObject go in GameObject.FindGameObjectsWithTag("clone"))
@@ -145,10 +122,4 @@ public class ListManager : MonoBehaviour { @@ -145,10 +122,4 @@ public class ListManager : MonoBehaviour {
145 this.index = last; 122 this.index = last;
146 } 123 }
147 124
148 - private string[] getNamesByPrefix(string prefix)  
149 - {  
150 - Queue<string> names = this.trie.keysWithPrefix(prefix);  
151 - return names.ToArray();  
152 - }  
153 -  
154 } 125 }
Assets/Scripts/Player Manager/GenericPlayerManager.cs
@@ -49,8 +49,6 @@ public abstract class GenericPlayerManager : MonoBehaviour { @@ -49,8 +49,6 @@ public abstract class GenericPlayerManager : MonoBehaviour {
49 49
50 // Guarda os nomes das palavras já carregadas 50 // Guarda os nomes das palavras já carregadas
51 private HashSet<string> loadedAssetBundles = new HashSet<string>(); 51 private HashSet<string> loadedAssetBundles = new HashSet<string>();
52 - // Guarda os nomes das palavras que não tem assetbundle  
53 - private HashSet<string> nonexistentAssetBundles = new HashSet<string>();  
54 52
55 // Fila de animações para reprodução 53 // Fila de animações para reprodução
56 // Utilizada para alterar velocidade e apresentar a legenda 54 // Utilizada para alterar velocidade e apresentar a legenda
@@ -476,32 +474,17 @@ public abstract class GenericPlayerManager : MonoBehaviour { @@ -476,32 +474,17 @@ public abstract class GenericPlayerManager : MonoBehaviour {
476 474
477 Queue<ToPlay> toPlayQueue = new Queue<ToPlay>(); 475 Queue<ToPlay> toPlayQueue = new Queue<ToPlay>();
478 toPlayQueue.Enqueue(new ToPlay(Subtitle.TYPE_NONE, DEFAULT_ANIMATION, "", this)); 476 toPlayQueue.Enqueue(new ToPlay(Subtitle.TYPE_NONE, DEFAULT_ANIMATION, "", this));
479 -  
480 - /*WWW checkConnectionRequest = getCheckConnectionRequest();  
481 - bool connected = false;  
482 -  
483 - while ( ! checkConnectionRequest.isDone)  
484 - yield return checkConnectionRequest;  
485 -  
486 - if (checkConnectionRequest.responseHeaders.Count > 0)  
487 - {  
488 - PlayerLogger.Log(checkConnectionRequest.responseHeaders["STATUS"]);  
489 - connected = checkConnectionRequest.responseHeaders["STATUS"].Contains("404");  
490 - }  
491 - else PlayerLogger.Log("No response headers.");*/  
492 -  
493 - bool connected = true; 477 +
494 bool playingStarted = false; 478 bool playingStarted = false;
495 479
496 String[] stringPos = gloss.Split(' '); 480 String[] stringPos = gloss.Split(' ');
497 foreach (string aniName in stringPos) 481 foreach (string aniName in stringPos)
498 { 482 {
499 if (String.IsNullOrEmpty(aniName)) continue; 483 if (String.IsNullOrEmpty(aniName)) continue;
500 -  
501 - bool nonexistent = nonexistentAssetBundles.Contains(aniName); 484 +
502 bool loaded = loadedAssetBundles.Contains(aniName); 485 bool loaded = loadedAssetBundles.Contains(aniName);
503 486
504 - if ( ! nonexistent && ! loaded && connected) 487 + if ( ! loaded)
505 { 488 {
506 WWW bundleRequest = loadAssetBundle(aniName); 489 WWW bundleRequest = loadAssetBundle(aniName);
507 490
@@ -533,8 +516,6 @@ public abstract class GenericPlayerManager : MonoBehaviour { @@ -533,8 +516,6 @@ public abstract class GenericPlayerManager : MonoBehaviour {
533 else PlayerLogger.Log("GPM", "L", "Sign \"" + aniName + "\" wasn't loaded successfuly."); 516 else PlayerLogger.Log("GPM", "L", "Sign \"" + aniName + "\" wasn't loaded successfuly.");
534 } 517 }
535 else PlayerLogger.Log("GPM", "L", "Bundle \"" + aniName + "\" wasn't loaded successfuly."); 518 else PlayerLogger.Log("GPM", "L", "Bundle \"" + aniName + "\" wasn't loaded successfuly.");
536 -  
537 - if ( ! loaded) nonexistentAssetBundles.Add(aniName);  
538 } 519 }
539 else 520 else
540 { 521 {
Assets/Scripts/PlayerManager.cs
@@ -8,6 +8,7 @@ using System.Threading; @@ -8,6 +8,7 @@ using System.Threading;
8 using LAViD.Utils; 8 using LAViD.Utils;
9 using LAViD.Structures; 9 using LAViD.Structures;
10 using System.Diagnostics; 10 using System.Diagnostics;
  11 +using System.Collections.Generic;
11 12
12 public class PlayerManager : GenericPlayerManager { 13 public class PlayerManager : GenericPlayerManager {
13 14
@@ -23,8 +24,6 @@ public class PlayerManager : GenericPlayerManager { @@ -23,8 +24,6 @@ public class PlayerManager : GenericPlayerManager {
23 private const string SERVER_URL = "http://traducao.vlibras.gov.br/translate?text="; 24 private const string SERVER_URL = "http://traducao.vlibras.gov.br/translate?text=";
24 private const int VERSION = 1; 25 private const int VERSION = 1;
25 26
26 - private Trie signs;  
27 -  
28 public enum ERROR_STATUS_MESSAGE 27 public enum ERROR_STATUS_MESSAGE
29 { 28 {
30 INTERNET_CONNECTION_FAILURE, 29 INTERNET_CONNECTION_FAILURE,
@@ -46,7 +45,8 @@ public class PlayerManager : GenericPlayerManager { @@ -46,7 +45,8 @@ public class PlayerManager : GenericPlayerManager {
46 private string regionPath = ""; 45 private string regionPath = "";
47 private int regionHash = 1; 46 private int regionHash = 1;
48 47
49 - private static string dictionaryJSON; 48 + public Trie signs = null;
  49 +
50 50
51 public static string get_connection_status_message(ERROR_STATUS_MESSAGE msg) 51 public static string get_connection_status_message(ERROR_STATUS_MESSAGE msg)
52 { 52 {
@@ -67,15 +67,14 @@ public class PlayerManager : GenericPlayerManager { @@ -67,15 +67,14 @@ public class PlayerManager : GenericPlayerManager {
67 67
68 public override void Start() 68 public override void Start()
69 { 69 {
70 - base.setRandomAnimations(randomAnimationNames);  
71 - base.Start();  
72 -  
73 - voiceRecognizer = new VoiceRecognition();  
74 -  
75 #if UNITY_EDITOR 70 #if UNITY_EDITOR
76 Caching.CleanCache(); 71 Caching.CleanCache();
77 #endif 72 #endif
78 73
  74 + base.setRandomAnimations(randomAnimationNames);
  75 + base.Start();
  76 +
  77 + voiceRecognizer = new VoiceRecognition();
79 Screen.fullScreen = false; 78 Screen.fullScreen = false;
80 79
81 StartCoroutine(WaitForResponse( 80 StartCoroutine(WaitForResponse(
@@ -83,28 +82,39 @@ public class PlayerManager : GenericPlayerManager { @@ -83,28 +82,39 @@ public class PlayerManager : GenericPlayerManager {
83 // Success 82 // Success
84 delegate(WWW www) 83 delegate(WWW www)
85 { 84 {
86 - PlayerManager.dictionaryJSON = www.text; 85 + string response = www.text;
87 86
88 - /*new Thread( 87 + new Thread(
89 () => { 88 () => {
90 - Thread.CurrentThread.IsBackground = true;*/ 89 + Thread.CurrentThread.IsBackground = true;
91 90
92 Stopwatch watch = new Stopwatch(); 91 Stopwatch watch = new Stopwatch();
93 watch.Start(); 92 watch.Start();
94 93
95 try { 94 try {
96 - this.signs = Trie.FromJSON(new JSONParser(Regex.Unescape(PlayerManager.dictionaryJSON)));  
97 -  
98 - PlayerLogger.Log("PM", "S", "Found EU: " + this.signs.Contains("EU"));  
99 - PlayerLogger.Log("PM", "S", "Found EUE: " + this.signs.Contains("EUE")); 95 + PlayerLogger.Log("T", "FJ", "Starting trie parsing.");
  96 + this.signs = Trie.FromJSON(new JSONParser(Regex.Unescape(response)));
  97 +
  98 + char[] charactersSigns = new char[]
  99 + {
  100 + 'A', 'B', 'C', 'Ç', 'D', 'E', 'F', 'G', 'H', 'I',
  101 + 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
  102 + 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  103 + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
  104 + };
  105 +
  106 + foreach (char c in charactersSigns)
  107 + {
  108 + this.signs.Add(c.ToString());
  109 + }
100 } catch (Exception e) { 110 } catch (Exception e) {
101 PlayerLogger.Log("PM", "S", "Exception at trie JSON parser: " + e.ToString()); 111 PlayerLogger.Log("PM", "S", "Exception at trie JSON parser: " + e.ToString());
102 } 112 }
103 113
104 watch.Stop(); 114 watch.Stop();
105 PlayerLogger.Log("T", "FJ", "Parsed in " + watch.ElapsedMilliseconds + " ms."); 115 PlayerLogger.Log("T", "FJ", "Parsed in " + watch.ElapsedMilliseconds + " ms.");
106 - /* }  
107 - ).Start();*/ 116 + }
  117 + ).Start();
108 }, 118 },
109 // Error 119 // Error
110 delegate (WWW www) 120 delegate (WWW www)
@@ -166,6 +176,9 @@ public class PlayerManager : GenericPlayerManager { @@ -166,6 +176,9 @@ public class PlayerManager : GenericPlayerManager {
166 176
167 protected override WWW loadAssetBundle(string aniName) 177 protected override WWW loadAssetBundle(string aniName)
168 { 178 {
  179 + if (this.regionHash == 1 && !this.signs.Contains(aniName))
  180 + return null;
  181 +
169 string address = BASE_URL + this.regionPath + WWW.EscapeURL(aniName); 182 string address = BASE_URL + this.regionPath + WWW.EscapeURL(aniName);
170 183
171 PlayerLogger.Log("PM", "lAB", "Requesting bundle: " + address); 184 PlayerLogger.Log("PM", "lAB", "Requesting bundle: " + address);
Assets/Scripts/Trie/JSONParser.cs
@@ -6,7 +6,7 @@ using System.Text; @@ -6,7 +6,7 @@ using System.Text;
6 6
7 namespace LAViD.Utils 7 namespace LAViD.Utils
8 { 8 {
9 - class JSONParser 9 + public class JSONParser
10 { 10 {
11 private static readonly string True = "true"; 11 private static readonly string True = "true";
12 private static readonly string False = "false"; 12 private static readonly string False = "false";
@@ -250,7 +250,7 @@ namespace LAViD.Utils @@ -250,7 +250,7 @@ namespace LAViD.Utils
250 } 250 }
251 } 251 }
252 252
253 - class JSONObject<T> 253 + public class JSONObject<T>
254 { 254 {
255 public delegate T Creator(); 255 public delegate T Creator();
256 public delegate void Assigner(JSONParser json, T obj); 256 public delegate void Assigner(JSONParser json, T obj);
Assets/Scripts/Trie/Trie.cs
@@ -9,7 +9,7 @@ using System.Text.RegularExpressions; @@ -9,7 +9,7 @@ using System.Text.RegularExpressions;
9 9
10 namespace LAViD.Structures 10 namespace LAViD.Structures
11 { 11 {
12 - class Trie 12 + public class Trie
13 { 13 {
14 private char[] characters; 14 private char[] characters;
15 private Dictionary<char, int> keys; 15 private Dictionary<char, int> keys;
@@ -32,13 +32,25 @@ namespace LAViD.Structures @@ -32,13 +32,25 @@ namespace LAViD.Structures
32 obj.characters = jsonParser.ParseList<char>(char.Parse).ToArray(); 32 obj.characters = jsonParser.ParseList<char>(char.Parse).ToArray();
33 }); 33 });
34 34
35 - public static Trie FromJSON(JSONParser json) 35 + public static Trie FromJSON(JSONParser json) {
  36 + return Trie.Parser.Parse(json);
  37 + }
  38 +
  39 + public void Add(string word)
36 { 40 {
37 - PlayerLogger.Log("T", "FJ", "Starting trie parsing."); 41 + Node node = this.root;
38 42
39 - Trie trie = Trie.Parser.Parse(json);  
40 -  
41 - return trie; 43 + foreach (char c in word)
  44 + {
  45 + int key = this.keys[c];
  46 +
  47 + if (node.children[key] == null)
  48 + node.children[key] = new Node(this.root.maxChildren);
  49 +
  50 + node = node.children[key];
  51 + }
  52 +
  53 + node.end = true;
42 } 54 }
43 55
44 public bool Contains(string word) 56 public bool Contains(string word)
@@ -57,6 +69,8 @@ namespace LAViD.Structures @@ -57,6 +69,8 @@ namespace LAViD.Structures
57 69
58 public List<string> StartsWith(string word) 70 public List<string> StartsWith(string word)
59 { 71 {
  72 + PlayerLogger.Log("T", "SW", "Searching for '" + word + "'.");
  73 +
60 List<string> list = new List<string>(); 74 List<string> list = new List<string>();
61 Node node = this.root; 75 Node node = this.root;
62 76
@@ -66,7 +80,7 @@ namespace LAViD.Structures @@ -66,7 +80,7 @@ namespace LAViD.Structures
66 if (node == null) break; 80 if (node == null) break;
67 } 81 }
68 82
69 - if (node != null) feed(list, "", node); 83 + if (node != null) feed(list, word, node);
70 84
71 return list; 85 return list;
72 } 86 }
@@ -89,6 +103,7 @@ namespace LAViD.Structures @@ -89,6 +103,7 @@ namespace LAViD.Structures
89 103
90 public Node(int maxChildren) { 104 public Node(int maxChildren) {
91 this.maxChildren = maxChildren; 105 this.maxChildren = maxChildren;
  106 + this.children = new Node[maxChildren];
92 } 107 }
93 108
94 private readonly static JSONObject<Node> Parser = new JSONObject<Node>() 109 private readonly static JSONObject<Node> Parser = new JSONObject<Node>()
@@ -108,9 +123,7 @@ namespace LAViD.Structures @@ -108,9 +123,7 @@ namespace LAViD.Structures
108 return Node.FromJSON(innerJSONParser, obj.maxChildren); 123 return Node.FromJSON(innerJSONParser, obj.maxChildren);
109 } 124 }
110 ); 125 );
111 -  
112 - obj.children = new Node[obj.maxChildren];  
113 - 126 +
114 foreach (KeyValuePair<int, Node> pair in children) 127 foreach (KeyValuePair<int, Node> pair in children)
115 obj.children[pair.Key] = pair.Value; 128 obj.children[pair.Key] = pair.Value;
116 }); 129 });