Is it defined to throw an exception? If so, put it in try/catch for that exception.
Printable View
Is it defined to throw an exception? If so, put it in try/catch for that exception.
Ok...finally now it is giving an output.
[OUTPUT]
layers that fly that food . that fly sneaks{}
[/OUTPUT]
Now the problem is the tagging part is not working (noun,verb or verbnoun) The tagger needs to tag each word in a sentence.
Code :
import java.util.HashMap; import java.util.Map; import org.junit.Test; public class Tagger { public HashMap my_member_variable = new HashMap(); public static void main (String args[]) { Tagger tag = new Tagger(); // Access member variable of tag System.out.println ("layers that fly that food . that fly sneaks" + tag.my_member_variable ); } String tagset; String corpus; Map<String, String> lexicon; String lexiconString; //private String HashMap; @Test public void testTagger() { //System.out.println(tag("layers that fly that food . that fly sneaks .")); } private void initData() { lexicon = new HashMap<String, String>(); lexicon.put("fly", "vn"); lexicon.put("layers", "vn"); lexicon.put("sneaks", "v"); lexicon.put("food", "n"); lexicon.put("that", "det,rp"); lexicon.put(".", "period"); StringBuilder lexBuf = new StringBuilder(); for (String word : lexicon.keySet()) { StringBuilder append = lexBuf.append(word).append(" "); } lexiconString = lexBuf.toString(); tagset = "det rp vn v n period"; corpus = "that fly layers. that fly sneaks. that, that sneaks food" + "layers. that fly layers that food. layers."; } public String tag(String input) { initData(); ViterbiHMM hmm = new ViterbiHMM(tagset, lexiconString, input, lexicon, corpus, true); String[] in = input.split(" "); String[] re = hmm.mostProbableSequence().split(" "); StringBuilder buf = new StringBuilder(); for (int i = 0; i < in.length; i++) { StringBuilder append = buf.append(in[i]).append(":").append(re[i]).append(" "); } return buf.toString(); } }
Please post the program's output and add some comments to it that describes what is wrong with the output and show what the output should be.
The output is printing out the training set which is pre- programmed in the code. Which is this:
layers that fly that food . that fly sneaks
The output should actually be something like this:
layers/vn that fly/vn that food/n . that/det,rp fly sneaks
This part of the code initialize the lexicon, the tagset and the untagged corpus
Code :
private void initData() { lexicon = new HashMap<String, String>(); lexicon.put("fly", "vn"); lexicon.put("layers", "vn"); lexicon.put("sneaks", "v"); lexicon.put("food", "n"); lexicon.put("that", "det,rp"); lexicon.put(".", "period"); StringBuilder lexBuf = new StringBuilder(); for (String word : lexicon.keySet()) { StringBuilder append = lexBuf.append(word).append(" "); } lexiconString = lexBuf.toString(); tagset = "det rp vn v n period"; corpus = "that fly layers. that fly sneaks. that, that sneaks food" + "layers. that fly layers that food. layers."; }
This part of the code adds the POS tag for every word in the input and returns that tagged string using the most probable tag set for the input.
Code :
public String tag(String input) { initData(); ViterbiHMM hmm = new ViterbiHMM(tagset, lexiconString, input, lexicon, corpus, true); String[] in = input.split(" "); String[] re = hmm.mostProbableSequence().split(" "); StringBuilder buf = new StringBuilder(); for (int i = 0; i < in.length; i++) { buf.append(in[i] + ":" + re[i] + " "); } return buf.toString(); }
Are the methods being called? Add some println statements to each method and constructor to print a message when it is executed so you can see if the methods are being executed.