Skip to content

Instantly share code, notes, and snippets.

@oleander
Forked from Linusstrom/gist:3342846
Created August 13, 2012 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oleander/3342969 to your computer and use it in GitHub Desktop.
Save oleander/3342969 to your computer and use it in GitHub Desktop.
class WordNgram{
private int size;
private String[] words ={};
public WordNgram(int size){
this.size = size;
}
public WordNgram(int size, String words){
this.size = size;
this.words = words.split(" ");
}
public String getWord(int size){
String result = null;
try {
result = this.toString().split(" ")[size].trim();
} catch(ArrayIndexOutOfBoundsException e){
System.out.println("index out of bounds");
return "";
}
return result;
}
public String toString(){
if (words.length == 0){
String value = "";
for ( int n=0; n<size; n++) {
value = value + ",";
}
return value;
}
if (size > this.words.length) {
String value = "";
for ( int n=0; n<size; n++) {
value = value + " " + words[n % words.length];
}
return value;
}
String result = "";
int n = 0;
while(true){
try {
String last = this.words[this.size + n - 1];
} catch(ArrayIndexOutOfBoundsException e){
return result;
}
for (int i = n; i <= (this.size + n - 1); i++) {
result += " " + words[i];
}
result += "\n";
n++;
}
}
public static void main(String[] args) {
WordNgram w1 = new WordNgram(5, "En katt sover i korgen .");
System.out.println(w1.getWord(2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment