Class Bigram

java.lang.Object
textprocessing.Word
textprocessing.Bigram
All Implemented Interfaces:
Comparable<Word>

public class Bigram extends Word
A class that represents a bigram
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final List<Long>
     
    private int
     
    private final String
     

    Fields inherited from class textprocessing.Word

    word
  • Constructor Summary

    Constructors
    Constructor
    Description
    Bigram(BasicWord word1, BasicWord word2)
    Constructor for a Bigram
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addLocation(long location)
    Adds a new locations to an existing Bigram and increments the number of occurrences.
    int
    Compares this object with the specified object for order.
    boolean
    Compares another Object to check if that Object is equal to this Bigram.
    Generates a String representation of the Bigram that contains both words of the Bigram and the number of occurrences of the Bigram.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • word2

      private final String word2
    • occurrences

      private int occurrences
    • locations

      private final List<Long> locations
  • Constructor Details

    • Bigram

      public Bigram(BasicWord word1, BasicWord word2)
      Constructor for a Bigram
      Parameters:
      word1 - the first Word in the Bigram
      word2 - the second Word in the Bigram
  • Method Details

    • addLocation

      public void addLocation(long location) throws IllegalArgumentException
      Adds a new locations to an existing Bigram and increments the number of occurrences. If the location already has been added, or the location is not a valid location (i.e. negative), an exception is thrown.
      Specified by:
      addLocation in class Word
      Parameters:
      location - a location of the bigram
      Throws:
      IllegalArgumentException - thrown if the location already exists or is invalid
    • toString

      public String toString()
      Generates a String representation of the Bigram that contains both words of the Bigram and the number of occurrences of the Bigram. Example:
      the        penguin      18
      Specified by:
      toString in class Word
      Returns:
      a String representation of the Bigram
    • equals

      public boolean equals(Object o)
      Compares another Object to check if that Object is equal to this Bigram. Equality is measured by whether the other Object is also a Bigram and both words contained in this Bigram matches exactly the words contained in the other Bigram object in the same order.
      Specified by:
      equals in class Word
      Parameters:
      o - the Object to compare to this
      Returns:
      true if o is both a Bigram and contains the same words in the same order as this Bigram, false otherwise
    • compareTo

      public int compareTo(Word that)
      Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
      For this class, we are comparing the number of occurrences of the two Bigrams.
      Parameters:
      that - the object to be compared.
      Returns:
      a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.