private static TimePoint AnnotateToTimePoint(string str) { if (str == "" || str == "YMDHMS") { return(null); } Regex pointRegex = new Regex(@"(?<year>[0-9]*)Y(?<month>[0-9]*)M(?<day>[0-9]*)D(?<hour>[0-9]*)H(?<minute>[0-9]*)M(?<second>[0-9]*)S"); Match match = pointRegex.Match(str); if (match == null) { return(null); } TimePoint ret = new TimePoint(); string[] names = { "year", "month", "day", "hour", "minute", "second" }; for (int i = 0; i < names.Length; i++) { string val = match.Groups[names[i]].Value; if (val != "") { switch (i) { case 0: ret.setYear(Convert.ToInt32(val)); break; case 1: ret.setMonth(Convert.ToInt32(val)); break; case 2: ret.setDay(Convert.ToInt32(val)); break; case 3: ret.setHour(Convert.ToInt32(val)); break; case 4: ret.setMinute(Convert.ToInt32(val)); break; case 5: ret.setSecond(Convert.ToInt32(val)); break; } } } return(ret); }
public static void ValGetTimeEntity(string val, TimeEntity e) { string origval = val; val = val.ToUpper(); val = val.Replace(" ", ""); val = val.Replace("\t", ""); if (Regex.IsMatch(val, @"\:(\+|\-)\:")) { e.type = TimeEntityType.RELATIVE_TP; int p1 = val.IndexOf(":"); int p2 = val.LastIndexOf(":"); string val_dur = val.Substring(0, p1); string val_keyev = val.Substring(p2 + 1); string val_rel = val.Substring(p1 + 1, 1); e.duration = GetDurationExpression(val_dur); e.relation = (val_rel == "-") ? TLinkType.BEFORE : TLinkType.AFTER; if (val_keyev == "ADMISSION") { e.keyev = KeyTimeType.ADMISSION; } else if (val_keyev == "DISCHARGE") { e.keyev = KeyTimeType.DISCHARGE; } else if (val_keyev == "OPERATION") { e.keyev = KeyTimeType.OPERATION; } else if (val_keyev == "TRANSFER") { e.keyev = KeyTimeType.TRANSFER; } else if (val_keyev == "LIFE" || val_keyev == "BIRTH") { e.keyev = KeyTimeType.BIRTH; } else if (val_keyev == "OTHERS" || val_keyev == "THISTP") { e.keyev = KeyTimeType.OTHERS; } return; } if (val == "") { //Date TimePoint tp = new TimePoint(); e.setTimePoint(tp); return; } Regex dateRegex1 = new Regex(@"^(?<year>[0-9]+)\-(?<month>[0-9]+)\-(?<day>[0-9]+)$"); if (dateRegex1.IsMatch(val)) { e.type = TimeEntityType.DATE; Match match = dateRegex1.Match(val); TimePoint tp = new TimePoint(Convert.ToInt32(match.Groups["year"].Value), Convert.ToInt32(match.Groups["month"].Value), Convert.ToInt32(match.Groups["day"].Value)); e.setTimePoint(tp); return; } Regex dateRegex2 = new Regex(@"^(?<year>[0-9]+)\-(?<month>[0-9]+)$"); if (dateRegex2.IsMatch(val)) { e.type = TimeEntityType.DATE; Match match = dateRegex2.Match(val); TimePoint tp = new TimePoint(Convert.ToInt32(match.Groups["year"].Value), Convert.ToInt32(match.Groups["month"].Value), TimeConstants.DEFAULT_VALUE); e.setTimePoint(tp); return; } Regex dateRegex3 = new Regex(@"^(?<year>[0-9]+)$"); if (dateRegex3.IsMatch(val)) { e.type = TimeEntityType.DATE; Match match = dateRegex3.Match(val); TimePoint tp = new TimePoint(Convert.ToInt32(match.Groups["year"].Value), TimeConstants.DEFAULT_VALUE, TimeConstants.DEFAULT_VALUE); e.setTimePoint(tp); return; } Regex timeRegex1 = new Regex(@"^(?<year>[0-9]+)\-(?<month>[0-9]+)\-(?<day>[0-9]+)T(?<hour>[0-9]+)\:(?<minute>[0-9]+)$"); if (timeRegex1.IsMatch(val)) { e.type = TimeEntityType.TIME; Match match = timeRegex1.Match(val); TimePoint tp = new TimePoint(Convert.ToInt32(match.Groups["year"].Value), Convert.ToInt32(match.Groups["month"].Value), Convert.ToInt32(match.Groups["day"].Value)); tp.setHour(Convert.ToInt32(match.Groups["hour"].Value)); tp.setMinute(Convert.ToInt32(match.Groups["minute"].Value)); e.setTimePoint(tp); return; } Regex timeRegex2 = new Regex(@"^T(?<hour>[0-9]+)\:(?<minute>[0-9]+)$"); if (timeRegex2.IsMatch(val)) { e.type = TimeEntityType.TIME; Match match = timeRegex2.Match(val); TimePoint tp = new TimePoint(); tp.setHour(Convert.ToInt32(match.Groups["hour"].Value)); tp.setMinute(Convert.ToInt32(match.Groups["minute"].Value)); e.setTimePoint(tp); return; } Regex timeRegex3 = new Regex(@"^T(?<hour>[0-9][0-9])(?<minute>[0-9][0-9])$"); if (timeRegex3.IsMatch(val)) { e.type = TimeEntityType.TIME; Match match = timeRegex3.Match(val); TimePoint tp = new TimePoint(); tp.setHour(Convert.ToInt32(match.Groups["hour"].Value)); tp.setMinute(Convert.ToInt32(match.Groups["minute"].Value)); e.setTimePoint(tp); return; } Regex timeRegex4 = new Regex(@"^(?<year>[0-9]+)\-(?<month>[0-9]+)\-(?<day>[0-9]+)T(?<hour>[0-9][0-9])(?<minute>[0-9][0-9])$"); if (timeRegex4.IsMatch(val)) { e.type = TimeEntityType.TIME; Match match = timeRegex4.Match(val); TimePoint tp = new TimePoint(Convert.ToInt32(match.Groups["year"].Value), Convert.ToInt32(match.Groups["month"].Value), Convert.ToInt32(match.Groups["day"].Value)); tp.setHour(Convert.ToInt32(match.Groups["hour"].Value)); tp.setMinute(Convert.ToInt32(match.Groups["minute"].Value)); e.setTimePoint(tp); return; } Regex tRegex = new Regex(@"T(?<hour>[0-9]+)\:(?<minute>[0-9]+)$"); if (tRegex.IsMatch(val)) { Match match = tRegex.Match(val); TimePoint tp = new TimePoint(); tp.setHour(Convert.ToInt32(match.Groups["hour"].Value)); tp.setMinute(Convert.ToInt32(match.Groups["minute"].Value)); e.setTimePoint(tp); } val = tRegex.Replace(val, ""); Regex absFreqRegex = new Regex(@"R(?<repeats>[0-9]*)$"); if (absFreqRegex.IsMatch(val)) { Match match = absFreqRegex.Match(val); e.type = TimeEntityType.FREQUENCY; e.repeat_times = 0; e.duration = new Dictionary <DurationUnit, double>(); if (match.Groups["repeats"].Value.Length > 0) { e.repeat_times = Convert.ToInt32(match.Groups["repeats"].Value); } return; } else if (val.StartsWith("R")) { if (val.IndexOf("P") == -1) { val = val.Replace("R", "RP"); } e.type = TimeEntityType.FREQUENCY; int p = val.IndexOf("P"); string t = val.Substring(1, p - 1); if (t == "") { e.repeat_times = 0; } else { e.repeat_times = Convert.ToDouble(t); } val = val.Substring(p); } else { e.type = TimeEntityType.DURATION; } e.duration = GetDurationExpression(val); }