{"record":{"id":"d9ad1689ee1b974f","repo":"stanfordnlp/CoreNLP","slug":"error-in-wordtonumber-function","errorCode":null,"errorMessage":"Error in wordToNumber function.","messagePattern":"Error in wordToNumber function\\.","errorType":"exception","errorClass":"NumberFormatException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ie/NumberNormalizer.java","lineNumber":366,"sourceCode":"    // now count words\n    String[] fields = wsPattern.split(str);\n    Number[] numFields = new Number[fields.length];\n    int numWords = fields.length;\n\n    // get numeric value of each word piece\n    for (int curIndex = 0; curIndex < numWords; curIndex++) {\n      String curPart = fields[curIndex] == null ? \"\" : fields[curIndex].replaceAll(whitespaceCharsRegex + \"+\", \"\").trim();\n      Matcher m = alphaPattern.matcher(curPart);\n      if (m.find()) {\n        // Some part of the word has alpha characters\n        Number curNum;\n        if (word2NumMap.containsKey(curPart)) {\n          curNum = word2NumMap.get(curPart);\n        } else if (ordWord2NumMap.containsKey(curPart)) {\n          if (curIndex == numWords-1){\n            curNum = ordWord2NumMap.get(curPart);\n          } else {\n            throw new NumberFormatException(\"Error in wordToNumber function.\");\n          }\n        } else if (curIndex > 0 && (curPart.endsWith(\"ths\") || curPart.endsWith(\"rds\"))) {\n          // Fractions?\n          curNum = ordWord2NumMap.get(curPart.substring(0, curPart.length()-1));\n          if (curNum != null) {\n            curNum = 1/curNum.doubleValue();\n          } else {\n            throw new NumberFormatException(\"Bad number put into wordToNumber.  Word is: \\\"\" + curPart + \"\\\", originally part of \\\"\" + originalString + \"\\\", piece # \" + curIndex);\n          }\n        } else if (Character.isDigit(curPart.charAt(0)) || curPart.charAt(0) == '.') {\n          if (curPart.endsWith(\"th\") || curPart.endsWith(\"rd\") || curPart.endsWith(\"nd\") || curPart.endsWith(\"st\")) {\n            curPart = curPart.substring(0, curPart.length()-2).trim();\n          }\n          curNum = parseNumberPart(curPart, originalString, curIndex);\n        } else {\n          throw new NumberFormatException(\"Bad number put into wordToNumber.  Word is: \\\"\" + curPart + \"\\\", originally part of \\\"\" + originalString + \"\\\", piece # \" + curIndex);\n        }\n        numFields[curIndex] = curNum;","sourceCodeStart":348,"sourceCodeEnd":384,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ie/NumberNormalizer.java#L348-L384","documentation":"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.","triggerScenarios":"Calling wordToNumber with a multi-part string where an ordinal word (e.g. 'hundredth', 'third') appears before other numeric pieces, like 'hundredth twelve'.","commonSituations":"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.","solutions":["Normalize the phrase to valid English numeric ordering before parsing ('fifth hundred' -> 'five hundred').","Catch NumberFormatException and skip normalization for that token.","Split the mention so ordinal pieces are passed standalone as the last/only piece."],"exampleFix":"// before\nNumber n = NumberNormalizer.wordToNumber(\"twentieth five\");\n// after\nNumber n = NumberNormalizer.wordToNumber(\"twenty five\"); // ordinal word not mid-string","handlingStrategy":"try-catch","validationCode":"static boolean ordinalWordIsFinal(String phrase) {\n  String[] parts = phrase.split(\"[ -]\");\n  for (int i = 0; i < parts.length - 1; i++) {\n    if (parts[i].matches(\".*(th|first|second|third|st|nd|rd)$\")) return false;\n  }\n  return true;\n}","typeGuard":null,"tryCatchPattern":"try {\n  Number n = NumberNormalizer.wordToNumber(phrase);\n} catch (NumberFormatException e) {\n  if (e.getMessage().contains(\"Error in wordToNumber\")) { /* reorder or skip */ }\n}","preventionTips":["Ensure ordinal words ('third', 'hundredth') only appear as the last piece of the phrase.","Normalize phrases to valid English numeric ordering before parsing.","Handle unsupported compositions as non-numeric rather than retrying."],"tags":["nlp","number-format","parsing","java"],"backgroundTag":"invalid-argument-format","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}