MarkovTextGen/WordNode.java
2024-08-10 01:24:54 -05:00

84 lines
2.5 KiB
Java

/**
* The WordNode class represents one or more consecutive words from the training data, and
* every one word that came after that word in the training data, as well as the number of
* times each word appeared.
*
* Example:
* The dog -
* eats, 5
* is, 2
* likes, 1
* chases, 3
*
* That example uses two words as the key. Any number of words can technically be used, but
* anything over two will likely result in it directly plagairizing the training data.
*/
import java.util.ArrayList;
public class WordNode {
private String key;
private ArrayList<String> wordValues;
private ArrayList<Integer> intValues;
/**
* Constructor method used to create a WordNode with an initial key value pair.
*/
public WordNode(String k, String v) {
key = k;
wordValues = new ArrayList<>();
intValues = new ArrayList<>();
wordValues.add(v);
intValues.add(1);
}
/**
* Method used to add a word that follows this WordNode.
*/
public void add(String str) {
str = str.trim();
// If str has already been found, increase the corresponding int by 1.
if(wordValues.contains(str)) {
int index = wordValues.indexOf(str);
intValues.set(index, intValues.get(index) + 1);
} else { // If str has not been found. Add it and a 1 to appropriate ArrayLists.
wordValues.add(str);
intValues.add(1);
}
}
/**
* Returns a word from wordValues, using the number of times each word
* appeared to determine the probability of returning that word.
*/
public String generateWord() {
/* Total represents the sum of every element in intValues, which is
the number of times the key appears in the training data. This
is used to determine the range for the random number generator. */
int total = 0;
for(int x : intValues) {
total += x;
}
// random number 0 through (total - 1)
int random = (int) (Math.random() * total);
int index = 0;
int count = intValues.get(index);
for(int i = 0; i < random; i++) {
count --;
if(count == 0) {
index++;
count = intValues.get(index);
}
}
return wordValues.get(index);
}
public String getKey() { return key; }
public ArrayList<String> getWordVals() { return wordValues; }
public ArrayList<Integer> getNumVals() { return intValues; }
}