Java program to generate new text from text using Markov Chains.

50 “words” long text generated using Markov Chains using Pride and Prejudice by Jane Austin as original text.

know the card party, or soothe her eyes. How strange!”  We dine here, I vow. Well, that he was, from the hall once in useless alarm,” added he, when Mr. Bingley, it is sure she concluded, unless among his endeavours in cutting off her aunt’s invitation was indifferent—but that

Main.java

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;

// © 2019 TheFlyingKeyboard and released under MIT License 
// theflyingkeyboard.net

public class Main {
    public static void main(String[] args) throws URISyntaxException, IOException {
        Dictionary dictionary = new Dictionary();
        String text = new String(Files.readAllBytes(Paths.get(Main.class.getClassLoader().getResource("text.txt").toURI())));
        text = text.replace("\n", " ");
        text = text.replace("“", " ");

        String[] words = text.split(" ");

        for (int i = 0; i < words.length - 1; ++i) {
            dictionary.addWordPair(words[i], words[i + 1]);
        }

        String nextWord = "know";

        for (int i = 0; i < 50; ++i) {
            System.out.print(nextWord + " ");
            nextWord = dictionary.getNextWord(nextWord);
        }
    }
}

Dictionary.java

import java.util.HashMap;
import java.util.Map;

// © 2019 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net

public class Dictionary {
    private Map<String, NextWordProbabilities> words = new HashMap<>();

    public void addWordPair(String word, String nextWord) {
        if (words.containsKey(word)) {
            words.get(word).addWord(nextWord);
        } else {
            NextWordProbabilities nextWordProbabilities = new NextWordProbabilities();
            nextWordProbabilities.addWord(nextWord);

            words.put(word, nextWordProbabilities);
        }
    }

    public String getNextWord(String word) {
        if (words.containsKey(word)) {
            return words.get(word).getNextWord();
        }

        return null;
    }
}

NextWordProbabilities.java

import org.apache.commons.lang3.mutable.MutableInt;

import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

// © 2019 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net

public class NextWordProbabilities {
    private Map<String, MutableInt> afterWordsCounts = new HashMap<>();
    private Map<String, Double> afterWordsProbabilities = new HashMap<>();
    private final Random randomGenerator = new SecureRandom();

    public void addWord(String wordToAdd) {
        if (afterWordsCounts.containsKey(wordToAdd)) {
            afterWordsCounts.get(wordToAdd).add(1);
        } else {
            afterWordsCounts.put(wordToAdd, new MutableInt(1));
        }
    }

    public String getNextWord() {
        if (afterWordsProbabilities.size() == 0) {
            countProbabilities();
        }

        double random = randomGenerator.nextDouble();
        double sum = 0.0d;

        for (Map.Entry<String, Double> entry : afterWordsProbabilities.entrySet()) {
            sum += entry.getValue();

            if (random <= sum) {
                return entry.getKey();
            }
        }

        return null;
    }

    private void countProbabilities() {
        long sum = 0;

        for (Map.Entry<String, MutableInt> entry : afterWordsCounts.entrySet()) {
            sum += entry.getValue().getValue();
        }

        for (Map.Entry<String, MutableInt> entry : afterWordsCounts.entrySet()) {
            afterWordsProbabilities.put(entry.getKey(), (double) entry.getValue().getValue() / sum);
        }
    }

}



Java Markov Chains Text generation
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close