-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e98a387
commit 6b318a9
Showing
2 changed files
with
74 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 72 additions & 73 deletions
145
src/main/scala/org/clulab/timenorm/neural/WordToNumber.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,72 @@ | ||
package org.clulab.timenorm.neural | ||
|
||
object WordToNumber { | ||
|
||
private val Small = Map( | ||
"zero" -> 0L, | ||
"one" -> 1L, | ||
"two" -> 2L, | ||
"three" -> 3L, | ||
"four" -> 4L, | ||
"five" -> 5L, | ||
"six" -> 6L, | ||
"seven" -> 7L, | ||
"eight" -> 8L, | ||
"nine" -> 9L, | ||
"ten" -> 10L, | ||
"eleven" -> 11L, | ||
"twelve" -> 12L, | ||
"thirteen" -> 13L, | ||
"fourteen" -> 14L, | ||
"fifteen" -> 15L, | ||
"sixteen" -> 16L, | ||
"seventeen" -> 17L, | ||
"eighteen" -> 18L, | ||
"nineteen" -> 19L, | ||
"twenty" -> 20L, | ||
"thirty" -> 30L, | ||
"forty" -> 40L, | ||
"fifty" -> 50L, | ||
"sixty" -> 60L, | ||
"seventy" -> 70L, | ||
"eighty" -> 80L, | ||
"ninety" -> 90L | ||
) | ||
|
||
private val Magnitude = Map( | ||
"thousand" -> 1000L, | ||
"million" -> 1000000L, | ||
"billion" -> 1000000000L, | ||
"trillion" -> 1000000000000L, | ||
"quadrillion" -> 1000000000000000L, | ||
"quintillion" -> 1000000000000000000L, | ||
// "sextillion" -> 1000000000000000000000L, | ||
// "septillion" -> 1000000000000000000000000L, | ||
// "octillion" -> 1000000000000000000000000000L, | ||
// "nonillion" -> 1000000000000000000000000000000L, | ||
// "decillion" -> 1000000000000000000000000000000000L | ||
) | ||
|
||
def convert(string: String): String = { | ||
try { | ||
var n = 0L | ||
var g = 0L | ||
for (w <- string.split("[\\s-]+")) { | ||
try { | ||
g += Small(w) | ||
} catch { | ||
case e: NoSuchElementException => | ||
if (w == "hundred" && g!=0) | ||
g *= 100L | ||
else { | ||
n += g * Magnitude(w) | ||
g = 0L | ||
} | ||
} | ||
(n + g).toString | ||
} | ||
} catch { | ||
case e: Exception => string | ||
} | ||
string | ||
} | ||
} | ||
package org.clulab.timenorm.neural | ||
|
||
object WordToNumber { | ||
|
||
private val Small: Map[String, Long] = Map( | ||
"zero" -> 0L, | ||
"one" -> 1L, | ||
"two" -> 2L, | ||
"three" -> 3L, | ||
"four" -> 4L, | ||
"five" -> 5L, | ||
"six" -> 6L, | ||
"seven" -> 7L, | ||
"eight" -> 8L, | ||
"nine" -> 9L, | ||
"ten" -> 10L, | ||
"eleven" -> 11L, | ||
"twelve" -> 12L, | ||
"thirteen" -> 13L, | ||
"fourteen" -> 14L, | ||
"fifteen" -> 15L, | ||
"sixteen" -> 16L, | ||
"seventeen" -> 17L, | ||
"eighteen" -> 18L, | ||
"nineteen" -> 19L, | ||
"twenty" -> 20L, | ||
"thirty" -> 30L, | ||
"forty" -> 40L, | ||
"fifty" -> 50L, | ||
"sixty" -> 60L, | ||
"seventy" -> 70L, | ||
"eighty" -> 80L, | ||
"ninety" -> 90L | ||
) | ||
|
||
private val Magnitude: Map[String, Long] = Map( | ||
"thousand" -> 1000L, | ||
"million" -> 1000000L, | ||
"billion" -> 1000000000L, | ||
"trillion" -> 1000000000000L, | ||
"quadrillion" -> 1000000000000000L, | ||
"quintillion" -> 1000000000000000000L, | ||
// "sextillion" -> 1000000000000000000000L, | ||
// "septillion" -> 1000000000000000000000000L, | ||
// "octillion" -> 1000000000000000000000000000L, | ||
// "nonillion" -> 1000000000000000000000000000000L, | ||
// "decillion" -> 1000000000000000000000000000000000L | ||
) | ||
|
||
def convert(string: String): String = { | ||
try { | ||
var n = 0L | ||
var g = 0L | ||
for (w <- string.split("[\\s-]+")) { | ||
try { | ||
g += Small(w) | ||
} catch { | ||
case e: NoSuchElementException => | ||
if (w == "hundred" && g!=0) | ||
g *= 100L | ||
else { | ||
n += g * Magnitude(w) | ||
g = 0L | ||
} | ||
} | ||
} | ||
(n + g).toString | ||
} catch { | ||
case e: Exception => string | ||
} | ||
} | ||
} |