You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by dr...@apache.org on 2010/02/20 19:47:23 UTC

svn commit: r912189 - in /lucene/mahout/trunk/utils/src: main/java/org/apache/mahout/utils/nlp/collocations/llr/ test/java/org/apache/mahout/utils/nlp/collocations/llr/

Author: drew
Date: Sat Feb 20 18:47:22 2010
New Revision: 912189

URL: http://svn.apache.org/viewvc?rev=912189&view=rev
Log:
Improvements to llr collocations, see MAHOUT-299

Removed:
    lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/NGramCollector.java
    lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/NGramCollectorTest.java
Modified:
    lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/CollocCombiner.java
    lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/CollocMapper.java
    lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/CollocReducer.java
    lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/Gram.java
    lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/LLRReducer.java
    lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/CollocMapperTest.java
    lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/CollocReducerTest.java
    lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/GramTest.java
    lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/LLRReducerTest.java

Modified: lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/CollocCombiner.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/CollocCombiner.java?rev=912189&r1=912188&r2=912189&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/CollocCombiner.java (original)
+++ lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/CollocCombiner.java Sat Feb 20 18:47:22 2010
@@ -71,9 +71,6 @@
     subgramKey.setFrequency(subgramFrequency);
     
     for (Gram ngram : ngramSet.keySet()) {
-      if (subgramKey.getType() == Type.UNIGRAM) {
-        ngram.setType(subgramKey.getType());
-      }
       output.collect(subgramKey, ngram);
     }
   }

Modified: lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/CollocMapper.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/CollocMapper.java?rev=912189&r1=912188&r2=912189&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/CollocMapper.java (original)
+++ lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/CollocMapper.java Sat Feb 20 18:47:22 2010
@@ -17,10 +17,6 @@
 
 package org.apache.mahout.utils.nlp.collocations.llr;
 
-import static org.apache.mahout.utils.nlp.collocations.llr.Gram.Type.HEAD;
-import static org.apache.mahout.utils.nlp.collocations.llr.Gram.Type.TAIL;
-import static org.apache.mahout.utils.nlp.collocations.llr.Gram.Type.UNIGRAM;
-
 import java.io.IOException;
 import java.util.Iterator;
 
@@ -110,9 +106,9 @@
   public void map(Text key, StringTuple value,
                   final OutputCollector<Gram,Gram> collector, Reporter reporter) throws IOException {
     
-    ShingleFilter sf = new ShingleFilter(new IteratorTokenStream(value.getEntries().iterator()),
-        maxShingleSize);
+    ShingleFilter sf = new ShingleFilter(new IteratorTokenStream(value.getEntries().iterator()), maxShingleSize);
     int count = 0; // ngram count
+    
     OpenObjectIntHashMap<String> ngrams = new OpenObjectIntHashMap<String>(value.getEntries().size()
                                                                            * (maxShingleSize - 1));
     OpenObjectIntHashMap<String> unigrams = new OpenObjectIntHashMap<String>(value.getEntries().size());
@@ -122,58 +118,59 @@
       String type = ((TypeAttribute) sf.getAttribute(TypeAttribute.class)).type();
       if ("shingle".equals(type)) {
         count++;
-        if (ngrams.containsKey(term) == false) {
-          ngrams.put(term, 1);
-        } else {
-          ngrams.put(term, 1 + ngrams.get(term));
-        }
+        ngrams.adjustOrPutValue(term, 1, 1);
       } else if (emitUnigrams && term.length() > 0) { // unigram
-        if (unigrams.containsKey(term) == false) {
-          unigrams.put(term, 1);
-        } else {
-          unigrams.put(term, 1 + unigrams.get(term));
-        }
+        unigrams.adjustOrPutValue(term, 1, 1);
       }
     } while (sf.incrementToken());
     
