马尔可夫链,基于概率的随机文本。 Java
Markov Chains, random text based on probability. Java
我正在尝试根据输入文本的重复概率生成 140 个字符的字符串。我已经有一个包含每个有效字符的数组,并且每个字符的数组概率不同。
char[] array = [a, b, c, ...]
double[] array2 = [.1, .3, .4, ...]
我已经读到我需要从 0-1 生成一个随机双精度数,但我不知道如何将它与我的数组相关联并生成一个 140 个字符的字符串。只需要有关该方法的帮助,至少需要有关如何操作的说明。
提前致谢。
这是我的实现。如果您有任何问题,请告诉我。
import java.util.Random;
public class MarkovChainTest {
private static char[] array = {'a', 'b', 'c', 'd'};
private static double[] array2 = {.1, .3, .4, .2};
public static void main(String[] args) {
System.out.println(generateString(array, normalizeToOne(array2), 140));
}
private static double[] normalizeToOne(double[] freqs) {
double sum = 0;
for (double freq : freqs)
sum += freq;
double[] ret = new double[freqs.length];
for (int i = 0; i < freqs.length; i++)
ret[i] = freqs[i] / sum;
return ret;
}
private static String generateString(char[] chars, double[] freqs, int length) {
return generateString(chars, freqs, length, new Random(System.currentTimeMillis()));
}
private static String generateString(char[] chars, double[] freqs, int length, Random rnd) {
StringBuilder ret = new StringBuilder();
for (int i = 0; i < length; i++)
ret.append(generateChar(chars, freqs, rnd));
return ret.toString();
}
private static char generateChar(char[] chars, double[] freqs, Random rnd) {
double val = rnd.nextDouble();
double sumOfPrevFreqs = 0;
for (int i = 0; i < chars.length; i++) {
if (sumOfPrevFreqs + freqs[i] >= val)
return chars[i];
sumOfPrevFreqs += freqs[i];
}
throw new IllegalStateException("Sum of frequencies (" + sumOfPrevFreqs + ") < " + val);
}
}
我正在尝试根据输入文本的重复概率生成 140 个字符的字符串。我已经有一个包含每个有效字符的数组,并且每个字符的数组概率不同。
char[] array = [a, b, c, ...]
double[] array2 = [.1, .3, .4, ...]
我已经读到我需要从 0-1 生成一个随机双精度数,但我不知道如何将它与我的数组相关联并生成一个 140 个字符的字符串。只需要有关该方法的帮助,至少需要有关如何操作的说明。
提前致谢。
这是我的实现。如果您有任何问题,请告诉我。
import java.util.Random;
public class MarkovChainTest {
private static char[] array = {'a', 'b', 'c', 'd'};
private static double[] array2 = {.1, .3, .4, .2};
public static void main(String[] args) {
System.out.println(generateString(array, normalizeToOne(array2), 140));
}
private static double[] normalizeToOne(double[] freqs) {
double sum = 0;
for (double freq : freqs)
sum += freq;
double[] ret = new double[freqs.length];
for (int i = 0; i < freqs.length; i++)
ret[i] = freqs[i] / sum;
return ret;
}
private static String generateString(char[] chars, double[] freqs, int length) {
return generateString(chars, freqs, length, new Random(System.currentTimeMillis()));
}
private static String generateString(char[] chars, double[] freqs, int length, Random rnd) {
StringBuilder ret = new StringBuilder();
for (int i = 0; i < length; i++)
ret.append(generateChar(chars, freqs, rnd));
return ret.toString();
}
private static char generateChar(char[] chars, double[] freqs, Random rnd) {
double val = rnd.nextDouble();
double sumOfPrevFreqs = 0;
for (int i = 0; i < chars.length; i++) {
if (sumOfPrevFreqs + freqs[i] >= val)
return chars[i];
sumOfPrevFreqs += freqs[i];
}
throw new IllegalStateException("Sum of frequencies (" + sumOfPrevFreqs + ") < " + val);
}
}