Skip to content

Commit

Permalink
Ngram - initial commit
Browse files Browse the repository at this point in the history
Class working for n-gram default n =2
  • Loading branch information
dabidgs3 committed Sep 5, 2016
0 parents commit e6644bd
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
85 changes: 85 additions & 0 deletions NGramClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* BCE -Copyright 2014
* ngramproject
*/
package ngramproject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
* @author David Guzman
*
*/
public class NGramClass {

private String words[]; // Initial Text
private HashMap<String, List<String>> nGramsMap; // NGram Map
private int counter = 0; // Counter value
private int n = 2; // Value for NGram default 2

/**
* Constructor Text parsing and splitting
*
* @param text
* , input text
* @param n
* , n-gram value [Default = 2]
*/
NGramClass(String text, int n) {

if (text == null || text.length() == 0)
throw new IllegalArgumentException("Not valid text inserted");

text.toLowerCase().replaceAll("[.,;]", "");
words = text.split(" ");
nGramsMap = new HashMap<String, List<String>>();

if (n > 2)
this.n = n;
}

/**
* Method to generate Ngrams
* @return Map with ngrams - Defaul n = 2
*/
public Map<String, List<String>> generateNgrams() {

for (int i = 0; i <= words.length - n; i++) {
counter = 0;
StringBuilder sb = new StringBuilder();
int j = 0;
while (j < n - 1) {
sb.append(words[i + j].trim());
j++;
if (j < n - 1)
sb.append(" ");
//System.out.println(sb.toString());
}
String key = sb.toString();
if (!nGramsMap.containsKey(key)) {
ArrayList<String> list = new ArrayList<>();
list.add(words[i + j]);
nGramsMap.put(key, list);


} else {

List<String> list = nGramsMap.get(key);
if(list.contains(words[i+j]))
counter++;
list.add(words[i + j]);

}
System.out.println("(( " + key + "," + words[i + j] + ")" + ","+counter +")");
}

return nGramsMap;

}


}
28 changes: 28 additions & 0 deletions TestClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* BCE -Copyright 2014
* ngramproject
*/
package ngramproject;


/**
* @author David Guzman
*
*/
public class TestClass {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
NGramClass n = new NGramClass("As we wound on our endless way, as we that the our endless. we wound wound we", 2);
n.generateNgrams();
}catch(Exception e)
{
System.out.print(e.getMessage());
}
}

}

0 comments on commit e6644bd

Please sign in to comment.