-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathWordList.cs
203 lines (192 loc) · 7.8 KB
/
WordList.cs
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
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)
{
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)
{
wordSet = new HashSet<string>(wordListTextAsset.text.Split("\n"[0]));
TextAsset fullDictionaryTextAsset = Resources.Load("WordList", typeof(TextAsset)) as TextAsset;
fullWordSet = new HashSet<string>(fullDictionaryTextAsset.text.Split("\n"[0]));
}
public void Init(TextAsset wordListTextAsset, TextAsset fullDictionaryTextAsset)
{
wordSet = new HashSet<string>(wordListTextAsset.text.Split("\n"[0]));
fullWordSet = new HashSet<string>(fullDictionaryTextAsset.text.Split("\n"[0]));
}
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;
fullWordSet = new HashSet<string>(fullDictionaryTextAsset.text.Split("\n"[0]));
if (wordSource != WordSource.Full)
{
TextAsset wordListTextAsset = Resources.Load(strWordResourceName, typeof(TextAsset)) as TextAsset;
wordSet = new HashSet<string>(wordListTextAsset.text.Split("\n"[0]));
}
}
public static bool CheckWord(string word, bool fullDict = true)
{
if (fullDict)
{
return Instance.fullWordSet.Contains(word.ToLower());
}
return 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)
{
List<string> commonList = (fullDict) ? Instance.fullWordSet.ToList() : Instance.WordSet.ToList();
return commonList.GetRandom();
}
public static List<string> GetRandomWords(int count, int minLength = 3, int maxLength = 7, bool fullDict = false)
{
List<string> wordSetList = (fullDict) ? Instance.fullWordSet.ToList() : Instance.WordSet.ToList();
List<string> results = new List<string>();
for (int i = 0; i < count; i++)
{
string word = "";
while (word.Length > maxLength || word.Length < minLength || results.Contains(word))
{
word = wordSetList.GetRandom();
}
results.Add(word);
}
return results;
}
public static string WordOfTheDay(bool fullDict = false)
{
List<string> words = (fullDict) ? Instance.fullWordSet.ToList() : Instance.WordSet.ToList();
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;
}
}
}