def test_end(self): i = Interval(Time(2013, 1, 1), Time(2013, 1, 2)) self.assertEqual(i.end, Time(2013, 1, 2, 23, 59)) i = Interval(None, Time(2013, 1, 2)) self.assertEqual(i.end, Time(2013, 1, 2, 23, 59)) i = Interval(Time(2013, 1, 1), None) self.assertIsNone(i.end)
def test_start(self): i = Interval(Time(2013, 1, 1), Time(2013, 1, 2)) self.assertEqual(i.start, Time(2013, 1, 1, 0, 0)) i = Interval(Time(2013, 1, 1), None) self.assertEqual(i.start, Time(2013, 1, 1, 0, 0)) i = Interval(None, Time(2013, 1, 2)) self.assertIsNone(i.start)
def test_latent_time_interval(): parse = ctparse("8:00 pm - 9:00 pm", ts=datetime(2020, 1, 1, 7, 0), latent_time=False) assert parse assert parse.resolution == Interval(Time(None, None, None, 20, 00), Time(None, None, None, 21, 00)) parse = ctparse("8:00 pm - 9:00 pm", ts=datetime(2020, 1, 1, 7, 0), latent_time=True) assert parse assert parse.resolution == Interval(Time(2020, 1, 1, 20, 00), Time(2020, 1, 1, 21, 00))
def test_dummy(): scorer = DummyScorer() pp = PartialParse((Time(), Interval()), ("rule1", "rule2")) assert scorer.score("a", datetime.datetime(2019, 1, 1), pp) == 0.0 assert scorer.score_final("a", datetime.datetime(2019, 1, 1), pp, pp.prod[0]) == 0.0
def _latent_time_interval(ts: datetime, ti: Interval) -> Interval: assert ti.t_from and ti.t_to # guaranteed by the caller dm_from = ts + relativedelta(hour=ti.t_from.hour, minute=ti.t_from.minute or 0) dm_to = ts + relativedelta(hour=ti.t_to.hour, minute=ti.t_to.minute or 0) if dm_from <= ts: dm_from += relativedelta(days=1) dm_to += relativedelta(days=1) return Interval( t_from=Time( year=dm_from.year, month=dm_from.month, day=dm_from.day, hour=dm_from.hour, minute=dm_from.minute, period=ti.t_from.period, ), t_to=Time( year=dm_to.year, month=dm_to.month, day=dm_to.day, hour=dm_to.hour, minute=dm_to.minute, period=ti.t_to.period, ), )
def test_random(): rng = random.Random(42) scorer = RandomScorer(rng) pp = PartialParse((Time(), Interval()), ("rule1", "rule2")) assert 0.0 <= scorer.score("a", datetime.datetime(2019, 1, 1), pp) <= 1.0 assert (0.0 <= scorer.score_final("a", datetime.datetime(2019, 1, 1), pp, pp.prod[1]) <= 1.0)
def test_from_str(self): # Complete interval t1 = Time(year=1, month=1, day=1, hour=1, minute=1, DOW=1, POD='pod') t2 = Time(year=2, month=1, day=1, hour=1, minute=1, DOW=1, POD='pod') interval = Interval(t1, t2) i_back = Interval.from_str(str(interval)) self.assertEqual(interval, i_back) # Incomplete interval interval = Interval(None, t2) i_back = Interval.from_str(str(interval)) self.assertEqual(interval, i_back) # Zeroed interval interval = Interval() i_back = Interval.from_str(str(interval)) self.assertEqual(interval, i_back) # Mistake with self.assertRaises(ValueError): Interval.from_str('X-X-X X: X(X/X) -X-X-X X: X(X/X)')
def test_eq(self): a = Artifact() b = Artifact() self.assertEqual(a, b) a = Time(2017, 12, 12, 12, 12, 4, 'morning') b = Time(2017, 12, 12, 12, 12, 4, 'morning') self.assertEqual(a, b) a = Time(2017, 12, 12, 12, 12, 4, 'morning') b = Time(2017, 12, 12, 12, 12, 3, 'morning') self.assertNotEqual(a, b) a = Time() b = Interval() self.assertNotEqual(a, b)
def test_nbscorer(): # We only test that it runs just fine X = [("a", "b"), ("a", ), ("b"), ("a", "b", "a", "b")] y = [False, True, True, False] model = train_naive_bayes(X, y) scorer = NaiveBayesScorer(model) pp = PartialParse((Time(), Interval()), ("rule1", "rule2")) pp.prod[0].mstart = 0 pp.prod[1].mend = 1 pp.prod[0].mend = 1 pp.prod[1].mend = 2 assert 0.0 <= scorer.score("ab", datetime.datetime(2019, 1, 1), pp) <= 1.0 assert (0.0 <= scorer.score_final("ab", datetime.datetime(2019, 1, 1), pp, pp.prod[1]) <= 1.0)
def test_parse_nb_string() -> None: t = Time(year=1, month=1, day=1, hour=1, minute=1, DOW=1, POD="pod") assert t == parse_nb_string("Time[]{0001-01-01 01:01 (1/pod)}") assert Interval(Time(), Time()) == parse_nb_string( "Interval[]{X-X-X X:X (X/X) - X-X-X X:X (X/X)}")
def test_repr(self): self.assertEqual(repr(Interval(Time(), Time())), 'Interval[0-0]{X-X-X X:X (X/X) - X-X-X X:X (X/X)}')
def test_isTimeInterval(self): self.assertTrue(Interval(Time(hour=1), Time(hour=2)).isTimeInterval)
def test_init(self): self.assertIsNotNone(Interval())