Return-Path: Delivered-To: apmail-incubator-uima-commits-archive@locus.apache.org Received: (qmail 71940 invoked from network); 2 Oct 2008 09:37:42 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 2 Oct 2008 09:37:42 -0000 Received: (qmail 95120 invoked by uid 500); 2 Oct 2008 09:37:40 -0000 Delivered-To: apmail-incubator-uima-commits-archive@incubator.apache.org Received: (qmail 95093 invoked by uid 500); 2 Oct 2008 09:37:40 -0000 Mailing-List: contact uima-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: uima-dev@incubator.apache.org Delivered-To: mailing list uima-commits@incubator.apache.org Received: (qmail 95084 invoked by uid 99); 2 Oct 2008 09:37:40 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 02 Oct 2008 02:37:40 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 02 Oct 2008 09:36:46 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 18BDC238899F; Thu, 2 Oct 2008 02:36:51 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r701055 - /incubator/uima/sandbox/trunk/Tagger/src/main/java/org/apache/uima/examples/tagger/Viterbi.java Date: Thu, 02 Oct 2008 09:36:50 -0000 To: uima-commits@incubator.apache.org From: twgoetz@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20081002093651.18BDC238899F@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: twgoetz Date: Thu Oct 2 02:36:50 2008 New Revision: 701055 URL: http://svn.apache.org/viewvc?rev=701055&view=rev Log: Jira UIMA-1193: protect against NPE. With this fix, the error no longer occurs. Test cases go through. https://issues.apache.org/jira/browse/UIMA-1193 Modified: incubator/uima/sandbox/trunk/Tagger/src/main/java/org/apache/uima/examples/tagger/Viterbi.java Modified: incubator/uima/sandbox/trunk/Tagger/src/main/java/org/apache/uima/examples/tagger/Viterbi.java URL: http://svn.apache.org/viewvc/incubator/uima/sandbox/trunk/Tagger/src/main/java/org/apache/uima/examples/tagger/Viterbi.java?rev=701055&r1=701054&r2=701055&view=diff ============================================================================== --- incubator/uima/sandbox/trunk/Tagger/src/main/java/org/apache/uima/examples/tagger/Viterbi.java (original) +++ incubator/uima/sandbox/trunk/Tagger/src/main/java/org/apache/uima/examples/tagger/Viterbi.java Thu Oct 2 02:36:50 2008 @@ -33,10 +33,6 @@ */ public class Viterbi { - static NGram ngram; - - static NGram ngram2; - @SuppressWarnings("unchecked") public static Map init_probs(Map pos_s) { Map init_probs = new HashMap(); @@ -84,6 +80,10 @@ Map> word_probs, double[] lambdas2, double[] lambdas3, double theta) { + NGram ngram = null; + + NGram ngram2 = null; + // here we add something like an "end-of-sentence" tag at the beginning of the first sentence, // to get probabilities for a certain tag to be at the beginning of the sentence. sentence.add(0, "."); @@ -286,6 +286,12 @@ Iterator keyValuePairs = all.entrySet().iterator(); + NGram nextNgram = new NGram(key_next); + double nextNgramTransProb = 0.0; + if (transition_probs.containsKey(nextNgram)) { + nextNgramTransProb = transition_probs.get(nextNgram); + } + /** ********************************************************************************** */ /* * calculates for the given next token which of the possible paths to the current token are @@ -350,8 +356,8 @@ double lambda2 = lambdas2[1]; ppp = (transition_probs.containsKey(ngram)) ? ((lambda2 * transition_probs.get(ngram)) + (lambda1 * transition_probs - .get(new NGram(key_next)))) - : (lambda1 * transition_probs.get(new NGram(key_next))); + .get(nextNgram))) + : (lambda1 * nextNgramTransProb); pp = Math.log(value_next) + Math.log(ppp); // P(t|w) * P(t1,t2,t3) } else if (N == 3) { double lambda1 = lambdas3[0]; @@ -360,19 +366,14 @@ if (transition_probs.containsKey(ngram)) { - // The following commented-out expression used to throw the occasional NPE, probably - // because transition_probs.get(ngram2) is null. Make sure this does not happen. Not - // sure this is the correct way to fix it, though. Test cases still go through... double transProbNgram2 = (transition_probs.containsKey(ngram2) ? transition_probs .get(ngram2) : 0.0); - ppp = (lambda3 * transition_probs.get(ngram)) - + (lambda2 * transProbNgram2) - + (lambda1 * transition_probs.get(new NGram(key_next))); + ppp = (lambda3 * transition_probs.get(ngram)) + (lambda2 * transProbNgram2) + + (lambda1 * nextNgramTransProb); } else { // System.out.println(ngram2); ppp = (transition_probs.containsKey(ngram2)) ? ((lambda2 * transition_probs - .get(ngram2)) + (lambda1 * transition_probs.get(new NGram(key_next)))) - : (lambda1 * transition_probs.get(new NGram(key_next))); + .get(ngram2)) + (lambda1 * nextNgramTransProb)) : (lambda1 * nextNgramTransProb); } pp = Math.log(value_next) + Math.log(ppp); } @@ -381,8 +382,8 @@ double lambda2 = lambdas2[1]; ppp = (transition_probs.containsKey(ngram)) ? ((lambda2 * transition_probs.get(ngram)) + (lambda1 * transition_probs - .get(new NGram(key_next)))) - : (lambda1 * transition_probs.get(new NGram(key_next))); + .get(nextNgram))) + : (lambda1 * transition_probs.get(nextNgram)); pp = Math.log(value_next) + Math.log(ppp); }