Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date rule issue776 #777

Merged
merged 5 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions main/src/main/resources/org/clulab/numeric/WEEK.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@
# note: multi-word phrases must be tokenized in the same way as our tokenizer. If not sure, try the phrases in ./shell first!
#
first week // XXXX-XX-01 -- XXXX-XX-07
1st week // XXXX-XX-01 -- XXXX-XX-07
1st week // XXXX-XX-01 -- XXXX-XX-07
Copy link
Member

@kwalcock kwalcock Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is what it (first week) really means, but that might have been discussed at length previously. If Saturday is the 1st of the month, then the first week might be 3-9 for me. Haven't I seen it written -W1? The first week of June is XXXX-06-W1.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will work on #778 and tidy a few things below and merge it all at once if that's OK.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Your comment about W1 is valid. I agree this would be the best representation.
Perhaps this could be done after merging these changes?

first // XXXX-XX-01 -- XXXX-XX-07
1st // XXXX-XX-01 -- XXXX-XX-07
second week // XXXX-XX-08 -- XXXX-XX-14
2nd week // XXXX-XX-08 -- XXXX-XX-14
second weeks // XXXX-XX-08 -- XXXX-XX-14
2nd week // XXXX-XX-08 -- XXXX-XX-14
2nd weeks // XXXX-XX-08 -- XXXX-XX-14
second // XXXX-XX-08 -- XXXX-XX-14
2nd // XXXX-XX-08 -- XXXX-XX-14
third week // XXXX-XX-15 -- XXXX-XX-21
3rd week // XXXX-XX-15 -- XXXX-XX-21
third weeks // XXXX-XX-15 -- XXXX-XX-21
3rd week // XXXX-XX-15 -- XXXX-XX-21
3rd weeks // XXXX-XX-15 -- XXXX-XX-21
third // XXXX-XX-15 -- XXXX-XX-21
3rd // XXXX-XX-15 -- XXXX-XX-21
fourth week // XXXX-XX-22 -- XXXX-XX-28
4th week // XXXX-XX-22 -- XXXX-XX-28
fourth weeks // XXXX-XX-22 -- XXXX-XX-28
4th week // XXXX-XX-22 -- XXXX-XX-28
4th weeks // XXXX-XX-22 -- XXXX-XX-28
first two weeks // XXXX-XX-01 -- XXXX-XX-14
second two weeks // XXXX-XX-15 -- XXXX-XX-28
11 changes: 10 additions & 1 deletion main/src/main/resources/org/clulab/numeric/date-ranges.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@
pattern: |
/(?i)between/ @month1:PossibleMonth /(?i)(and|to)/ @month2:PossibleMonth @year:PossibleYear

- name: date-range-5-week
priority: ${rulepriority}
label: DateRange
type: token
example: "It took place between the second and third weeks of June"
action: mkDateRangeMentionBetweenWeeks
pattern: |
/(?i)between/ /(?i)the/? (?<week1> /(?i)(first|1st|second|2nd)/ /(?i)week/?) /(?i)(and|to)/ (?<week2> /(?i)(second|2nd|third|3rd|fourth|4th|last)/ /(?i)weeks?/) /(?i)of/ @month:PossibleMonth

- name: date-range-6
priority: ${rulepriority}
label: DateRange
Expand Down Expand Up @@ -101,7 +110,7 @@
example: "First two weeks of May"
action: mkDateRangeMentionWithWeek
pattern: |
(?<week> /(?i)(first|second|last)/ /(?i)two/ /(?i)weeks/) /(?i)of/ @month:PossibleMonth
(?<week> /(?i)(first|second|last)/ /(?i)(two|three)/ /(?i)weeks/) /(?i)of/ @month:PossibleMonth

- name: date-unbound-range-1
priority: ${rulepriority}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ class NumericActions(seasonNormalizer: SeasonNormalizer, unitNormalizer: UnitNor
convert(mentions, toDateRangeMentionWithMonth, "toDateRangeMentionWithMonth")
}

/** Constructs a DateRangeMention from a token pattern */
def mkDateRangeMentionBetweenWeeks(mentions: Seq[Mention], state: State): Seq[Mention] = {
convert(mentions, toDateRangeMentionBetweenWeeks(weekNormalizer), "toDateRangeMentionBetweenWeeks")
}

