As an AI language model, I am unable to create visual sketches with processing. However, I can suggest a possible approach to implementing the described program.
-
Parse the input text:
The first step in analyzing the input text is to parse it and split it into sentences and then into words. To achieve this, you can use the splitTokens() method from Processing, which splits a string into an array of tokens using a given set of separators.
-
Build a dictionary of transitions:
A Markov-chain model requires a dictionary of the transitions probabilities between words. You can create a HashMap and store the mapped probabilities of each word using a for-loop to iterate over the list of words.
-
Generate a new text:
To generate new text, you can use the dictionary of transition probabilities. Start with a random word from the dictionary and iterate the text generation process until you hit the maximum length or an end-of-sentence token. You can use the random() method to select the next word based on the dictionary of probabilities.
-
Blind mode:
To allow the user to blind his own text, you can implement a simple GUI allowing the user to insert their own text and start the generation process from their text. This can be done by adding a simple text input field and a button to start the generation. Once the user clicks the button, the text is parsed, and the Markov-chain model is updated with new transition probabilities.
-
Display the output:
Finally, you can display the output generated text to the user using a simple text box.
Below is a sample implementation of this approach:
HashMap<String, HashMap<String, Integer>> dict;
int wordCount = 0;
void setup() {
size(400, 400);
dict = new HashMap<String, HashMap<String, Integer>>();
generateDictionary(loadStrings("input.txt"));
}
void draw() {
background(255);
fill(0);
textSize(16);
text(generateText(15), 20, 50);
}
void generateDictionary(String[] sentences) {
for (String sentence : sentences) {
String[] words = splitTokens(sentence, " ");
String current = null;
for (String next : words) {
if (current != null) {
if (dict.containsKey(current)) {
HashMap<String, Integer> transitions = dict.get(current);
if (transitions.containsKey(next)) {
transitions.put(next, transitions.get(next) + 1);
}
else {
transitions.put(next, 1);
}
}
else {
HashMap<String, Integer> transitions = new HashMap<String, Integer>();
transitions.put(next, 1);
dict.put(current, transitions);
}
wordCount++;
}
current = next;
}
}
for (String word : dict.keySet()) {
HashMap<String, Integer> transitions = dict.get(word);
for (String next : transitions.keySet()) {
int count = transitions.get(next);
transitions.put(next, count / wordCount);
}
}
}
String generateText(int maxLength) {
String current = getRandomWord();
String output = current;
while (dict.containsKey(current) && output.length() < maxLength) {
current = getNextWord(current);
output += " " + current;
if (current.endsWith(".")) {
break;
}
}
return output;
}
String getRandomWord() {
Set<String> keySet = dict.keySet();
return (String) keySet.toArray()[(int) random(keySet.size())];
}
String getNextWord(String current) {
HashMap<String, Integer> transitions = dict.get(current);
String[] choices = new String[transitions.size()];
int index = 0;
for (String next : transitions.keySet()) {
for (int i = 0; i < transitions.get(next); i++) {
choices[index++] = next;
}
}
return choices[(int) random(choices.length)];
}
void keyPressed() {
if (key == ENTER) {
String text = getKeyedText();
generateDictionary(text.split("\\. "));
}
}
String getKeyedText() {
pushStyle();
textAlign(CENTER);
textSize(20);
fill(0);
text("Enter Text:", width / 2, height / 2 - 25);
String input = "";
do {
if (keyPressed && key != ENTER) {
input += key;
}
background(255);
textSize(14);
textAlign(LEFT);
text(input, 10, 50, width - 20, height - 50);
textAlign(CENTER);
textSize(20);
text("Enter Text:", width / 2, height / 2 - 25);
} while (!keyPressed || key != ENTER);
popStyle();
return input;
}
This code initializes a Markov-chain model with the contents of a file named input.txt containing input text. The program can then generate new text with the generateText() method, and the user can enter their own text in blind mode by typing into the input field and hitting the Enter key.