Is it defined to throw an exception? If so, put it in try/catch for that exception.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Is it defined to throw an exception? If so, put it in try/catch for that exception.
If you don't understand my answer, don't ignore it, ask a question.
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.
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.
If you don't understand my answer, don't ignore it, ask a question.
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
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.
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.
If you don't understand my answer, don't ignore it, ask a question.