opennlp.tools.util.Span.java Source code

Java tutorial

Introduction

Here is the source code for opennlp.tools.util.Span.java

Source

    ///////////////////////////////////////////////////////////////////////////////
    // Copyright (C) 2002 Tom Morton
    // 
    // This library is free software; you can redistribute it and/or
    // modify it under the terms of the GNU Lesser General Public
    // License as published by the Free Software Foundation; either
    // version 2.1 of the License, or (at your option) any later version.
    // 
    // This library is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU Lesser General Public License for more details.
    // 
    // You should have received a copy of the GNU Lesser General Public
    // License along with this program; if not, write to the Free Software
    // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    //////////////////////////////////////////////////////////////////////////////

    //package opennlp.tools.util;

/** 
 * Class for storing start and end integer offsets.  
**/
public class Span implements Comparable {

  private int start;
  private int end;

  /** 
   * Initializes a new Span Object.
   * 
   * @param s start of span.
   * @param e end of span.
   */
  public Span(int s,int e) {
    start=s;
    end=e;
  }
  
  /** 
   * Return the start of a span.
   * 
   * @return the start of a span.
   **/
  public int getStart() {
    return start;
  }
  
  /** 
   * Return the end of a span.
   * 
   * @return the end of a span.
   **/
  public int getEnd() {
    return end;
  }

  /** 
   * Returns the length of this span.
   * 
   * @return the length of the span.
   */
  public int length() {
    return end-start;
  }
  
  /**
   * Returns true if the specified span is contained by this span.  
   * Identical spans are considered to contain each other. 
   * 
   * @param s The span to compare with this span.
   * 
   * @return true is the specified span is contained by this span; 
   * false otherwise.
   */
  public boolean contains(Span s) {
    return start <= s.getStart() && s.getEnd() <= end;
  }
  
  public boolean contains(int index) {
    return start <= index && index <= end;
  }
  
  /**
   * Returns true if the specified span is the begin of this span and the
   * specified span is contained in this span.
   * 
   * @param s The span to compare with this span.
   * 
   * @return true if the specified span starts with this span and is
   * contained in this span; false otherwise
   */
  public boolean startsWith(Span s) {
    return getStart() == s.getStart() && contains(s);
  }
  
  /**
   * Returns true if the specified span intersects with this span.
   * 
   * @param s The span to compare with this span. 
   * 
   * @return true is the spans overlap; false otherwise. 
   */
  public boolean intersects(Span s) {
    int sstart = s.getStart();
    //either s's start is in this or this' start is in s
    return this.contains(s) || s.contains(this) || 
      getStart() <= sstart && sstart < getEnd() ||
      sstart <= getStart() && getStart() < s.getEnd();
  }
  
  /**
   * Returns true is the specified span crosses this span.
   * 
   * @param s The span to compare with this span.
   * 
   * @return true is the specified span overlaps this span and contains a 
   * non-overlapping section; false otherwise.
   */
  public boolean crosses(Span s) {
    int sstart = s.getStart();
    //either s's start is in this or this' start is in s
    return !this.contains(s) && !s.contains(this) && 
      (getStart() <= sstart && sstart < getEnd() ||
      sstart <= getStart() && getStart() < s.getEnd());
  }
  
  /**
   * Retrieves the string covered by the current span of the specified text.
   * 
   * @param text
   * 
   * @return the substring covered by the current span
   */
  public String getCoveredText(String text) {
    if (getEnd() > text.length()) {
      throw new IllegalArgumentException("The span " + toString() + 
          " is outside the given text!");
    }
    
    return text.substring(getStart(), getEnd());
  }
  
  /**
   * Compares the specified span to the current span.
   */
  public int compareTo(Object o) { 
    Span s = (Span) o;
    if (getStart() < s.getStart()) {
      return -1;
    }
    else if (getStart() == s.getStart()) {
      if (getEnd() > s.getEnd()) {
        return -1;
      }
      else if (getEnd() < s.getEnd()) {
        return 1;
      }
      else {
        return 0;
      }
    }
    else {
      return 1;
    }
  }

  /**
   * Generates a hash code of the current span.
   */
  public int hashCode() {
    return this.start << 16 | 0x0000FFFF | this.end;
  }
  
  /**
   * Checks if the specified span is equal to the current span.
   */
  public boolean equals(Object o) {

    boolean result;
    
    if (o == this) {
      result = true;
    }
    else if (o instanceof Span) {
      Span s = (Span) o;
      
      result = getStart() == s.getStart() && getEnd() == s.getEnd();
    }
    else {
      result = false;
    }
    
    return result;
  }
  
  /**
   * Generates a human readable string.
   */
  public String toString() {
    StringBuffer toStringBuffer = new StringBuffer(15);
    toStringBuffer.append(getStart());
    toStringBuffer.append("..");
    toStringBuffer.append(getEnd());
    
    return toStringBuffer.toString();
  }
  
  /**
   * Converts an array of {@link Span}s to an array of {@link String}s. 
   * 
   * @param spans
   * @param s
   * @return the strings
   */
  public static String[] spansToStrings(Span[] spans, String s) {
    String[] tokens = new String[spans.length];
    
    for (int si = 0, sl = spans.length; si < sl; si++) {
      tokens[si] = spans[si].getCoveredText(s);
    }
    
    return tokens;
  }
  
