-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathWordList.cs
More file actions
239 lines (228 loc) · 9.42 KB
/
WordList.cs
File metadata and controls
239 lines (228 loc) · 9.42 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Wrj
{
public class WordList : MonoBehaviour
{
private HashSet<string> wordSet;
private HashSet<string> fullWordSet;
public enum WordSource { Full, Common6000, Common3000, Common1000 }
private WordSource wordSource = WordSource.Full;
private static WordList _instance;
public static WordList Instance
{
get
{
if (_instance != null) return _instance;
_instance = FindFirstObjectByType<WordList>();
if (_instance != null) return _instance;
if (!Application.isPlaying)
{
return null;
}
GameObject go = new GameObject();
go.name = "WordList";
_instance = go.AddComponent<WordList>();
return _instance;
}
}
public HashSet<string> WordSet
{
get
{
if (wordSource == WordSource.Full)
{
return fullWordSet;
}
return wordSet;
}
}
private void Awake()
{
if (_instance == null)
{
_instance = this;
Init(wordSource);
}
}
public void Init(TextAsset wordListTextAsset)
{
if (wordListTextAsset == null) return;
wordSet = ParseWordSet(wordListTextAsset);
TextAsset fullDictionaryTextAsset = Resources.Load("WordList", typeof(TextAsset)) as TextAsset;
if (fullDictionaryTextAsset != null)
{
fullWordSet = ParseWordSet(fullDictionaryTextAsset);
}
}
public void Init(TextAsset wordListTextAsset, TextAsset fullDictionaryTextAsset)
{
if (wordListTextAsset == null || fullDictionaryTextAsset == null) return;
wordSet = ParseWordSet(wordListTextAsset);
fullWordSet = ParseWordSet(fullDictionaryTextAsset);
}
public void Init(WordSource wordSource)
{
string strWordResourceName = "WordList";
switch (wordSource)
{
case WordSource.Common6000:
strWordResourceName = "WordListTop6000";
break;
case WordSource.Common3000:
strWordResourceName = "WordListTop3000";
break;
case WordSource.Common1000:
strWordResourceName = "WordListTop1000";
break;
default:
strWordResourceName = "WordList";
break;
}
TextAsset fullDictionaryTextAsset = Resources.Load("WordList", typeof(TextAsset)) as TextAsset;
if (fullDictionaryTextAsset != null)
{
fullWordSet = ParseWordSet(fullDictionaryTextAsset);
}
if (wordSource != WordSource.Full)
{
TextAsset wordListTextAsset = Resources.Load(strWordResourceName, typeof(TextAsset)) as TextAsset;
if (wordListTextAsset != null)
{
wordSet = ParseWordSet(wordListTextAsset);
}
}
}
private static HashSet<string> ParseWordSet(TextAsset asset)
{
string[] parts = asset.text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
HashSet<string> set = new HashSet<string>();
foreach (string p in parts)
{
string trimmed = p.Trim().ToLowerInvariant();
if (!string.IsNullOrWhiteSpace(trimmed))
{
set.Add(trimmed);
}
}
return set;
}
public static bool CheckWord(string word, bool fullDict = true)
{
if (Instance == null || string.IsNullOrWhiteSpace(word)) return false;
if (fullDict)
{
return Instance.fullWordSet != null && Instance.fullWordSet.Contains(word.ToLower());
}
return Instance.WordSet != null && Instance.WordSet.Contains(word.ToLower());
}
public static List<string> GetPossibleWords(string chars, int minLength = 3, int maxLength = 7, bool fullDict = false)
{
chars = chars.ToLower();
List<string> combinations = CharCombinations(chars.ToLower());
HashSet<string> results = new HashSet<string>();
foreach (string item in combinations)
{
if (item.Length >= minLength && item.Length <= maxLength && CheckWord(item, fullDict))
{
results.Add(item.ToUpper());
}
}
return results.ToList();
}
public static string RandomWord(bool fullDict = false)
{
if (Instance == null) return null;
List<string> commonList = (fullDict) ? Instance.fullWordSet?.ToList() : Instance.WordSet?.ToList();
if (commonList == null || commonList.Count == 0) return null;
return commonList.GetRandom();
}
public static List<string> GetRandomWords(int count, int minLength = 3, int maxLength = 7, bool fullDict = false)
{
if (Instance == null) return new List<string>();
List<string> wordSetList = (fullDict) ? Instance.fullWordSet?.ToList() : Instance.WordSet?.ToList();
if (wordSetList == null || wordSetList.Count == 0) return new List<string>();
List<string> candidates = wordSetList.FindAll(w => w.Length >= minLength && w.Length <= maxLength);
if (candidates.Count == 0) return new List<string>();
if (count > candidates.Count) count = candidates.Count;
List<string> results = new List<string>();
for (int i = 0; i < count; i++)
{
int index = UnityEngine.Random.Range(0, candidates.Count);
results.Add(candidates[index]);
candidates.RemoveAt(index);
}
return results;
}
public static string WordOfTheDay(bool fullDict = false)
{
if (Instance == null) return null;
List<string> words = (fullDict) ? Instance.fullWordSet?.ToList() : Instance.WordSet?.ToList();
if (words == null || words.Count == 0) return null;
DateTime today = DateTime.UtcNow.Date;
Int64 todayInt = today.ToBinary();
Int64 wordIndex = todayInt % (Int64)words.Count;
int nWordIndex = Mathf.Abs((int)wordIndex);
string wordOtD = words[(int)nWordIndex];
return wordOtD;
}
/// http://stackoverflow.com/questions/7802822/all-possible-combinations-of-a-list-of-values
public static List<string> CharCombinations(char[] inputCharArray, int minimumItems = 1,
int maximumItems = int.MaxValue)
{
int nonEmptyCombinations = (int)Mathf.Pow(2, inputCharArray.Length) - 1;
List<string> listOfLists = new List<string>(nonEmptyCombinations + 1);
// Optimize generation of empty combination, if empty combination is wanted
if (minimumItems == 0)
listOfLists.Add("");
if (minimumItems <= 1 && maximumItems >= inputCharArray.Length)
{
// Simple case, generate all possible non-empty combinations
for (int bitPattern = 1; bitPattern <= nonEmptyCombinations; bitPattern++)
listOfLists.Add(GenerateCombination(inputCharArray, bitPattern));
}
else
{
// Not-so-simple case, avoid generating the unwanted combinations
for (int bitPattern = 1; bitPattern <= nonEmptyCombinations; bitPattern++)
{
int bitCount = CountBits(bitPattern);
if (bitCount >= minimumItems && bitCount <= maximumItems)
listOfLists.Add(GenerateCombination(inputCharArray, bitPattern));
}
}
return listOfLists;
}
public static List<string> CharCombinations(string input, int minItems = 1, int maxItems = int.MaxValue)
{
return CharCombinations(input.ToCharArray(), minItems, maxItems);
}
private static string GenerateCombination(char[] inputList, int bitPattern)
{
string thisCombination = string.Empty;// new List<T>(inputList.Length);
for (int j = 0; j < inputList.Length; j++)
{
if ((bitPattern >> j & 1) == 1)
thisCombination += inputList[j];
}
return thisCombination;
}
/// <summary>
/// Sub-method of CharCombinations() method to count the bits in a bit pattern. Based on this:
/// https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
/// </summary>
private static int CountBits(int bitPattern)
{
int numberBits = 0;
while (bitPattern != 0)
{
numberBits++;
bitPattern &= bitPattern - 1;
}
return numberBits;
}
}
}