-    ngrams.forEachPair(new ObjectIntProcedure<String>() {
-      
-      @Override
-      public boolean apply(String term, int frequency) {
-        Gram ngram = new Gram(term, frequency);
-        // obtain components, the leading (n-1)gram and the trailing unigram.
-        int i = term.lastIndexOf(' ');
-        if (i != -1) { // bigram, trigram etc
+    try {
+      ngrams.forEachPair(new ObjectIntProcedure<String>() {
+        @Override
+        public boolean apply(String term, int frequency) {
+          // obtain components, the leading (n-1)gram and the trailing unigram.
+          int i = term.lastIndexOf(' '); // TODO: fix for non-whitespace delimited languages.
+          if (i != -1) { // bigram, trigram etc
+            Gram ngram = new Gram(term, frequency, Gram.Type.NGRAM);
+            try {
+              collector.collect(new Gram(term.substring(0, i), frequency, Gram.Type.HEAD), ngram);
+              collector.collect(new Gram(term.substring(i + 1), frequency, Gram.Type.TAIL), ngram);
+            } catch (IOException e) {
+              throw new IllegalStateException(e);
+            }
+          }
+          return true;
+        }
+      });
+  
+      unigrams.forEachPair(new ObjectIntProcedure<String>() {
+        @Override
+        public boolean apply(String term, int frequency) {
           try {
-            collector.collect(new Gram(term.substring(0, i), frequency, HEAD), ngram);
-            collector.collect(new Gram(term.substring(i + 1), frequency, TAIL), ngram);
+            Gram unigram = new Gram(term, frequency, Gram.Type.UNIGRAM);
+            collector.collect(unigram, unigram);
           } catch (IOException e) {
-            throw new RuntimeException(e);
+            throw new IllegalStateException(e);
           }
+          return true;
         }
-        return true;
+      });
+    }
+    catch (IllegalStateException ise) {
+      // catch an re-throw original exceptions from the procedures.
+      if (ise.getCause() instanceof IOException) {
+        throw (IOException) ise.getCause();
       }
-    });
-    
-    unigrams.forEachPair(new ObjectIntProcedure<String>() {
-      @Override
-      public boolean apply(String term, int frequency) {
-        try {
-          Gram ngram = new Gram(term, frequency);
-          Gram unigram = new Gram(term, frequency, UNIGRAM);
-          collector.collect(unigram, ngram);
-        } catch (IOException e) {
-          throw new RuntimeException(e);
-        }
-        return true;
+      else {
+        // wasn't what was expected, so re-throw
+        throw ise;
       }
-    });
+    }
     
     reporter.incrCounter(Count.NGRAM_TOTAL, count);
     
     sf.end();
     sf.close();
-    
   }
   
   /** Used to emit tokens from an input string array in the style of TokenStream */

Modified: lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/CollocReducer.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/CollocReducer.java?rev=912189&r1=912188&r2=912189&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/CollocReducer.java (original)
+++ lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/CollocReducer.java Sat Feb 20 18:47:22 2010
@@ -26,7 +26,6 @@
 import org.apache.hadoop.mapred.OutputCollector;
 import org.apache.hadoop.mapred.Reducer;
 import org.apache.hadoop.mapred.Reporter;
-import org.apache.mahout.utils.nlp.collocations.llr.Gram.Type;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -108,9 +107,6 @@
         reporter.incrCounter(Skipped.LESS_THAN_MIN_SUPPORT, 1);
         continue;
       }
-      if (subgramKey.getType() == Type.UNIGRAM) {
-        ngram.setType(subgramKey.getType());
-      }
       output.collect(ngram, subgramKey);
     }
   }

Modified: lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/Gram.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/Gram.java?rev=912189&r1=912188&r2=912189&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/Gram.java (original)
+++ lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/Gram.java Sat Feb 20 18:47:22 2010
@@ -17,14 +17,16 @@
 
 package org.apache.mahout.utils.nlp.collocations.llr;
 
-import static org.apache.mahout.utils.nlp.collocations.llr.Gram.Type.HEAD;
-
 import java.io.DataInput;
 import java.io.DataOutput;
 import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.charset.CharacterCodingException;
 
+import org.apache.hadoop.io.BinaryComparable;
 import org.apache.hadoop.io.Text;
 import org.apache.hadoop.io.WritableComparable;
+import org.apache.hadoop.io.WritableUtils;
 
 /**
  * Writable for holding data generated from the collocation discovery jobs. Depending on the job configuration
@@ -32,63 +34,56 @@
  * holds a part of an existing ngram (subgram). Tracks the frequency of the gram and its position in the ngram
  * in which is was found.
  */
