Skip to content

Commit

Permalink
Add simple random text generator
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinadam committed Nov 23, 2013
1 parent 630b943 commit ab0f402
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/main/java/bullshit_paper/MarkovChain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package bullshit_paper;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Random;

public class MarkovChain {
private HashMap<String, LinkedList<String> > graph;

MarkovChain(){
graph = new HashMap<>();
}

public void build(List<String> sentences){

for(String s: sentences){
List<String> cur = Parser.parseSentence(s);
cur.add(".");

for(int i=-1; i<cur.size()-1; ++i){
LinkedList<String> tmp;
String from;

if(i == -1){
from = "#";
tmp = graph.get(from);
}
else{
from = cur.get(i);
tmp = graph.get(from);
}

if(tmp == null){
tmp = new LinkedList<String>();
}

tmp.add(cur.get(i+1));
graph.put(from, tmp);
}
}

for(Entry<String, LinkedList<String>> entry: graph.entrySet()){
LinkedList<String> tmp = entry.getValue();
Collections.shuffle(tmp);
graph.put(entry.getKey(),tmp);
}

}

public String generateRandomText(int length){
StringBuilder builder = new StringBuilder();
Random random = new Random();

for(int i=0; i<length; ++i){
String cur = "#";

while(cur != "."){
LinkedList<String> list = graph.get(cur);
int size = list.size();
cur = list.get(random.nextInt(size));

if(cur != "."){
builder.append(" "+cur);
}
}

builder.append(".");
}

return builder.toString();
}

}

0 comments on commit ab0f402

Please sign in to comment.