leila.parsing
Class NumberParser

java.lang.Object
  extended by leila.parsing.NumberParser

public class NumberParser
extends java.lang.Object

This class is part of LEILA (http://mpii.de/yago-naga/leila). It is licensed under the Creative Commons Attribution License (http://creativecommons.org/licenses/by/3.0) by the author Fabian M. Suchanek (http://suchanek.name).

The NumberParser normalizes number expressions in English natural language text. It can work with expressions like

         10 million meters
         7 inches
         2.3 sq ft
         12:30 pm
         100 km
         12 ml
         10 Mb
   
Example:
         System.out.println(NumberParser.normalize("It was 1.2 inches long"));
         --> "It was 0.030479999999999997 meters long"

         System.out.println(toLong("more than ten kB"));
         --> 10000.0
   


Field Summary
static java.lang.String ANYNUMBER
          The universal unspecified normalized number
static double EPSILON
          Two numbers match if their quotient is inside 1+-EPSILON
static java.util.Map<java.lang.String,java.lang.Double> prefixes
          Maps decimal prefixes (like "giga") to their double
 
Constructor Summary
NumberParser()
           
 
Method Summary
static java.lang.String getNumber(java.lang.String d)
          Extracts the pure number from a String containing a normalized number, else ""
static boolean isInt(java.lang.String s)
          Tells whether this string is a normalized integer number
static boolean isNumber(java.lang.String s)
          Tells whether this string is a normalized number
static void main(java.lang.String[] argv)
          Test method
static boolean match(java.lang.String d1, java.lang.String d2)
          Tells whether two normalized numbers are the same (within EPSILON).
static java.lang.String newNumber(double d, java.lang.String type)
          Creates a normalized number from a double and a type.
static java.lang.String newNumber(java.lang.String n)
          Creates a normalized number without a type.
static java.lang.String newNumber(java.lang.String n, java.lang.String type)
          Creates a normalized number from a number and a type.
protected static java.lang.String newNumberNoPl(java.lang.String n, java.lang.String type)
          Creates a normalized number without isPlural form
static java.lang.String normalize(java.lang.String s)
          Normalizes all numbers in a String
static java.lang.StringBuilder normalize(java.lang.StringBuilder s)
          Normalizes all numbers in a StringBuilder Convenience method for the more efficient String-method
static double toDouble(java.lang.String d)
          Converts a String that contains a (possibly non-normalized) number to a double
static long toLong(java.lang.String d)
          Converts a String that contains a (possibly non-normalized) number to a long
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EPSILON

public static final double EPSILON
Two numbers match if their quotient is inside 1+-EPSILON

See Also:
Constant Field Values

prefixes

public static final java.util.Map<java.lang.String,java.lang.Double> prefixes
Maps decimal prefixes (like "giga") to their double


ANYNUMBER

public static final java.lang.String ANYNUMBER
The universal unspecified normalized number

Constructor Detail

NumberParser

public NumberParser()
Method Detail

newNumber

public static final java.lang.String newNumber(java.lang.String n,
                                               java.lang.String type)
Creates a normalized number from a number and a type. In the current implementation, the normalized number is just a string of the number, followed by the type in isPlural.


newNumberNoPl

protected static final java.lang.String newNumberNoPl(java.lang.String n,
                                                      java.lang.String type)
Creates a normalized number without isPlural form


newNumber

public static final java.lang.String newNumber(java.lang.String n)
Creates a normalized number without a type.

See Also:
newNumber(String n, String type)

newNumber

public static final java.lang.String newNumber(double d,
                                               java.lang.String type)
Creates a normalized number from a double and a type.

See Also:
newNumber(String n, String type)

isNumber

public static boolean isNumber(java.lang.String s)
Tells whether this string is a normalized number


isInt

public static boolean isInt(java.lang.String s)
Tells whether this string is a normalized integer number


normalize

public static java.lang.String normalize(java.lang.String s)
Normalizes all numbers in a String


normalize

public static java.lang.StringBuilder normalize(java.lang.StringBuilder s)
Normalizes all numbers in a StringBuilder Convenience method for the more efficient String-method


match

public static boolean match(java.lang.String d1,
                            java.lang.String d2)
Tells whether two normalized numbers are the same (within EPSILON). Any characters preceding or succeeding the numbers are ignored.


getNumber

public static java.lang.String getNumber(java.lang.String d)
Extracts the pure number from a String containing a normalized number, else ""


toDouble

public static double toDouble(java.lang.String d)
Converts a String that contains a (possibly non-normalized) number to a double


toLong

public static long toLong(java.lang.String d)
Converts a String that contains a (possibly non-normalized) number to a long


main

public static void main(java.lang.String[] argv)
                 throws java.lang.Exception
Test method

Throws:
java.lang.Exception