-public class Gram implements WritableComparable<Gram> {
+public class Gram extends BinaryComparable implements WritableComparable<BinaryComparable> {
   
   public enum Type {
-    HEAD,
-    TAIL,
-    UNIGRAM
+    HEAD('h'),
+    TAIL('t'),
+    UNIGRAM('u'),
+    NGRAM('n');
+    
+    char x;
+    Type(char c) {
+      this.x = c;
+    }
   }
 
-  private String gram;
+  private byte[] bytes;
+  private int length;
   private int frequency;
-  private Type type;
   
   public Gram() {
 
   }
   
-  public Gram(Gram other) {
-    this.gram = other.gram;
-    this.frequency = other.frequency;
-    this.type = other.type;
-  }
-  
   /**
-   * Create an gram that is at the head of its text unit with a frequency of 1
+   * Copy constructor
    * 
-   * @param gram
-   *          the gram string
+   * @param other
    */
-  public Gram(String ngram) {
-    this(ngram, 1, HEAD);
+  public Gram(Gram other) {
+    frequency = other.frequency;
+    length = other.length;
+    bytes = new byte[length];
+    System.arraycopy(other.bytes, 0, bytes, 0, length);
   }
-  
+
   /**
    * Create an gram with a frequency of 1
    * 
    * @param gram
    *          the gram string
    * @param type
-   *          whether the gram is at the head of its text unit or tail or unigram
+   *          whether the gram is at the head or tail of its text unit or it is a unigram
    */
   public Gram(String ngram, Type type) {
     this(ngram, 1, type);
   }
   
+
   /**
-   * Create an gram with a frequency of 1
    * 
-   * @param gram
-   *          the gram string
-   * @param part
-   *          whether the gram is at the head of its text unit.
-   */
-  public Gram(String ngram, int frequency) {
-    this(ngram, frequency, HEAD);
-  }
-  
-  /**
+   * Create a gram with the specified frequency.
    * 
    * @param gram
    *          the gram string
@@ -98,41 +93,55 @@
    *          whether the gram is at the head of its text unit or tail or unigram
    */
   public Gram(String ngram, int frequency, Type type) {
-    this.gram = ngram;
+    
+    if (ngram == null) {
+      throw new NullPointerException();
+    }
+    
+    try {  
+      // extra character is used for storing type which is part 
+      // of the sort key.
+      ByteBuffer bb = Text.encode(ngram + "\0", true);
+      bytes = bb.array();
+      length = bb.limit();
+    }
+    catch (CharacterCodingException e) {
+      throw new RuntimeException("Should not have happened ",e); 
+    }
+    
+    encodeType(type, bytes, length-1);
     this.frequency = frequency;
-    this.type = type;
   }
   
+  
+  @Override
+  public byte[] getBytes() {
+    return bytes;
+  }
+
+  @Override
+  public int getLength() {
+    return length;
+  }
+
   /**
    * @return the gram is at the head of its text unit or tail or unigram.
    */
   public Type getType() {
-    return this.type;
-  }
-  
-  /**
-   * @param part
-   *          whether the gram is at the head of its text unit or tail or unigram
-   */
-  public void setType(Type type) {
-    this.type = type;
+    return decodeType(bytes, length-1);
   }
-  
+
   /**
    * @return gram term string
    */
   public String getString() {
-    return gram;
-  }
-  
-  /**
-   * @param gram
-   *          gram term string
-   */
-  public void setString(String str) {
-    this.gram = str;
+    try {
+      return Text.decode(bytes, 0, length-1);
+    } catch (CharacterCodingException e) {
+      throw new RuntimeException("Should not have happened " + e.toString()); 
+    }
   }
-  
+
   /**
    * @return gram frequency
    * @return
@@ -155,110 +164,77 @@
   
   @Override
   public void readFields(DataInput in) throws IOException {
-    frequency = in.readInt();
-    int typeValue = in.readUnsignedByte();
-    
-    if (typeValue == 0) {
-      type = Type.TAIL;
-    } else if (typeValue == 1) {
-      type = HEAD;
-    } else {
-      type = Type.UNIGRAM;
-    }
-    
-    Text data = new Text();
-    data.readFields(in);
-    gram = data.toString();
+    int newLength = WritableUtils.readVInt(in);
+    setCapacity(newLength, false);
+    in.readFully(bytes, 0, newLength);
+    int newFrequency = WritableUtils.readVInt(in);
+    length = newLength;
+    frequency = newFrequency;
   }
   
   @Override
   public void write(DataOutput out) throws IOException {
-    out.writeInt(frequency);
-    
-    if (type == Type.TAIL) {
-      out.writeByte(0);
-    } else if (type == HEAD) {
-      out.writeByte(1);
-    } else {
-      out.writeByte(2);
-    }
-    
-    Text data = new Text(gram);
-    data.write(out);
-  }
-  
-  @Override
-  public int compareTo(Gram other) {
-    int ret = getString().compareTo(other.getString());
-    if (ret != 0) {
-      return ret;
-    }
-    
-    if (this.type == Type.UNIGRAM && other.type != Type.UNIGRAM) {
-      return -1;
-    }
-    
-    if (this.type != Type.UNIGRAM && other.type == Type.UNIGRAM) {
-      return 1;
-    }
-    
-    if (this.type == HEAD && other.type != HEAD) {
-      return -1;
-    }
-    
-    if (this.type != HEAD && other.type == HEAD) {
-      return 1;
+    WritableUtils.writeVInt(out, length);
+    out.write(bytes, 0, length);
+    WritableUtils.writeVInt(out, frequency);
+  }
+
+  /* Cribbed from o.a.hadoop.io.Text:
+   * Sets the capacity of this object to <em>at least</em>
+   * <code>len</code> bytes. If the current buffer is longer,
+   * then the capacity and existing content of the buffer are
+   * unchanged. If <code>len</code> is larger
+   * than the current capacity, this object's capacity is
+   * increased to match.
+   * @param len the number of bytes we need
+   * @param keepData should the old data be kept
+   */
+  private void setCapacity(int len, boolean keepData) {
+    len+=1; // extra byte to hold type
+    if (bytes == null || bytes.length < len) {
+      byte[] newBytes = new byte[len];
+      if (bytes != null && keepData) {
+        System.arraycopy(bytes, 0, newBytes, 0, length);
+      }
+      bytes = newBytes;
     }
-    
-    return 0;
   }
-  
-  /** Generates hashcode, does not include frequency in the hash calculation */
-  @Override
-  public int hashCode() {
-    final int prime = 31;
-    int result = 1;
-    result = prime * result + (gram == null ? 0 : gram.hashCode());
-    result = prime * result + (type == null ? 0 : type.hashCode());
-    return result;
+
+  public String toString() {
+    return "'" + getString() + "'[" + getType().x + "]:" + frequency;
   }
   
-  /**
-   * Determines equality, does not include frequency in the equality calculation
-   */
-  @Override
-  public boolean equals(Object obj) {
-    if (this == obj) {
-      return true;
-    }
-    if (obj == null) {
-      return false;
-    }
-    if (getClass() != obj.getClass()) {
-      return false;
-    }
-    Gram other = (Gram) obj;
-    if (gram == null) {
-      if (other.gram != null) {
-        return false;
-      }
-    } else if (!gram.equals(other.gram)) {
-      return false;
-    }
-    if (type == null) {
-      if (other.type != null) {
-        return false;
-      }
-    } else if (!type.equals(other.type)) {
-      return false;
+  public static void encodeType(Type type, byte[] buf, int offset) {
+    switch (type) {
+      case HEAD:
+        buf[offset] = 0x1;
+        break;
+      case TAIL:
+        buf[offset] = 0x2; 
+        break;
+      case UNIGRAM:
+        buf[offset] = 0x3;
+        break;
+      case NGRAM:
+        buf[offset] = 0x4;
+        break;
+      default:
+        throw new IllegalStateException("switch/case problem in encodeType");  
     }
-    return true;
   }
   
-  @Override
-  public String toString() {
-    return "'" + gram + "'[" + (type == Type.UNIGRAM ? "u" : type == HEAD ? "h" : "t") + "]:"
-           + frequency;
+  public static Type decodeType(byte[] buf, int offset) {
+    switch (buf[offset]) {
+      case 0x1:
+        return Type.HEAD;
+      case 0x2:
+        return Type.TAIL;
+      case 0x3:
+        return Type.UNIGRAM;
+      case 0x4:
+        return Type.NGRAM;
+      default:
+        throw new IllegalStateException("switch/case problem in decodeType");
+    }
   }
-  
 }

Modified: lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/LLRReducer.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/LLRReducer.java?rev=912189&r1=912188&r2=912189&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/LLRReducer.java (original)
+++ lucene/mahout/trunk/utils/src/main/java/org/apache/mahout/utils/nlp/collocations/llr/LLRReducer.java Sat Feb 20 18:47:22 2010
@@ -28,7 +28,6 @@
 import org.apache.hadoop.mapred.Reducer;
 import org.apache.hadoop.mapred.Reporter;
 import org.apache.mahout.math.stats.LogLikelihood;
-import org.apache.mahout.utils.nlp.collocations.llr.Gram.Type;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -38,6 +37,7 @@
  */
 public class LLRReducer extends MapReduceBase implements Reducer<Gram,Gram,Text,DoubleWritable> {
   
+  /** Counter to track why a particlar entry was skipped */
   public enum Skipped {
     EXTRA_HEAD,
     EXTRA_TAIL,
@@ -45,7 +45,6 @@
     MISSING_TAIL,
     LESS_THAN_MIN_LLR,
     LLR_CALCULATION_ERROR,
-    UNIGRAM_COUNT
   }
 
   private static final Logger log = LoggerFactory.getLogger(LLRReducer.class);
@@ -115,7 +114,7 @@
     int[] gramFreq = new int[2];
     gramFreq[0] = gramFreq[1] = -1;
     
-    if (ngram.getType() == Type.UNIGRAM && emitUnigrams) {
+    if (ngram.getType() == Gram.Type.UNIGRAM && emitUnigrams) {
       DoubleWritable dd = new DoubleWritable(ngram.getFrequency());
       Text t = new Text(ngram.getString());
       output.collect(t, dd);
@@ -126,11 +125,11 @@
     while (values.hasNext()) {
       Gram value = values.next();
       
-      int pos = value.getType() == Type.HEAD ? 0 : 1;
+      int pos = value.getType() == Gram.Type.HEAD ? 0 : 1;
       
       if (gramFreq[pos] != -1) {
         log.warn("Extra {} for {}, skipping", value.getType(), ngram);
-        if (value.getType() == Type.HEAD) {
+        if (value.getType() == Gram.Type.HEAD) {
           reporter.incrCounter(Skipped.EXTRA_HEAD, 1);
         } else {
           reporter.incrCounter(Skipped.EXTRA_TAIL, 1);

Modified: lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/CollocMapperTest.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/CollocMapperTest.java?rev=912189&r1=912188&r2=912189&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/CollocMapperTest.java (original)
+++ lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/CollocMapperTest.java Sat Feb 20 18:47:22 2010
@@ -17,10 +17,6 @@
 
 package org.apache.mahout.utils.nlp.collocations.llr;
 
-import static org.apache.mahout.utils.nlp.collocations.llr.Gram.Type.HEAD;
-import static org.apache.mahout.utils.nlp.collocations.llr.Gram.Type.TAIL;
-import static org.apache.mahout.utils.nlp.collocations.llr.Gram.Type.UNIGRAM;
-
 import java.io.Reader;
 import java.util.Collections;
 
@@ -81,13 +77,13 @@
                                          {"t_of", "worst of"},};
     // set up expectations for mocks. ngram max size = 2
     for (String[] v : values) {
-      Type p = v[0].startsWith("h") ? HEAD : TAIL;
+      Type p = v[0].startsWith("h") ? Gram.Type.HEAD : Gram.Type.TAIL;
       int frequency = 1;
       if (v[1].equals("of times")) {
         frequency = 2;
       }
       Gram subgram = new Gram(v[0].substring(2), frequency, p);
-      Gram ngram = new Gram(v[1], frequency);
+      Gram ngram = new Gram(v[1], frequency, Type.NGRAM);
       collector.collect(subgram, ngram);
     }
     
@@ -135,15 +131,15 @@
                                          {"u_times", "times"},};
     // set up expectations for mocks. ngram max size = 2
     for (String[] v : values) {
-      Type p = v[0].startsWith("h") ? HEAD : TAIL;
-      p = v[0].startsWith("u") ? UNIGRAM : p;
+      Type p = v[0].startsWith("h") ? Gram.Type.HEAD : Gram.Type.TAIL;
+      p = v[0].startsWith("u") ? Gram.Type.UNIGRAM : p;
       int frequency = 1;
       if (v[1].equals("of times") || v[1].equals("of") || v[1].equals("times")
           || v[1].equals("the")) {
         frequency = 2;
       }
       Gram subgram = new Gram(v[0].substring(2), frequency, p);
-      Gram ngram = new Gram(v[1], frequency);
+      Gram ngram = new Gram(v[1], frequency, p == Gram.Type.UNIGRAM ? Gram.Type.UNIGRAM : Gram.Type.NGRAM);
       collector.collect(subgram, ngram);
     }
     

Modified: lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/CollocReducerTest.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/CollocReducerTest.java?rev=912189&r1=912188&r2=912189&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/CollocReducerTest.java (original)
+++ lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/CollocReducerTest.java Sat Feb 20 18:47:22 2010
@@ -20,6 +20,7 @@
 import static org.apache.mahout.utils.nlp.collocations.llr.Gram.Type.HEAD;
 import static org.apache.mahout.utils.nlp.collocations.llr.Gram.Type.TAIL;
 import static org.apache.mahout.utils.nlp.collocations.llr.Gram.Type.UNIGRAM;
+import static org.apache.mahout.utils.nlp.collocations.llr.Gram.Type.NGRAM;
 
 import java.util.LinkedList;
 import java.util.List;
@@ -51,16 +52,16 @@
     // input[*][1..n] are the values passed in via
     // the iterator.
     Gram[][] input = new Gram[][] {{new Gram("the", UNIGRAM), new Gram("the", UNIGRAM), new Gram("the", UNIGRAM)},
-                                   {new Gram("the", HEAD), new Gram("the best"), new Gram("the worst")},
-                                   {new Gram("of", HEAD), new Gram("of times"), new Gram("of times")},
-                                   {new Gram("times", TAIL), new Gram("of times"), new Gram("of times")}};
+                                   {new Gram("the", HEAD), new Gram("the best", NGRAM), new Gram("the worst", NGRAM)},
+                                   {new Gram("of", HEAD), new Gram("of times", NGRAM), new Gram("of times", NGRAM)},
+                                   {new Gram("times", TAIL), new Gram("of times", NGRAM), new Gram("of times", NGRAM)}};
     
     // expected results.
-    Gram[][] values = new Gram[][] {{new Gram("the", 4, UNIGRAM), new Gram("the", 2, UNIGRAM)},
-                                    {new Gram("the best", 1), new Gram("the", 2, HEAD)},
-                                    {new Gram("the worst", 1), new Gram("the", 2, HEAD)},
-                                    {new Gram("of times", 2), new Gram("of", 2, HEAD)},
-                                    {new Gram("of times", 2), new Gram("times", 2, TAIL)}};
+    Gram[][] values = new Gram[][] {{new Gram("the", 2, UNIGRAM), new Gram("the", 2, UNIGRAM)},
+                                    {new Gram("the best", 1, NGRAM), new Gram("the", 2, HEAD)},
+                                    {new Gram("the worst", 1, NGRAM), new Gram("the", 2, HEAD)},
+                                    {new Gram("of times", 2, NGRAM), new Gram("of", 2, HEAD)},
+                                    {new Gram("of times", 2, NGRAM), new Gram("times", 2, TAIL)}};
     
     // set up expectations
     for (Gram[] v : values) {

Modified: lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/GramTest.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/GramTest.java?rev=912189&r1=912188&r2=912189&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/GramTest.java (original)
+++ lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/GramTest.java Sat Feb 20 18:47:22 2010
@@ -17,52 +17,106 @@
 
 package org.apache.mahout.utils.nlp.collocations.llr;
 
-import static org.apache.mahout.utils.nlp.collocations.llr.Gram.Type.HEAD;
-import static org.apache.mahout.utils.nlp.collocations.llr.Gram.Type.TAIL;
-
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
 import java.util.HashMap;
 
 import junit.framework.Assert;
+import junit.framework.TestCase;
 
 import org.junit.Test;
 
 public class GramTest {
   
   @Test
+  public void testConstructorsGetters() {
+    Gram one = new Gram("foo", 2, Gram.Type.HEAD);
+    
+    Assert.assertEquals("foo", one.getString());
+    Assert.assertEquals(2, one.getFrequency());
+    Assert.assertEquals(Gram.Type.HEAD, one.getType());
+    
+    Gram oneClone = new Gram(one);
+    
+    Assert.assertEquals("foo", oneClone.getString());
+    Assert.assertEquals(2, oneClone.getFrequency());
+    Assert.assertEquals(Gram.Type.HEAD, oneClone.getType());
+    
+    Gram two = new Gram("foo", 3, Gram.Type.TAIL);
+    Assert.assertEquals(Gram.Type.TAIL, two.getType());
+    
+    Gram three = new Gram("foo", 4, Gram.Type.UNIGRAM);
+    Assert.assertEquals(Gram.Type.UNIGRAM, three.getType());
+    
+    Gram four = new Gram("foo", 5, Gram.Type.NGRAM);
+    Assert.assertEquals(Gram.Type.NGRAM, four.getType());
+   
+    try {
+      new Gram(null, 4, Gram.Type.UNIGRAM);
+      Assert.fail("expected exception");
+    }
+    catch (NullPointerException ex) {
+      /* ok */
+    }
+   
+    
+    try {
+      new Gram("foo", 4, null);
+      Assert.fail("expected exception");
+    }
+    catch (NullPointerException ex) {
+      /* ok */
+    }
+  }
+  
+  @Test
   public void testEquality() {
-    Gram one = new Gram("foo", 2, HEAD);
-    Gram two = new Gram("foo", 3, HEAD);
+    Gram one = new Gram("foo", 2, Gram.Type.HEAD);  
+    Gram two = new Gram("foo", 3, Gram.Type.HEAD);
     
     Assert.assertTrue(one.equals(two));
     Assert.assertTrue(two.equals(one));
     
-    Gram three = new Gram("foo", 4, TAIL);
-    Gram four = new Gram("foo");
+    Gram three = new Gram("foo", 4, Gram.Type.TAIL);
+    Gram four = new Gram("foo", Gram.Type.UNIGRAM);
     
     Assert.assertTrue(!three.equals(two));
-    Assert.assertTrue(four.equals(one));
-    Assert.assertTrue(one.equals(four));
+    Assert.assertTrue(!four.equals(one));
+    Assert.assertTrue(!one.equals(four));
+    
+    Gram five = new Gram("foo", 5, Gram.Type.UNIGRAM);
+    
+    Assert.assertTrue(four.equals(five));
+    
+    Gram six = new Gram("foo", 6, Gram.Type.NGRAM);
+    Gram seven = new Gram("foo", 7, Gram.Type.NGRAM);
     
-    Gram five = new Gram("foobar", 4, TAIL);
+    Assert.assertTrue(!five.equals(six));
+    Assert.assertTrue(six.equals(seven));
     
-    Assert.assertTrue(!five.equals(four));
-    Assert.assertTrue(!five.equals(three));
-    Assert.assertTrue(!five.equals(two));
-    Assert.assertTrue(!five.equals(one));
+    Gram eight = new Gram("foobar", 4, Gram.Type.TAIL);
+    
+    Assert.assertTrue(!eight.equals(four));
+    Assert.assertTrue(!eight.equals(three));
+    Assert.assertTrue(!eight.equals(two));
+    Assert.assertTrue(!eight.equals(one));
   }
   
   @Test
   public void testHashing() {
     Gram[] input =
     {
-     new Gram("foo", 2, HEAD),
-     new Gram("foo", 3, HEAD),
-     new Gram("foo", 4, TAIL),
-     new Gram("foo", 5, TAIL),
-     new Gram("bar", 6, HEAD),
-     new Gram("bar", 7, TAIL),
-     new Gram("bar", 8),
-     new Gram("bar")
+     new Gram("foo", 2, Gram.Type.HEAD),
+     new Gram("foo", 3, Gram.Type.HEAD),
+     new Gram("foo", 4, Gram.Type.TAIL),
+     new Gram("foo", 5, Gram.Type.TAIL),
+     new Gram("bar", 6, Gram.Type.HEAD),
+     new Gram("bar", 7, Gram.Type.TAIL),
+     new Gram("bar", 8, Gram.Type.NGRAM),
+     new Gram("bar", Gram.Type.UNIGRAM)
     };
     
     HashMap<Gram,Gram> map = new HashMap<Gram,Gram>();
@@ -81,7 +135,7 @@
                   3,
                   9,
                   5,
-                  15,
+                  6,
                   7,
                   8,
                   1
@@ -95,8 +149,8 @@
                       false,
                       true,
                       true,
-                      false,
-                      false
+                      true,
+                      true
     };
     
     for (int i = 0; i < input.length; i++) {
@@ -105,4 +159,35 @@
       Assert.assertEquals(memb[i], input[i] == map.get(input[i]));
     }
   }
+  
+ @Test
+ public void testWritable() throws IOException {
+   Gram one = new Gram("foo", 2, Gram.Type.HEAD);
+   Gram two = new Gram("foobar", 3, Gram.Type.UNIGRAM);
+   
+   TestCase.assertEquals("foo", one.getString());
+   TestCase.assertEquals(2, one.getFrequency());
+   TestCase.assertEquals(Gram.Type.HEAD, one.getType());
+   
+   TestCase.assertEquals("foobar", two.getString());
+   TestCase.assertEquals(3, two.getFrequency());
+   TestCase.assertEquals(Gram.Type.UNIGRAM, two.getType());
+   
+   ByteArrayOutputStream bout = new ByteArrayOutputStream();
+   DataOutputStream out = new DataOutputStream(bout);
+   
+   two.write(out);
+   
+   byte[] b = bout.toByteArray();
+   
+   ByteArrayInputStream bin = new ByteArrayInputStream(b);
+   DataInputStream din = new DataInputStream(bin);
+   
+   one.readFields(din);
+   
+   TestCase.assertEquals("foobar", one.getString());
+   TestCase.assertEquals(3, one.getFrequency());
+   TestCase.assertEquals(Gram.Type.UNIGRAM, one.getType());
+   
+ }
 }

Modified: lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/LLRReducerTest.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/LLRReducerTest.java?rev=912189&r1=912188&r2=912189&view=diff
==============================================================================
--- lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/LLRReducerTest.java (original)
+++ lucene/mahout/trunk/utils/src/test/java/org/apache/mahout/utils/nlp/collocations/llr/LLRReducerTest.java Sat Feb 20 18:47:22 2010
@@ -19,6 +19,7 @@
 
 import static org.apache.mahout.utils.nlp.collocations.llr.Gram.Type.HEAD;
 import static org.apache.mahout.utils.nlp.collocations.llr.Gram.Type.TAIL;
+import static org.apache.mahout.utils.nlp.collocations.llr.Gram.Type.NGRAM;
 
 import java.io.IOException;
 import java.util.LinkedList;
@@ -89,12 +90,12 @@
     
     
     Gram[][] input = {
-                      {new Gram("the best",  1), new Gram("the",   2, HEAD), new Gram("best",  1, TAIL) },
-                      {new Gram("best of",   1), new Gram("best",  1, HEAD), new Gram("of",    2, TAIL) },
-                      {new Gram("of times",  2), new Gram("of",    2, HEAD), new Gram("times", 2, TAIL) },
-                      {new Gram("times the", 1), new Gram("times", 1, HEAD), new Gram("the",   1, TAIL) },
-                      {new Gram("the worst", 1), new Gram("the",   2, HEAD), new Gram("worst", 1, TAIL) },
-                      {new Gram("worst of",  1), new Gram("worst", 1, HEAD), new Gram("of",    2, TAIL) }
+                      {new Gram("the best",  1, NGRAM), new Gram("the",   2, HEAD), new Gram("best",  1, TAIL) },
+                      {new Gram("best of",   1, NGRAM), new Gram("best",  1, HEAD), new Gram("of",    2, TAIL) },
+                      {new Gram("of times",  2, NGRAM), new Gram("of",    2, HEAD), new Gram("times", 2, TAIL) },
+                      {new Gram("times the", 1, NGRAM), new Gram("times", 1, HEAD), new Gram("the",   1, TAIL) },
+                      {new Gram("the worst", 1, NGRAM), new Gram("the",   2, HEAD), new Gram("worst", 1, TAIL) },
+                      {new Gram("worst of",  1, NGRAM), new Gram("worst", 1, HEAD), new Gram("of",    2, TAIL) }
     };
     
     int[][] expectations = {