Generate an array filled with gibberish words. - Java java.lang

Java examples for java.lang:String Random

Description

Generate an array filled with gibberish words.

Demo Code

/* *****************************************************************************
 * Project:           StoneQuest//from w ww. jav a 2s  .  c  o m
 * File Name:         StringHelper.java
 * Author:            Matt Schwartz
 * Date Created:      12.03.2013 
 * Redistribution:    You are free to use, reuse, and edit any of the text in
                        this file.  You are not allowed to take credit for code
                        that was not written fully by yourself, or to remove 
                        credit from code that was not written fully by yourself.  
                        Please email stonequest.bcgames@gmail.com for issues or concerns.
 * File Description:  
 ************************************************************************** */

//package com.java2s;

import java.util.ArrayList;
import java.util.HashMap;

import java.util.Map;

public class Main {
    public static void main(String[] argv) {
        int numWords = 42;
        generateGibberish(numWords);
    }

    private static final Map VOWEL_FREQUENCY_MAP = new HashMap<Character, Double>();
    private static final char[] VOWELS_BY_FREQ = { 'e', 'a', 'o', 'i', 'u' };
    private static final double[] VOWEL_FREQ_PERC = { 12.702, 8.167, 7.507,
            6.966, 2.758 };
    /**
     * These values are used to generate a gibberish word list.
     */
    private static final char[] CONSONANTS_BY_FREQ = { 't', 'n', 's', 'h',
            'r', 'd', 'l', 'c', 'm', 'w', 'f', 'g', 'y', 'p', 'b', 'v',
            'k', 'j', 'x', 'q', 'z' };
    private static final double[] CONSONANTS_OCCURRENCE_PERC = { 9.056,
            6.749, 6.327, 6.094, 5.987, 4.253, 4.025, 2.782, 2.406, 2.36,
            2.228, 2.015, 1.974, 1.929, 1.492, 0.978, 0.772, 0.153, 0.15,
            0.095, 0.074 };
    private static final Map CONSONANT_FREQUENCY_MAP = new HashMap<Character, Double>();
    /**
     * A list of randomly generated "gibberish" words for use whenever a scroll has not been identified.
     */
    public static final ArrayList GIBBERISH_WORD_LIST = new ArrayList<String>();

    /**
     * Generate an array filled with gibberish words. Used for the static naming of unidentified words
     */
    public static void generateGibberish(int numWords) {
        for (int i = 0; i < CONSONANTS_BY_FREQ.length; i++) {
            CONSONANT_FREQUENCY_MAP.put(CONSONANTS_BY_FREQ[i],
                    CONSONANTS_OCCURRENCE_PERC[i]);
        }
        for (int i = 0; i < VOWELS_BY_FREQ.length; i++) {
            VOWEL_FREQUENCY_MAP.put(VOWELS_BY_FREQ[i], VOWEL_FREQ_PERC[i]);
        }
        String str;
        int lengthOfWord;
        for (int i = 0; i < numWords; i++) {
            str = "";
            lengthOfWord = 2;
            if (Math.random() * 100 <= 80) {
                lengthOfWord++;
                if (Math.random() * 100 <= 55) {
                    lengthOfWord++;
                    if (Math.random() * 100 <= 45) {
                        lengthOfWord++;
                    }
                }
            }
            str += Math.random() * 100 <= 17 ? getWeightedRandomCharacter(VOWEL_FREQUENCY_MAP)
                    : getWeightedRandomCharacter(CONSONANT_FREQUENCY_MAP);
            for (int j = 1; j < lengthOfWord; j++) {
                if (VOWEL_FREQUENCY_MAP
                        .containsKey(str.toCharArray()[j - 1])) {
                    str += getWeightedRandomCharacter(CONSONANT_FREQUENCY_MAP);
                } else {
                    if (j >= 2
                            && CONSONANT_FREQUENCY_MAP.containsKey(str
                                    .toCharArray()[j - 2])) {
                        str += (Math.random() * 100) <= 5 ? getWeightedRandomCharacter(CONSONANT_FREQUENCY_MAP)
                                : getWeightedRandomCharacter(VOWEL_FREQUENCY_MAP);
                    } else {
                        str += (Math.random() * 100) <= 44 ? getWeightedRandomCharacter(CONSONANT_FREQUENCY_MAP)
                                : getWeightedRandomCharacter(VOWEL_FREQUENCY_MAP);
                    }
                }
            }
            if (GIBBERISH_WORD_LIST.contains(str)) {
                i--;
                continue;
            }
            GIBBERISH_WORD_LIST.add(str);
        }
    }

    /**
     * Performs a weighted randomized letter generator where the weights are based on each letter's rough percent
     * frequency
     *
     * @param charMap a map containing characters and the corresponding character's frequency
     * @return a weighted random character
     */
    private static char getWeightedRandomCharacter(
            Map<Character, Double> charMap) {
        double totalWeight = 0;
        double randomValue;
        for (double val : charMap.values()) {
            totalWeight += val;
        }
        randomValue = Math.random() * totalWeight;
        for (char letter : charMap.keySet()) {
            if (randomValue < charMap.get(letter)) {
                return letter;
            }
            randomValue -= charMap.get(letter);
        }
        return '?';
    }
}

Related Tutorials