PageRenderTime 32ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/test/scala/org/dbpedia/extraction/dataparser/DurationParserTest.scala

https://gitlab.com/varunkothamachu/extraction-framework
Scala | 177 lines | 158 code | 11 blank | 8 comment | 0 complexity | 9e8a588e336d743f51f4b4b06c6b128a MD5 | raw file
  1. package org.dbpedia.extraction.dataparser
  2. import org.scalatest.FlatSpec
  3. import org.dbpedia.extraction.ontology.OntologyDatatypes
  4. import org.dbpedia.extraction.util.Language
  5. import org.scalatest.Matchers
  6. import org.junit.runner.RunWith
  7. import org.scalatest.junit.JUnitRunner
  8. import org.scalatest.matchers.BeMatcher
  9. import org.scalatest.matchers.MatchResult
  10. @RunWith(classOf[JUnitRunner])
  11. class DurationParserTest extends FlatSpec with Matchers
  12. {
  13. "DurationParser" should "return 5 seconds for '5 secs'" in
  14. {
  15. parse("en", "Time", "5 secs").get should equal (5.0)
  16. }
  17. "DurationParser" should "return 10 minutes for 'context 10 min context'" in
  18. {
  19. parse("en", "Time", "context 10 min context").get should equal (600.0)
  20. }
  21. "DurationParser" should "return 14820 seconds for 'context 4 hours, 7 minutes context'" in
  22. {
  23. parse("en", "Time", "context 4 hours, 7 minutes context").get should equal (14820.0)
  24. }
  25. "DurationParser" should "return 14730 seconds for ' 4h 5m, 30s '" in
  26. {
  27. parse("en", "Time", " 4h 5m, 30s ").get should equal (14730.0)
  28. }
  29. "DurationParser" should "return 857752817 seconds for 'ganze 27 years, 2 months, 5 days, 1h 40min., 17s hat es gedauert '" in
  30. {
  31. parse("en", "Time", "ganze 27 years, 2 months, 5 days, 1h 40min., 17s hat es gedauert ").get should be (approximatelyEqualTo(857752817.0))
  32. }
  33. "DurationParser" should "return 188 seconds for 'context 3m 8s context'" in
  34. {
  35. parse("en", "Time", "context 3m 8s context").get should equal (188.0)
  36. }
  37. "DurationParser" should "return 2417 seconds for '40min., 17s'" in
  38. {
  39. parse("en", "Time", "40min., 17s").get should equal (2417.0)
  40. }
  41. "DurationParser" should "return aprox. 32263698 seconds for '1y 8d 4h 7m 8s'" in
  42. {
  43. parse("en", "Time", "1y 8d 4h 7m 8s").get should be (approximatelyEqualTo(32263628.0))
  44. }
  45. "DurationParser" should "return 604997.0 seconds for '27 years, 2 GLUCK, 5 days, 1hrs. 40min., 17s'" in
  46. {
  47. parse("en", "Time", "7 days, 5 DOLLARS, 3 min., 8 METERS, 17s").get should equal (604997.0)
  48. }
  49. // don't match characters after units
  50. "DurationParser" should "return 660 seconds for '11 min '" in
  51. {
  52. parse("en", "Time", "11 min ").get should equal (660.0)
  53. }
  54. "DurationParser" should "return 660 seconds for '(11 min) asd'" in
  55. {
  56. parse("en", "Time", "(11 min)").get should equal (660.0)
  57. }
  58. "DurationParser" should "return 660 seconds for '11 min,'" in
  59. {
  60. parse("en", "Time", "11 min,").get should equal (660.0)
  61. }
  62. "DurationParser" should "return 660 seconds for '11 min/'" in
  63. {
  64. parse("en", "Time", "11 min/").get should equal (660.0)
  65. }
  66. "DurationParser" should "return 86400 seconds for '1d/'" in
  67. {
  68. parse("en", "Time", "1d/").get should equal (86400.0)
  69. }
  70. // Dots, commas, spaces
  71. "DurationParser" should "return 435649 seconds for '5.042 234 days'" in
  72. {
  73. parse("en", "Time", "5.042 234 days").get should equal (435649.01759999996)
  74. }
  75. "DurationParser" should "return 48124803628 seconds for '577,000.042 d'" in
  76. {
  77. parse("en", "Time", "577,000.042 d").get should equal (49852803628.8)
  78. }
  79. "DurationParser" should "return 48124803628 seconds for '577.000,042 Tage'" in
  80. {
  81. parse("de", "Time", "577.000,042 Tage").get should equal (49852803628.8)
  82. }
  83. "DurationParser" should "return 9954414708530 seconds for '115,213,133.2 days, 50 secs'" in
  84. {
  85. parse("en", "Time", "115,213,133.2 days, 50 secs").get should equal (9954414708530.0)
  86. }
  87. // Implicit unit format
  88. "DurationParser" should "return 273 seconds for '4 min. 33'" in
  89. {
  90. parse("fr", "Time", "4 min. 33").get should equal (273.0)
  91. }
  92. "DurationParser" should "return 5400 seconds for '1h30'" in
  93. {
  94. parse("fr", "Time", "1h30").get should equal (5400.0)
  95. }
  96. "DurationParser" should "return 4010 seconds for '66'50'" in
  97. {
  98. parse("fr", "Time", "66'50").get should equal (4010.0)
  99. }
  100. // Colon separated
  101. "DurationParser" should "return 30 seconds for ':30'" in
  102. {
  103. parse("en", "Time", ":30").get should equal (30.0)
  104. }
  105. "DurationParser" should "return 30 seconds for ':30 sec'" in
  106. {
  107. parse("en", "Time", ":30 sec").get should equal (30.0)
  108. }
  109. "DurationParser" should "return 90 seconds for '1:30'" in
  110. {
  111. parse("en", "Time", "1:30 min").get should equal (90.0)
  112. }
  113. "DurationParser" should "return 90 minutes for '1:30 h'" in
  114. {
  115. parse("en", "Time", "1:30 h").get should equal (5400.0)
  116. }
  117. "DurationParser" should "return 15030 seconds for '4:10:30'" in
  118. {
  119. parse("en", "Time", "4:10:30").get should equal (15030.0)
  120. }
  121. "DurationParser" should "return 20430 seconds for 'bla 5:40:30 h long'" in
  122. {
  123. parse("en", "Time", "bla 5:40:30 h long").get should equal (20430.0)
  124. }
  125. "DurationParser" should "return 20430 seconds for 'bla 5:40:30 super'" in
  126. {
  127. parse("en", "Time", "bla 5:40:30 super").get should equal (20430.0)
  128. }
  129. // These tests should fail:
  130. "DurationParser" should "return None for 'bla 5:40:30 seconds'" in
  131. {
  132. parse("en", "Time", "bla 5:40:30 seconds") should equal (None)
  133. }
  134. "DurationParser" should "return None for '2002-present'" in
  135. {
  136. parse("en", "Time", "2002-present") should equal (None)
  137. }
  138. /**
  139. * Matcher to test if 2 values are approximately equal.
  140. */
  141. case class approximatelyEqualTo(r : Double) extends BeMatcher[Double]
  142. {
  143. val epsilon = 0.001
  144. def apply(l: Double) =
  145. MatchResult(
  146. compare(l, r),
  147. l + " is not approximately equal to " + r,
  148. l + " is approximately equal to " + r
  149. )
  150. private def compare(l : Double, r : Double) : Boolean =
  151. {
  152. scala.math.abs(l - r) < epsilon
  153. }
  154. }
  155. private val datatypes = OntologyDatatypes.load().map(dt => (dt.name, dt)).toMap
  156. private def parse(lang : String, datatypeName : String, input : String) : Option[Double] =
  157. {
  158. val context = new
  159. {
  160. def language : Language = Language(lang)
  161. }
  162. val durationParser = new DurationParser(context)
  163. val inputDatatype = datatypes(datatypeName)
  164. durationParser.parseToSeconds(input, inputDatatype)
  165. }
  166. }