javatools.parsers
Class NumberParser

java.lang.Object
  extended by javatools.parsers.NumberParser

public class NumberParser
extends java.lang.Object

This class is part of the Java Tools (see http://mpii.de/yago-naga/javatools). It is licensed under the Creative Commons Attribution License (see http://creativecommons.org/licenses/by/3.0) by the YAGO-NAGA team (see http://mpii.de/yago-naga). 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#meter long"
          
          System.out.println(toLong("more than ten kB"));
          --> 10000.0
 


Field Summary
static java.lang.String FLOAT
          A number as a capturing RegEx
static java.lang.String INT
          An integer as a capturing regex
static java.util.regex.Pattern NUMBERPATTERN
          The number pattern
static java.lang.String POSINT
          An integer as a capturing regex
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 boolean different(java.lang.String n1, java.lang.String n2)
          TRUE if the numbers differ in the unit and or by more than 10%
static java.lang.Double getDouble(java.lang.CharSequence d)
          Converts a String that contains a (non-normalized) number to a double or null
static java.lang.Integer getInt(java.lang.CharSequence d)
          Converts a String that contains a (non-normalized) number to a int or null
static java.lang.Long getLong(java.lang.CharSequence d)
          Converts a String that contains a (non-normalized) number to a long or null
static java.lang.String getNumber(java.lang.CharSequence d)
          Extracts the pure number from a String containing a normalized number, else null
static java.lang.String[] getNumberAndUnit(java.lang.CharSequence d, int[] pos)
          Extracts the number and its unit from a String containing a normalized number, else null, returns start and end position in pos[0] and pos[1] resp.
static java.util.List<java.lang.String> getNumbers(java.lang.CharSequence d)
          Extracts the numbers and units from a normalized String
static boolean isFloat(java.lang.String s)
          Tells whether this string is a normalized number
static boolean isInt(java.lang.String s)
          Tells whether this string is a normalized integer number
static boolean isNonNegativeInt(java.lang.String s)
          Tells whether this string is a normalized non-negative integer number
static boolean isNumberAndUnit(java.lang.String s)
          Tells whether this string is a normalized number with unit
static void main(java.lang.String[] argv)
          Test method
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
static java.lang.String normalize(java.lang.CharSequence s)
          Normalizes all numbers in a String Note: If you fix something in this version, please try to apply the same fix at the position change tracking function below
static java.lang.String normalize(java.lang.CharSequence s, PositionTracker posTracker)
          Normalizes all numbers in a String and updates a position mapping with the introduced pos changes Note: If you fix something in this version, please try to apply the same fix at the non-tracking function above
static java.lang.Double parseDouble(java.lang.String d)
          Calls Double.parseDouble, returns an Integer or NULL
static java.lang.Integer parseInt(java.lang.String d)
          Calls Integer.parseInt, returns an Integer or NULL
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

prefixes

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


FLOAT

public static final java.lang.String FLOAT
A number as a capturing RegEx

See Also:
Constant Field Values

POSINT

public static final java.lang.String POSINT
An integer as a capturing regex

See Also:
Constant Field Values

INT

public static final java.lang.String INT
An integer as a capturing regex

See Also:
Constant Field Values

NUMBERPATTERN

public static final java.util.regex.Pattern NUMBERPATTERN
The number pattern

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


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)

isFloat

public static boolean isFloat(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


isNonNegativeInt

public static boolean isNonNegativeInt(java.lang.String s)
Tells whether this string is a normalized non-negative integer number


isNumberAndUnit

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


normalize

public static java.lang.String normalize(java.lang.CharSequence s)
Normalizes all numbers in a String Note: If you fix something in this version, please try to apply the same fix at the position change tracking function below


normalize

public static java.lang.String normalize(java.lang.CharSequence s,
                                         PositionTracker posTracker)
Normalizes all numbers in a String and updates a position mapping with the introduced pos changes Note: If you fix something in this version, please try to apply the same fix at the non-tracking function above


getNumber

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


getNumberAndUnit

public static java.lang.String[] getNumberAndUnit(java.lang.CharSequence d,
                                                  int[] pos)
Extracts the number and its unit from a String containing a normalized number, else null, returns start and end position in pos[0] and pos[1] resp.


getNumbers

public static java.util.List<java.lang.String> getNumbers(java.lang.CharSequence d)
Extracts the numbers and units from a normalized String


getDouble

public static java.lang.Double getDouble(java.lang.CharSequence d)
Converts a String that contains a (non-normalized) number to a double or null


getLong

public static java.lang.Long getLong(java.lang.CharSequence d)
Converts a String that contains a (non-normalized) number to a long or null


getInt

public static java.lang.Integer getInt(java.lang.CharSequence d)
Converts a String that contains a (non-normalized) number to a int or null


parseInt

public static java.lang.Integer parseInt(java.lang.String d)
Calls Integer.parseInt, returns an Integer or NULL


parseDouble

public static java.lang.Double parseDouble(java.lang.String d)
Calls Double.parseDouble, returns an Integer or NULL


different

public static boolean different(java.lang.String n1,
                                java.lang.String n2)
TRUE if the numbers differ in the unit and or by more than 10%


main

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

Throws:
java.lang.Exception