Words is a library with algorithms that are useful for word games.
Most algorithms require a dictionary – a data structure that can do quick lookups on large lists of words. Dictionaries can be loaded from simple text files.
Words can be configured to work with different alphabets, including alphabets that treat multiple characters as a single letter. This is also occasionally useful even in English games. For example, in many word games, the character pair qu is treated as a single letter. Algorithms on words that don’t require a dictionary usually require you to specify the alphabet.
These are the basic class types in Words, and most methods revolve around them.
There are also some algorithms that works with grids, the principle class type of our Grids library. Grids is a rich library with many concepts of its own. If you are making a grid game with words, you should also read the Grids documentation here:
The main functionality provided by dictionaries are documented in the IWordsDictionary interface:
We also define some extension methods on IWordDictionary:
Our concrete implementation of this interface is the TrieDictionary class. It can do fast lookups of words and prefixes, but also supports various other searches.
There are two main for creating dictionaries:
- Construct a new TrieDictionary, and load words into it in code using one of the extension methods defined on IWordDictionary. Note that you need to specify an alphabet; this is explained in a section below.
- Add an empty game object to your scene, add the WordsDefaultDictionary component to it, and attach your text asset containing your list of words. You can then access the dictionary through the Dictionary property of this component. It also allows some basic filtering, and allows you to select the alphabet from a drop down list.
In some cases you may want to make a new word list file by filtering a larger word list. We provide some basic filtering through a wizard that you can access through the menu: Tools | Gamelogic | Words | Dictionary Filter.
Setting up the alphabet
For English games, we provide the LatinAlphabet class with two constants of letters defined: one is the normal 26 letter alphabet, the other is one where instead of the letter q, the letter qu is used.
You can also provide your own characters when constructing the alphabet, which should be useful for many other languages.
Searching in the dictionary
The IDictionary provides several lookups. For ordinary words, these work as you would expect. But the dictionary also allows you to search for patterns (somewhat simplified regular expressions):
- ? matches any single letter
- * matches zero or more letters
- + matches one or more letters
- at* returns all words that starts with at, including at itself
- at+ will return all the same words, except for at
- at??? will return all the words that start with at and have five letters.
The dictionary does not support more general regular expressions directly. However, dictionaries work with LINQ, so you can always do (somewhat slow) arbitrary searches like this:
dictionary.Where(w => WordSatisfiesSomeCondition(w))
which may include matches against full-blown regular expressions.
Other Dictionary Functions
Dictionaries also have some other functions. Consult the API documentation for more detailed explanations.
- Adding words
- Whether words with given prefix exists
- Finding anagrams and anagrams of subset
- Find sub words
A generator is a simple class that has a single method Next that gives you “the next item”. The particular generator determines what “the next item” means.
Our Extensions library comes with a whole bunch of useful generators, and some extension methods that make them very powerful.
Words come with word and letter generators to generate random elements from the dictionary or alphabet. For example, the following will return random words that have more than 4 letters, and convert them to uppercase:
var generator = new WordGenerator()
The shuffle generator shuffles the letters of words, and mutation generators that shuffle words with so that the edit distance between the shuffled word and edit distance is at least a given value. (This is useful for anagram games, where you don’t want random shuffles that are too close to the original).
Letter generators also support generating letters with specific frequencies, and specific conditional frequencies. These are useful for procedurally constructing puzzles with high probability of having words or having letters that can be used to form words.
See these links for more on generators:
The Algorithms class provides various algorithms for words, including these:
- Edit Distance
- Word mutation
- Largest Common Substring
- Largest Overlap
See here for more detail:
The GridAlgorithms class provides some functions for working with words in grids, including these:
- Filling grids with words that cross
- Deterring whether placement of a word in a partially filled grid is legal (alla Scrabble)
- Finding lines of connected words
- Finding a word in a grid at a point
See here for more detail: