图模型与概率统计assignment02
问题描述
原谅我不放文字了。(我估计没人看这个博客吧,当自己笔记用了,有价值的话后期再改)
代码
注释就加了几行
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Random;
public class SeussAndSaki {
final static String alphabet = "abcdefghijklmnopqrstuvwxyz";
public static void main(String[] args) throws Exception {
String fileA = "spamiam.txt";
String fileB = "saki_story.txt";
randomA();
System.out.println("\nspamiam.txt in strategy B");randomB(fileA);
System.out.println("\nspamiam.txt in strategy C");randomC(fileA);
System.out.println("\nspamiam.txt in strategy D");randomD(fileA);
System.out.println("\nsaki_story.txt in strategy B");randomB(fileB);
System.out.println("\nsaki_story.txt in strategy C");randomC(fileB);
System.out.println("\nsaki_story.txt in strategy D");randomD(fileB);
}
/* Completely Random */
public static void randomA() {
Random random = new Random();
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
for (int k = 1; k <= 4; k++) {
System.out.print(alphabet.charAt(random.nextInt(26)));
}
System.out.print(" ");
}
System.out.println();
}
}
/* Random with frequency as probability */
public static void randomB(String fileName) throws Exception {
int[] frequency = new int[26];
String[] splitedTxt = readToString(fileName);
/* Initial frequency */
for(int i=0; i<26; i++)
frequency[i] = 0;
/* Calculate frequency */
for(String s : splitedTxt)
for(int i=0; i<s.length(); i++)
frequency[s.charAt(i)-97]++;
/* Sum frequency for Random with frequency */
for(int i=1; i<=25; i++)
frequency[i] += frequency[i-1];
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
for (int k = 1; k <= 4; k++) {
System.out.print(strategyB(frequency));
}
System.out.print(" ");
}
System.out.println();
}
}
/* Random with one-step transition probability */
public static void randomC(String fileName) throws Exception {
int[][] frequency = new int[26][27];
int[] total = new int[26];
String[] splitedTxt = readToString(fileName);
/* Initial one-step TP matrix */
for(int i=0; i<26; i++)
for(int j=0; j<27; j++)
frequency[i][j] = 0;
initTotal(total);
/* Calculate matrix and total */
for(String s : splitedTxt) {
if(s.length() == 0)
continue;
total[s.charAt(0)-97]++;
for(int i=1; i<s.length(); i++) {
char cn1 = s.charAt(i-1);
char cn2 = s.charAt(i);
frequency[cn1-97][cn2-97]++;
total[cn2-97]++;
}
}
/* Sum total and matrix for Random with transition frequency */
sumTotal(total);
for(int i=0; i<26; i++)
for(int j=1; j<=26; j++)
frequency[i][j] += frequency[i][j-1];
/* Generate words */
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
char pre = strategyB(total);
System.out.print(pre);
for(int k=1; k<=3; k++) {
char ch = strategyC(total, frequency, pre);
System.out.print(ch);
pre = ch;
}
System.out.print(" ");
}
System.out.println();
}
}
/* Random with two-step transition probability */
public static void randomD(String fileName) throws Exception {
Random random = new Random();
int frequency[][][] = new int[26][26][27];
int frequencyC[][] = new int[26][27];
int[] total = new int[26];
String[] splitedTxt = readToString(fileName);
/* Initial two-step TP matrix */
for(int i=0; i<26; i++)
for(int j=0; j<26; j++)
for(int k=0; k<27; k++)
frequency[i][j][k] = 0;
/* Initial one-step TP matrix */
for(int i=0; i<26; i++)
for(int j=0; j<27; j++)
frequencyC[i][j] = 0;
initTotal(total);
/* Calculate the one-step TP matrix and two-step TP matrix and total */
for(String s : splitedTxt) {
if(s.length() == 0)
continue;
else if(s.length() == 1) {
total[s.charAt(0)-97]++;
continue;
}
total[s.charAt(0)-97]++;
total[s.charAt(1)-97]++;
for(int i=2; i<s.length(); i++) {
char cn1 = s.charAt(i-2);
char cn2 = s.charAt(i-1);
char cn3 = s.charAt(i);
frequency[cn1-97][cn2-97][cn3-97]++;
frequencyC[cn1-97][cn2-97]++;
total[cn3-97]++;
}
frequencyC[s.charAt(s.length()-2)-97][s.charAt(s.length()-1)-97]++;
}
/* Sum the matrices and total for Random with transition frequency */
sumTotal(total);
for(int i=0; i<26; i++) {
for(int j=0; j<26; j++) {
for(int k=1; k<27; k++) {
frequency[i][j][k] += frequency[i][j][k-1];
}
}
}
for(int i=0; i<26; i++) {
for(int j=1; j<27; j++) {
frequencyC[i][j] += frequencyC[i][j-1];
}
}
/* Generate words */
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
char cn1 = strategyB(total);
char cn2 = strategyC(total, frequencyC, cn1);
System.out.print(cn1);
System.out.print(cn2);
for(int k=1; k<=2; k++) {
if(frequency[cn1-97][cn2-97][26] <= 0) {
cn1 = cn2;
cn2 = strategyC(total, frequencyC, cn1);
System.out.print(cn2);
}else {
int r = random.nextInt(frequency[cn1-97][cn2-97][26]);
for(int m=0; m<26; m++) {
if(r <= frequency[cn1-97][cn2-97][m]) {
cn1 = cn2;
cn2 = alphabet.charAt(m);
System.out.print(cn2);
break;
}
}
}
}
System.out.print(" ");
}
System.out.println();
}
}
/* Read files and ignore all characters but a-z(A-Z), and return String[] */
public static String[] readToString(String filename) throws Exception {
File file = new File(filename);
FileReader reader = new FileReader(file);
BufferedReader bf = new BufferedReader(reader);
String txt = "";
String line = null;
while((line = bf.readLine()) != null) {
txt += (line + " ");
}
txt = txt.toLowerCase();
String[] splitedTxt = txt.split(" ");
for(int i=0; i<splitedTxt.length; i++) {
splitedTxt[i] = splitedTxt[i].replaceAll("[^a-zA-Z]", "");
}
bf.close();
return splitedTxt;
}
/* Generate a character with frequency as probability */
public static char strategyB(int[] frequency) {
Random random = new Random();
int r = random.nextInt(frequency[25]);
for(int m=0; m<26; m++) {
if(r <= frequency[m]) {
return alphabet.charAt(m);
}
}
return alphabet.charAt(25);
}
/* Generate a character with one-step transition probability of char pre */
public static char strategyC(int[] total, int[][] frequency, char pre) {
Random random = new Random();
if(frequency[pre-97][26] <= 0) {
pre = strategyB(total);
return pre;
}else {
int r = random.nextInt(frequency[pre-97][26]);
for(int m=0; m<26; m++) {
if(r <= frequency[pre-97][m]) {
return alphabet.charAt(m);
}
}
}
return alphabet.charAt(25);
}
/* Initialize int[] total */
public static void initTotal(int[] total) {
for(int i=0; i<26; i++)
total[i] = 0;
}
/* Sum int[] total */
public static void sumTotal(int[] total) {
for(int i=1; i<=25; i++)
total[i] += total[i-1];
}
}