Skip to content

Commit

Permalink
WordToNumber fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
EgoLaparra committed May 25, 2019
1 parent e98a387 commit 6b318a9
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ Period Unknown some_time
Period Unknown time
Period Weeks week
Period Weeks weeks
Period Years months
Period Months month
Period Months months
Period Years year
Period Years years
Season-Of-Year Fall fall
Expand Down
145 changes: 72 additions & 73 deletions src/main/scala/org/clulab/timenorm/neural/WordToNumber.scala
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
}
}
}

0 comments on commit 6b318a9

Please sign in to comment.