  public static String[] spansToStrings(Span[] spans, String[] tokens) {
    String[] chunks = new String[spans.length];
    StringBuffer cb = new StringBuffer();
    for (int si = 0, sl = spans.length; si < sl; si++) {
      cb.setLength(0);
      for (int ti=spans[si].getStart();ti<spans[si].getEnd();ti++) {
        cb.append(tokens[ti]).append(" ");
      }
      chunks[si]=cb.substring(0, cb.length()-1);
    }
    return chunks;
  }
}
    ///////////////////////////////////////////////////////////////////////////////
    //Copyright (C) 2008 OpenNlp
    //
    //This library is free software; you can redistribute it and/or
    //modify it under the terms of the GNU Lesser General Public
    //License as published by the Free Software Foundation; either
    //version 2.1 of the License, or (at your option) any later version.
    //
    //This library is distributed in the hope that it will be useful,
    //but WITHOUT ANY WARRANTY; without even the implied warranty of
    //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    //GNU Lesser General Public License for more details.
    //
    //You should have received a copy of the GNU Lesser General Public
    //License along with this program; if not, write to the Free Software
    //Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    //////////////////////////////////////////////////////////////////////////////

    package opennlp.tools.util;

    import junit.framework.Assert;
    import junit.framework.TestCase;

    /**
    * Tests for the {@link Span} class.
    */
    public class SpanTest extends TestCase {

        /**
         * Test for {@link Span#getStart()}.
         */
        public void testGetStart() {
            Assert.assertEquals(5, new Span(5, 6).getStart());
        }

        /**
         * Test for {@link Span#getEnd()}.
         */
        public void testGetEnd() {
            Assert.assertEquals(6, new Span(5, 6).getEnd());
        }

        /**
         * Test for {@link Span#length()}.
         */
        public void testLength() {
            Assert.assertEquals(11, new Span(10, 21).length());
        }

        /**
         * Test for {@link Span#contains(Span)}.
         */
        public void testContains() {
            Span a = new Span(500, 900);
            Span b = new Span(520, 600);

            Assert.assertEquals(true, a.contains(b));
        }

        /**
         * Test for {@link Span#contains(Span)}.
         */
        public void testContainsWithEqual() {
            Span a = new Span(500, 900);

            Assert.assertEquals(true, a.contains(a));
        }

        /**
         * Test for {@link Span#contains(Span)}.
         */
        public void testContainsWithLowerIntersect() {
            Span a = new Span(500, 900);
            Span b = new Span(450, 1000);

            Assert.assertEquals(false, a.contains(b));
        }

        /**
         * Test for {@link Span#contains(Span)}.
         */
        public void testContainsWithHigherIntersect() {
            Span a = new Span(500, 900);
            Span b = new Span(500, 1000);

            Assert.assertEquals(false, a.contains(b));
        }

        /**
         * Test for {@link Span#contains(int)}.
         */
        public void testContainsInt() {
            Span a = new Span(10, 300);

            Assert.assertFalse(a.contains(9));
            Assert.assertTrue(a.contains(10));
            Assert.assertTrue(a.contains(200));
            Assert.assertTrue(a.contains(300));
            Assert.assertFalse(a.contains(301));
        }

        /**
         * Test for {@link Span#startsWith(Span)}.
         */
        public void testStartsWith() {
            Span a = new Span(10, 50);
            Span b = new Span(10, 12);

            Assert.assertTrue(a.startsWith(a));

            Assert.assertTrue(a.startsWith(b));

            Assert.assertFalse(b.startsWith(a));

        }

        /**
         * Test for {@link Span#intersects(Span)}.
         */
        public void testIntersects() {
            Span a = new Span(10, 50);
            Span b = new Span(40, 100);

            Assert.assertTrue(a.intersects(b));
            Assert.assertTrue(b.intersects(a));

            Span c = new Span(10, 20);
            Span d = new Span(40, 50);

            Assert.assertFalse(c.intersects(d));
            Assert.assertFalse(d.intersects(c));

            Assert.assertTrue(b.intersects(d));
        }

        /**
         * Test for {@link Span#crosses(Span)}.
         */
        public void testCrosses() {
            Span a = new Span(10, 50);
            Span b = new Span(40, 100);

            Assert.assertTrue(a.crosses(b));
            Assert.assertTrue(b.crosses(a));

            Span c = new Span(10, 20);
            Span d = new Span(40, 50);

            Assert.assertFalse(c.crosses(d));
            Assert.assertFalse(d.crosses(c));

            Assert.assertFalse(b.crosses(d));
        }

        /**
         * Test for {@link Span#compareTo(Object)}.
         */
        public void testCompareToLower() {
            Span a = new Span(100, 1000);
            Span b = new Span(10, 50);

            Assert.assertEquals(true, a.compareTo(b) > 0);
        }

        /**
         * Test for {@link Span#compareTo(Object)}.
         */
        public void testCompareToHigher() {
            Span a = new Span(100, 200);
            Span b = new Span(300, 400);

            Assert.assertEquals(true, a.compareTo(b) < 0);
        }

        /**
         * Test for {@link Span#compareTo(Object)}.
         */
        public void testCompareToEquals() {
            Span a = new Span(30, 1000);
            Span b = new Span(30, 1000);

            Assert.assertEquals(true, a.compareTo(b) == 0);
        }

        /**
         * Test for {@link Span#hashCode()}.
         */
        public void testhHashCode() {
            Assert.assertEquals(new Span(10, 11), new Span(10, 11));
        }

        /**
         * Test for {@link Span#equals(Object)}.
         */
        public void testEqualsWithNull() {
            Span a = new Span(0, 0);

            Assert.assertEquals(a.equals(null), false);
        }

        /**
         * Test for {@link Span#equals(Object)}.
         */
        public void testEquals() {
            Span a = new Span(100, 1000);
            Span b = new Span(100, 1000);

            Assert.assertEquals(a.equals(b), true);
        }

        /**
         * Test for {@link Span#toString()}.
         */
        public void testToString() {
            new Span(50, 100).toString();
        }
    }