stanfordnlp/CoreNLP · error · NumberFormatException

Error in wordToNumber function.

Error message

Error in wordToNumber function.

What it means

NumberNormalizer.wordToNumber throws NumberFormatException("Error in wordToNumber function.") when a piece matches an ordinal word map (like 'third') but is not the last piece of the string — an unsupported composition, e.g. 'third fifty'. Ordinal words are only allowed as the final token.

Solutions

  1. Normalize the phrase to valid English numeric ordering before parsing ('fifth hundred' -> 'five hundred').
  2. Catch NumberFormatException and skip normalization for that token.
  3. Split the mention so ordinal pieces are passed standalone as the last/only piece.

Example fix

// before
Number n = NumberNormalizer.wordToNumber("twentieth five");
// after
Number n = NumberNormalizer.wordToNumber("twenty five"); // ordinal word not mid-string
Defensive patterns

Strategy: try-catch

Validate before calling

static boolean ordinalWordIsFinal(String phrase) {
  String[] parts = phrase.split("[ -]");
  for (int i = 0; i < parts.length - 1; i++) {
    if (parts[i].matches(".*(th|first|second|third|st|nd|rd)$")) return false;
  }
  return true;
}

Try / catch

try {
  Number n = NumberNormalizer.wordToNumber(phrase);
} catch (NumberFormatException e) {
  if (e.getMessage().contains("Error in wordToNumber")) { /* reorder or skip */ }
}

Prevention

When it happens

Trigger: Calling wordToNumber with a multi-part string where an ordinal word (e.g. 'hundredth', 'third') appears before other numeric pieces, like 'hundredth twelve'.

Common situations: Feeding compound or reversed numeric phrases from entity mentions (e.g. 'twentieth five') into the normalizer; such phrasings are grammatically invalid so the parser rejects them.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/d9ad1689ee1b974f. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/ie/NumberNormalizer.java:366

    // now count words
    String[] fields = wsPattern.split(str);
    Number[] numFields = new Number[fields.length];
    int numWords = fields.length;

    // get numeric value of each word piece
    for (int curIndex = 0; curIndex < numWords; curIndex++) {
      String curPart = fields[curIndex] == null ? "" : fields[curIndex].replaceAll(whitespaceCharsRegex + "+", "").trim();
      Matcher m = alphaPattern.matcher(curPart);
      if (m.find()) {
        // Some part of the word has alpha characters
        Number curNum;
        if (word2NumMap.containsKey(curPart)) {
          curNum = word2NumMap.get(curPart);
        } else if (ordWord2NumMap.containsKey(curPart)) {
          if (curIndex == numWords-1){
            curNum = ordWord2NumMap.get(curPart);
          } else {
            throw new NumberFormatException("Error in wordToNumber function.");
          }
        } else if (curIndex > 0 && (curPart.endsWith("ths") || curPart.endsWith("rds"))) {
          // Fractions?
          curNum = ordWord2NumMap.get(curPart.substring(0, curPart.length()-1));
          if (curNum != null) {
            curNum = 1/curNum.doubleValue();
          } else {
            throw new NumberFormatException("Bad number put into wordToNumber.  Word is: \"" + curPart + "\", originally part of \"" + originalString + "\", piece # " + curIndex);
          }
        } else if (Character.isDigit(curPart.charAt(0)) || curPart.charAt(0) == '.') {
          if (curPart.endsWith("th") || curPart.endsWith("rd") || curPart.endsWith("nd") || curPart.endsWith("st")) {
            curPart = curPart.substring(0, curPart.length()-2).trim();
          }
          curNum = parseNumberPart(curPart, originalString, curIndex);
        } else {
          throw new NumberFormatException("Bad number put into wordToNumber.  Word is: \"" + curPart + "\", originally part of \"" + originalString + "\", piece # " + curIndex);
        }
        numFields[curIndex] = curNum;

View on GitHub (pinned to 1b7edd19c4)