/** Constructs a DateRangeMention from a token pattern */
def mkDateRangeMentionWithSinceRef(mentions: Seq[Mention], state: State): Seq[Mention] = {
convert(mentions, toDateRangeMentionWithSinceRef, "toDateRangeMentionWithSinceRef")
Expand Down
44 changes: 41 additions & 3 deletions main/src/main/scala/org/clulab/numeric/mentions/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,30 @@ package object mentions {
throw new RuntimeException(s"ERROR: cannot convert mention of type [${m.getClass.toString}] to DateRangeMention!")
}

def toDateRangeMentionBetweenWeeks(weekNormalizer: WeekNormalizer)(mention: Mention): DateRangeMention = mention match {
case m: DateRangeMention => m

case m: RelationMention =>
val w1Norm = getWeekRange(weekNormalizer)("week1", m)
if(w1Norm.isEmpty)
throw new RuntimeException(s"ERROR: could not find argument week1 in mention [${m.raw.mkString(" ")}]!")

val w2Norm = getWeekRange(weekNormalizer)("week2", m)
if(w2Norm.isEmpty)
throw new RuntimeException(s"ERROR: could not find argument week2 in mention [${m.raw.mkString(" ")}]!")

val month = getArgWords("month", m)

DateRangeMention(
m,
TempEvalFormatter.mkDate(w1Norm.get.startDay, month, None),
TempEvalFormatter.mkDate(w2Norm.get.endDay, month, None)
)

case m =>
throw new RuntimeException(s"ERROR: cannot convert mention of type [${m.getClass.toString}] to DateRangeMention!")
}

def toDateRangeMentionWithMonth(mention: Mention): DateRangeMention = mention match {
case m: DateRangeMention => m

Expand Down Expand Up @@ -918,9 +942,14 @@ package object mentions {
val wordsOpt = getArgWords(argName, m)

if (wordsOpt.isEmpty) None
else if (wordsOpt.get.mkString(" ").toLowerCase().equals("last week")) {getLastWeekRange(m)}
else if (wordsOpt.get.mkString(" ").toLowerCase().equals("last two weeks")) {getLastTwoWeeksRange(m)}
else weekNormalizer.norm(wordsOpt.get)
else {
val wordSeq = wordsOpt.get.mkString(" ").toLowerCase()
//println(s"wordSeq = $wordSeq")
if (wordSeq.equals("last week")) getLastWeekRange(m)
else if (wordSeq.equals("last two weeks")) getLastTwoWeeksRange(m)
else if (wordSeq.equals("last three weeks")) getLastThreeWeeksRange(m)
else weekNormalizer.norm(wordsOpt.get)
}
}

private def getLastWeekRange(m:Mention): Option[WeekRange] = {
Expand All @@ -941,6 +970,15 @@ package object mentions {
Some(WeekRange(startDay = Some(Seq((lastDay - 13).toString)), endDay = Some(Seq(lastDay.toString))))
}

private def getLastThreeWeeksRange(m:Mention): Option[WeekRange] = {
val month = getArgWords("month", m)
val modifiedMonth = TempEvalFormatter.convertLiteralMonth(month.get.mkString(""))
val monthObj = Month.of(modifiedMonth)
val lastDay = monthObj.length(false)

Some(WeekRange(startDay = Some(Seq((lastDay - 20).toString)), endDay = Some(Seq(lastDay.toString))))
}

private def getHoliday(holiday: Seq[String], year: Option[Seq[String]]): (Option[Seq[String]], Option[Seq[String]]) = {
val dayMonthOpt = HolidayNormalizer.norm(holiday, year)
dayMonthOpt match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,14 @@ class TestNumericEntityRecognition extends Test {
ensure(sentence = "We applied it in Fall in 21", Interval(4, 7), goldEntity= "DATE-RANGE", goldNorm = "XX21-09-22 -- XX21-12-21")
ensure(sentence = "We applied it in fall of 2021", Interval(4, 7), goldEntity= "DATE-RANGE", goldNorm = "2021-09-22 -- 2021-12-21")
}

it should "recognize between-week date ranges" in {
ensure("It took place between the second and third weeks of June",
Interval(3, 11), "DATE-RANGE", "XXXX-06-08 -- XXXX-06-21")

ensure("It took place between the first and 4th weeks of May",
Interval(3, 11), "DATE-RANGE", "XXXX-05-01 -- XXXX-05-28")
}

it should "recognize measurement units" in {
ensure("It was 12 ha", Interval(2, 4), "MEASUREMENT-AREA", "12.0 ha")
Expand Down