public class LanguageCipher extends Object implements Serializable
setCacheLevel()
, where it starts at 2 (writing to table and reverse), and can be lowered to 1 (writing to
table only) if you don't need reverse to decipher a language easily, or to 0 (writing to neither) if you expect that
memory will be at a premium and don't mind re-generating the same word each time it occurs in a source text. If
cacheLevel is 1 or less, then this will not check for overlap between previously-generated words (it won't have an
easy way to look up previously-generated ones) and so may be impossible to accurately decipher. As an example, one
test of level 1 generated "he" as the translation for both "a" and "at", so every time "a" had been ciphered and then
deciphered, the reproduced version said "at" instead. This won't happen by default, but the default instead relies on
words being entered as inputs to cipher() or lookup() in the same order. If words are entered in two different orders
to different runs of the program, they may have different generated results if cacheLevel is 2. One way to handle
this is to use cacheLevel 2 and cipher the whole game script, or just the unique words in it (maybe just a large word
list, such as http://wordlist.aspell.net/12dicts/ ), then serialize the LanguageCipher for later usage.Modifier and Type | Field and Description |
---|---|
int |
cacheLevel
The degree of vocabulary to cache to speed up future searches at the expense of memory usage.
|
FakeLanguageGen |
language
The FakeLanguageGen this will use to construct words; normally one of the static fields in FakeLanguageGen or a
FakeLanguageGen produced by using the mix() method of one of them.
|
HashMap<String,String> |
reverse
The mapping of lower-case word keys to lower-case word values, where keys are in the source language and values
are generated by language.
|
HashMap<String,String> |
table
The mapping of lower-case word keys to lower-case word values, where keys are in the source language and values
are generated by language.
|
Constructor and Description |
---|
LanguageCipher()
Constructs a LanguageCipher that will generate English-like or Dutch-like text by default.
|
LanguageCipher(FakeLanguageGen language)
Constructs a LanguageCipher that will use the given style of language generator to produce its text.
|
LanguageCipher(LanguageCipher other)
Copies another LanguageCipher and constructs this one with the information in the other.
|
Modifier and Type | Method and Description |
---|---|
String |
cipher(CharSequence text)
Given a String, StringBuilder, or other CharSequence that should contain words in the source language, this
translates each word to the fake language, using existing translations if previous calls to cipher() or lookup()
had translated that word.
|
String |
decipher(String text,
Map<String,String> vocabulary)
Deciphers words in an already-ciphered text with a given String-to-String Map for a vocabulary.
|
int |
getCacheLevel() |
LanguageCipher |
learnTranslation(Map<String,String> vocabulary,
String sourceWord)
Adds a translation pair to vocabulary so it can be used in decipher, giving a correct translation for sourceWord.
|
LanguageCipher |
learnTranslations(Map<String,String> vocabulary,
Iterable<String> sourceWords)
Adds translation pairs to vocabulary so it can be used in decipher, giving a correct translation for sourceWords.
|
LanguageCipher |
learnTranslations(Map<String,String> vocabulary,
String... sourceWords)
Adds translation pairs to vocabulary so it can be used in decipher, giving a correct translation for sourceWords.
|
String |
lookup(String source)
Given a word in the source language (usually English), looks up an existing translation for that word, or if none
exists, generates a new word based on the hash of the source word and this LanguageCipher's FakeLanguageGen.
|
LanguageCipher |
mismatchTranslation(Map<String,String> vocabulary,
String correctWord,
String mismatchWord)
Adds a translation pair to vocabulary so it can be used in decipher, giving a typically-incorrect translation for
correctWord where it provides mismatchWord instead when the ciphered version of correctWord appears.
|
void |
setCacheLevel(int cacheLevel) |
public FakeLanguageGen language
public HashMap<String,String> table
public HashMap<String,String> reverse
public int cacheLevel
public LanguageCipher()
public LanguageCipher(FakeLanguageGen language)
language
- a FakeLanguageGen, typically one of the static constants in that class or a mix of them.public LanguageCipher(LanguageCipher other)
other
- a previously-constructed LanguageCipher.public String lookup(String source)
source
- a word in the source languagepublic String cipher(CharSequence text)
text
- a CharSequence, such as a String, that contains words in the source languagepublic String decipher(String text, Map<String,String> vocabulary)
text
- a text in the fake languagevocabulary
- a Map of Strings in the fake language to Strings in the source languagepublic LanguageCipher learnTranslation(Map<String,String> vocabulary, String sourceWord)
vocabulary
- a Map of String keys to String values that will be modified in-placesourceWord
- a word in the source language, typically English; the meaning will be "learned" for decipherpublic LanguageCipher learnTranslations(Map<String,String> vocabulary, String... sourceWords)
vocabulary
- a Map of String keys to String values that will be modified in-placesourceWords
- an array or vararg of words in the source language, typically English; their meanings will
be "learned" for decipherpublic LanguageCipher learnTranslations(Map<String,String> vocabulary, Iterable<String> sourceWords)
vocabulary
- a Map of String keys to String values that will be modified in-placesourceWords
- an Iterable of words in the source language, typically English; their meanings will be
"learned" for decipherpublic LanguageCipher mismatchTranslation(Map<String,String> vocabulary, String correctWord, String mismatchWord)
vocabulary
- a Map of String keys to String values that will be modified in-placecorrectWord
- a word in the source language, typically English; where the ciphered version of this
appears and the text is deciphered, mismatchWord will be used insteadmismatchWord
- a String that will be used for deciphering in place of the translation of correctWord.public int getCacheLevel()
public void setCacheLevel(int cacheLevel)
Copyright © 2012–2016. All rights reserved.