From 11997340f898354c4e974195d959ac911a9a4c8b Mon Sep 17 00:00:00 2001 From: abroad-crawler <287510038@qq.com> Date: Wed, 1 Sep 2021 16:45:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9NLP=E8=AF=86=E5=88=ABbug,?= =?UTF-8?q?=E4=BE=8B=E5=A6=82:"=E6=98=A8=E5=A4=A9XX","=E5=89=8D=E5=A4=A9XX?= =?UTF-8?q?","=E5=A4=A7=E5=89=8D=E5=A4=A9XX"=E8=AE=A1=E7=AE=97=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/xkzhangsan/time/nlp/TimeNLP.java | 484 +++++++++--------- .../xkzhangsan/time/test/TimeNLPUtilTest.java | 467 ++++++++--------- 2 files changed, 484 insertions(+), 467 deletions(-) diff --git a/src/main/java/com/xkzhangsan/time/nlp/TimeNLP.java b/src/main/java/com/xkzhangsan/time/nlp/TimeNLP.java index f889bf1..73fb46f 100644 --- a/src/main/java/com/xkzhangsan/time/nlp/TimeNLP.java +++ b/src/main/java/com/xkzhangsan/time/nlp/TimeNLP.java @@ -1,44 +1,41 @@ package com.xkzhangsan.time.nlp; +import com.xkzhangsan.time.calculator.DateTimeCalculatorUtil; +import com.xkzhangsan.time.converter.DateTimeConverterUtil; +import com.xkzhangsan.time.enums.MomentEnum; +import com.xkzhangsan.time.enums.RegexEnum; +import com.xkzhangsan.time.formatter.DateTimeFormatterUtil; +import com.xkzhangsan.time.utils.CollectionUtil; + import java.time.LocalDateTime; import java.time.temporal.ChronoField; import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalField; import java.time.temporal.TemporalUnit; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; -import com.xkzhangsan.time.calculator.DateTimeCalculatorUtil; -import com.xkzhangsan.time.converter.DateTimeConverterUtil; -import com.xkzhangsan.time.enums.MomentEnum; -import com.xkzhangsan.time.enums.RegexEnum; -import com.xkzhangsan.time.formatter.DateTimeFormatterUtil; -import com.xkzhangsan.time.utils.CollectionUtil; - /** * 时间自然语言分析 - * + *

* 修改自 https://github.com/shinyke/Time-NLP + * * @author xkzhangsan */ public class TimeNLP { - + private static Map TUNIT_MAP = new HashMap<>(); static { - //ChronoField + //ChronoField TUNIT_MAP.put(0, ChronoField.YEAR); TUNIT_MAP.put(1, ChronoField.MONTH_OF_YEAR); TUNIT_MAP.put(2, ChronoField.DAY_OF_MONTH); TUNIT_MAP.put(3, ChronoField.HOUR_OF_DAY); TUNIT_MAP.put(4, ChronoField.MINUTE_OF_HOUR); TUNIT_MAP.put(5, ChronoField.SECOND_OF_MINUTE); - + //ChronoUnit TUNIT_MAP.put(10, ChronoUnit.YEARS); TUNIT_MAP.put(11, ChronoUnit.MONTHS); @@ -47,12 +44,12 @@ public class TimeNLP { TUNIT_MAP.put(14, ChronoUnit.MINUTES); TUNIT_MAP.put(15, ChronoUnit.SECONDS); } - + /** * 目标字符串 */ private String timeExpression = null; - + /** * 解析结果格式化1 * yyyy年MM月dd日hh时mm分ss秒 @@ -67,7 +64,7 @@ public class TimeNLP { * 解析结果时间 */ private Date time; - + private Boolean isAllDayTime = true; private boolean isFirstTimeSolveContext = true; @@ -80,7 +77,7 @@ public class TimeNLP { * 该方法作为时间表达式单元的入口,将时间表达式字符串传入 * * @param timeExpression 时间表达式字符串 - * @param textAnalysis 正则文件分析类 + * @param textAnalysis 正则文件分析类 */ public TimeNLP(String timeExpression, TextAnalysis textAnalysis) { this.timeExpression = timeExpression; @@ -92,20 +89,21 @@ public TimeNLP(String timeExpression, TextAnalysis textAnalysis) { * 时间表达式单元构造方法 * 该方法作为时间表达式单元的入口,将时间表达式字符串传入 * - * @param timeExpression 时间表达式字符串 - * @param textAnalysis 正则文件分析类 - * @param timePoint 上下文时间 + * @param timeExpression 时间表达式字符串 + * @param textAnalysis 正则文件分析类 + * @param timePoint 上下文时间 */ public TimeNLP(String timeExpression, TextAnalysis textAnalysis, TimeContext timePoint) { - this.timeExpression = timeExpression; - this.textAnalysis = textAnalysis; - this.timeContextOrigin = timePoint; + this.timeExpression = timeExpression; + this.textAnalysis = textAnalysis; + this.timeContextOrigin = timePoint; timeNormalization(); } - + /** * 获取 Date + * * @return Date */ public Date getTime() { @@ -114,40 +112,41 @@ public Date getTime() { /** * 标准时间解析 - *

-	 *     yyyy-MM-dd HH:mm:ss
-	 *     yyyy-MM-dd HH:mm
-	 *     yyyy-MM-dd
-	 * 
+ *
+     *     yyyy-MM-dd HH:mm:ss
+     *     yyyy-MM-dd HH:mm
+     *     yyyy-MM-dd
+     * 
+ * * @return LocalDateTime */ - private LocalDateTime normStandardTime() { - LocalDateTime localDateTime = null; + private LocalDateTime normStandardTime() { + LocalDateTime localDateTime = null; Pattern pattern = RegexEnum.NormStandard.getPattern(); Matcher match = pattern.matcher(timeExpression); if (match.find()) { - try{ - localDateTime = DateTimeFormatterUtil.smartParseToLocalDateTime(timeExpression); - int [] tunit = timeContext.getTunit(); - tunit[0]=localDateTime.getYear(); - tunit[1]=localDateTime.getMonthValue(); - tunit[2]=localDateTime.getDayOfMonth(); - if(localDateTime.getHour()>0){ - tunit[3]=localDateTime.getHour(); - } - if(localDateTime.getMinute()>0){ - tunit[4]=localDateTime.getMinute(); - } - if(localDateTime.getSecond()>0){ - tunit[5]=localDateTime.getSecond(); - } - }catch(Exception e){ - System.out.println("normStandardTime error:"+e.getMessage()); - } - } - return localDateTime; - } - + try { + localDateTime = DateTimeFormatterUtil.smartParseToLocalDateTime(timeExpression); + int[] tunit = timeContext.getTunit(); + tunit[0] = localDateTime.getYear(); + tunit[1] = localDateTime.getMonthValue(); + tunit[2] = localDateTime.getDayOfMonth(); + if (localDateTime.getHour() > 0) { + tunit[3] = localDateTime.getHour(); + } + if (localDateTime.getMinute() > 0) { + tunit[4] = localDateTime.getMinute(); + } + if (localDateTime.getSecond() > 0) { + tunit[5] = localDateTime.getSecond(); + } + } catch (Exception e) { + System.out.println("normStandardTime error:" + e.getMessage()); + } + } + return localDateTime; + } + /** * 年-规范化方法 *

@@ -251,14 +250,14 @@ private void normHour() { } /* * 对关键字:早(包含早上/早晨/早间),上午,中午,午间,下午,午后,晚上,傍晚,晚间,晚,pm,PM的正确时间计算 - * 规约: - * 1.中午/午间0-10点视为12-22点 - * 2.下午/午后0-11点视为12-23点 - * 3.晚上/傍晚/晚间/晚1-11点视为13-23点,12点视为0点 - * 4.0-11点pm/PM视为12-23点 - * - * add by kexm - */ + * 规约: + * 1.中午/午间0-10点视为12-22点 + * 2.下午/午后0-11点视为12-23点 + * 3.晚上/傍晚/晚间/晚1-11点视为13-23点,12点视为0点 + * 4.0-11点pm/PM视为12-23点 + * + * add by kexm + */ pattern = RegexEnum.NormHourDayBreak.getPattern(); match = pattern.matcher(timeExpression); if (match.find()) { @@ -336,14 +335,14 @@ else if (timeContext.getTunit()[3] == -1) * 该方法识别时间表达式单元的分字段 */ private void normMinute() { - - //特殊情况排查,比如30分后 - Pattern pattern = RegexEnum.NormMinuteSpec.getPattern(); - Matcher match = pattern.matcher(timeExpression); - if (match.find()) { - return; - } - + + //特殊情况排查,比如30分后 + Pattern pattern = RegexEnum.NormMinuteSpec.getPattern(); + Matcher match = pattern.matcher(timeExpression); + if (match.find()) { + return; + } + pattern = RegexEnum.NormMinute.getPattern(); match = pattern.matcher(timeExpression); if (match.find()) { @@ -381,6 +380,7 @@ private void normMinute() { preferFuture(4); isAllDayTime = false; } + } /** @@ -389,17 +389,20 @@ private void normMinute() { * 该方法识别时间表达式单元的秒字段 */ private void normSecond() { - //特殊情况排查,比如30秒后 - Pattern pattern = RegexEnum.NormSecondSpec.getPattern(); - Matcher match = pattern.matcher(timeExpression); - if (match.find()) { - return; - } - /* - * 添加了省略“分”说法的时间 - * 如17点15分32 - * modified by 曹零 - */ + //特殊情况排查,比如30秒后 + Pattern pattern = RegexEnum.NormSecondSpec.getPattern(); + Matcher match = pattern.matcher(timeExpression); + if (match.find()) { + return; + } + +// timeContext.getTunit()[5] = 0; + + /* + * 添加了省略“分”说法的时间 + * 如17点15分32 + * modified by 曹零 + */ pattern = RegexEnum.NormSecond.getPattern(); match = pattern.matcher(timeExpression); if (match.find()) { @@ -445,11 +448,11 @@ private void normTotal() { isAllDayTime = false; } } - /* - * 增加了:固定形式时间表达式的 - * 中午,午间,下午,午后,晚上,傍晚,晚间,晚,pm,PM - * 的正确时间计算,规约同上 - */ + /* + * 增加了:固定形式时间表达式的 + * 中午,午间,下午,午后,晚上,傍晚,晚间,晚,pm,PM + * 的正确时间计算,规约同上 + */ pattern = RegexEnum.NormHourNoon.getPattern(); match = pattern.matcher(timeExpression); if (match.find()) { @@ -489,7 +492,6 @@ else if (timeContext.getTunit()[3] == 12) isAllDayTime = false; } - pattern = RegexEnum.NormTotalDateOne.getPattern(); match = pattern.matcher(timeExpression); if (match.find()) { @@ -511,11 +513,11 @@ else if (timeContext.getTunit()[3] == 12) timeContext.getTunit()[2] = Integer.parseInt(tmpParser[1]); timeContext.getTunit()[0] = Integer.parseInt(tmpParser[2]); } - - /* - * 增加了:固定形式时间表达式 年.月.日 的正确识别 - * add by 曹零 - */ + + /* + * 增加了:固定形式时间表达式 年.月.日 的正确识别 + * add by 曹零 + */ pattern = RegexEnum.NormTotalDateThree.getPattern(); match = pattern.matcher(timeExpression); if (match.find()) { @@ -542,11 +544,10 @@ private void normBaseRelated() { boolean flag = false;//观察时间表达式是否因当前相关时间表达式而改变时间 - Pattern pattern = RegexEnum.NormBaseRelatedDayBefore.getPattern(); Matcher match = pattern.matcher(timeExpression); if (match.find()) { - flag = true; + flag = true; int day = Integer.parseInt(match.group()); localDateTime = localDateTime.minusDays(day); } @@ -554,7 +555,7 @@ private void normBaseRelated() { pattern = RegexEnum.NormBaseRelatedDayAfter.getPattern(); match = pattern.matcher(timeExpression); if (match.find()) { - flag = true; + flag = true; int day = Integer.parseInt(match.group()); localDateTime = localDateTime.plusDays(day); } @@ -562,7 +563,7 @@ private void normBaseRelated() { pattern = RegexEnum.NormBaseRelatedMonthBefore.getPattern(); match = pattern.matcher(timeExpression); if (match.find()) { - flag = true; + flag = true; int month = Integer.parseInt(match.group()); localDateTime = localDateTime.minusMonths(month); } @@ -570,7 +571,7 @@ private void normBaseRelated() { pattern = RegexEnum.NormBaseRelatedMonthAfter.getPattern(); match = pattern.matcher(timeExpression); if (match.find()) { - flag = true; + flag = true; int month = Integer.parseInt(match.group()); localDateTime = localDateTime.plusMonths(month); } @@ -578,7 +579,7 @@ private void normBaseRelated() { pattern = RegexEnum.NormBaseRelatedYearBefore.getPattern(); match = pattern.matcher(timeExpression); if (match.find()) { - flag = true; + flag = true; int year = Integer.parseInt(match.group()); localDateTime = localDateTime.minusYears(year); } @@ -586,16 +587,16 @@ private void normBaseRelated() { pattern = RegexEnum.NormBaseRelatedYearAfter.getPattern(); match = pattern.matcher(timeExpression); if (match.find()) { - flag = true; + flag = true; int year = Integer.parseInt(match.group()); localDateTime = localDateTime.plusYears(year); } - if(flag){ - setUnitValues(localDateTime); + if (flag) { + setUnitValues(localDateTime); } } - + /** * 设置以上文时间为基准的时间偏移计算,时间部分 */ @@ -639,7 +640,7 @@ private void normBaseTimeRelated() { flag = true; localDateTime = localDateTime.plusMinutes(30); } - + pattern = RegexEnum.NormBaseTimeRelatedMinuteBefore.getPattern(); Matcher matchMinuteBefore = pattern.matcher(timeExpression); if (matchMinuteBefore.find()) { @@ -655,9 +656,9 @@ private void normBaseTimeRelated() { int minute = Integer.parseInt(matchMinuteAfter.group()); localDateTime = localDateTime.plusMinutes(minute); } - + //1个小时10分钟前,组合处理 - if(matchMinuteBefore.find()){ + if (matchMinuteBefore.find()) { pattern = RegexEnum.NormBaseTimeRelatedHour.getPattern(); match = pattern.matcher(timeExpression); if (match.find()) { @@ -666,8 +667,8 @@ private void normBaseTimeRelated() { localDateTime = localDateTime.minusHours(hour); } } - - if(matchMinuteAfter.find()){ + + if (matchMinuteAfter.find()) { pattern = RegexEnum.NormBaseTimeRelatedHour.getPattern(); match = pattern.matcher(timeExpression); if (match.find()) { @@ -676,7 +677,7 @@ private void normBaseTimeRelated() { localDateTime = localDateTime.plusHours(hour); } } - + pattern = RegexEnum.NormBaseTimeRelatedSecondBefore.getPattern(); Matcher matchSecondBefore = pattern.matcher(timeExpression); if (matchSecondBefore.find()) { @@ -692,8 +693,8 @@ private void normBaseTimeRelated() { int second = Integer.parseInt(matchSecondAfter.group()); localDateTime = localDateTime.plusSeconds(second); } - - if(matchSecondBefore.find()){ + + if (matchSecondBefore.find()) { pattern = RegexEnum.NormBaseTimeRelatedMinute.getPattern(); match = pattern.matcher(timeExpression); if (match.find()) { @@ -702,8 +703,8 @@ private void normBaseTimeRelated() { localDateTime = localDateTime.minusMinutes(minute); } } - - if(matchSecondAfter.find()){ + + if (matchSecondAfter.find()) { pattern = RegexEnum.NormBaseTimeRelatedMinute.getPattern(); match = pattern.matcher(timeExpression); if (match.find()) { @@ -713,8 +714,8 @@ private void normBaseTimeRelated() { } } - if(flag){ - setUnitValues(localDateTime); + if (flag) { + setUnitValues(localDateTime); } } @@ -794,21 +795,21 @@ private void normCurRelated() { match = pattern.matcher(timeExpression); if (match.find()) { flag[2] = true; - localDateTime = localDateTime.minusDays(-3); + localDateTime = localDateTime.minusDays(3); } pattern = RegexEnum.NormCurRelatedDayBeforeLast.getPattern(); match = pattern.matcher(timeExpression); if (match.find()) { flag[2] = true; - localDateTime = localDateTime.minusDays(-2); + localDateTime = localDateTime.minusDays(2); } pattern = RegexEnum.NormCurRelatedDayYesterday.getPattern(); match = pattern.matcher(timeExpression); if (match.find()) { flag[2] = true; - localDateTime = localDateTime.minusDays(-1); + localDateTime = localDateTime.minusDays(1); } pattern = RegexEnum.NormCurRelatedDayToday.getPattern(); @@ -927,7 +928,8 @@ private void normCurRelated() { * 如果用户选项是倾向于未来时间,检查所指的day_of_week是否是过去的时间,如果是的话,设为下周。 *

* 如在周五说:周一开会,识别为下周一开会 - * @param weekday 识别出是周几(范围1-7) + * + * @param weekday 识别出是周几(范围1-7) * @param localDateTime * @return */ @@ -945,8 +947,10 @@ private LocalDateTime preferFutureWeek(int weekday, LocalDateTime localDateTime) LocalDateTime curDateTime = LocalDateTime.now(); if (this.timeContextOrigin.getTimeBase() != null) { String[] ini = this.timeContextOrigin.getTimeBase().split("-"); - curDateTime = LocalDateTime.of(Integer.valueOf(ini[0]).intValue(), Integer.valueOf(ini[1]).intValue(), Integer.valueOf(ini[2]).intValue() - , Integer.valueOf(ini[3]).intValue(), Integer.valueOf(ini[4]).intValue(), Integer.valueOf(ini[5]).intValue()); + curDateTime = LocalDateTime.of(Integer.valueOf(ini[0]).intValue(), Integer.valueOf(ini[1]).intValue(), + Integer.valueOf(ini[2]).intValue() + , Integer.valueOf(ini[3]).intValue(), Integer.valueOf(ini[4]).intValue(), + Integer.valueOf(ini[5]).intValue()); } int curWeekday = curDateTime.get(ChronoField.DAY_OF_WEEK); if (curWeekday < weekday) { @@ -954,9 +958,9 @@ private LocalDateTime preferFutureWeek(int weekday, LocalDateTime localDateTime) } //准备增加的时间单位是被检查的时间的上一级,将上一级时间+1 return localDateTime.plusWeeks(1); - } + } - /** + /** * 该方法用于更新timeBase使之具有上下文关联性 */ private void modifyTimeBase() { @@ -985,75 +989,75 @@ private void modifyTimeBase() { * 具体识别每个字段的值 */ private void timeNormalization() { - //标准时间解析 - LocalDateTime localDateTime = normStandardTime(); - if(localDateTime == null){ - normYear(); - normMonth(); - normDay(); - normMonthFuzzyDay();/**add by kexm*/ - normBaseRelated(); - normBaseTimeRelated(); - normCurRelated(); - normHour(); - normMinute(); - normSecond(); - normTotal(); - modifyTimeBase(); - localDateTime = LocalDateTime.of(1970, 1, 1, 0, 0); - } - String[] timeGrid = new String[6]; - timeGrid = timeContextOrigin.getTimeBase().split("-"); - - int tunitpointer = 5; - while (tunitpointer >= 0 && timeContext.getTunit()[tunitpointer] < 0) { - tunitpointer--; - } - for (int i = 0; i < tunitpointer; i++) { - if (timeContext.getTunit()[i] < 0) - timeContext.getTunit()[i] = Integer.parseInt(timeGrid[i]); - } - String[] resultTmp = new String[6]; - resultTmp[0] = String.valueOf(timeContext.getTunit()[0]); - if (timeContext.getTunit()[0] >= 10 && timeContext.getTunit()[0] < 100) { - resultTmp[0] = "19" + String.valueOf(timeContext.getTunit()[0]); - } - if (timeContext.getTunit()[0] > 0 && timeContext.getTunit()[0] < 10) { - resultTmp[0] = "200" + String.valueOf(timeContext.getTunit()[0]); - } - - for (int i = 1; i < 6; i++) { - resultTmp[i] = String.valueOf(timeContext.getTunit()[i]); - } - if (Integer.parseInt(resultTmp[0]) != -1) { - timeNorm += resultTmp[0] + "年"; - localDateTime = localDateTime.withYear(Integer.valueOf(resultTmp[0])); - if (Integer.parseInt(resultTmp[1]) != -1) { - timeNorm += resultTmp[1] + "月"; - localDateTime = localDateTime.withMonth(Integer.valueOf(resultTmp[1])); - if (Integer.parseInt(resultTmp[2]) != -1) { - timeNorm += resultTmp[2] + "日"; - localDateTime = localDateTime.withDayOfMonth(Integer.valueOf(resultTmp[2])); - if (Integer.parseInt(resultTmp[3]) != -1) { - timeNorm += resultTmp[3] + "时"; - localDateTime = localDateTime.withHour(Integer.valueOf(resultTmp[3])); - if (Integer.parseInt(resultTmp[4]) != -1) { - timeNorm += resultTmp[4] + "分"; - localDateTime = localDateTime.withMinute(Integer.valueOf(resultTmp[4])); - if (Integer.parseInt(resultTmp[5]) != -1) { - timeNorm += resultTmp[5] + "秒"; - localDateTime = localDateTime.withSecond(Integer.valueOf(resultTmp[5])); - } - } - } - } - } - } - timeContextOrigin.setTunit(timeContext.getTunit().clone()); - timeContext.setTimeBase(timeContextOrigin.getTimeBase()); - timeContext.setOldTimeBase(timeContextOrigin.getOldTimeBase()); - time = DateTimeConverterUtil.toDate(localDateTime); - timeNormFormat = DateTimeFormatterUtil.format(localDateTime, DateTimeFormatterUtil.YYYY_MM_DD_HH_MM_SS_FMT); + //标准时间解析 + LocalDateTime localDateTime = normStandardTime(); + if (localDateTime == null) { + normYear(); + normMonth(); + normDay(); + normMonthFuzzyDay();/**add by kexm*/ + normBaseRelated(); + normBaseTimeRelated(); + normCurRelated(); + normHour(); + normMinute(); + normSecond(); + normTotal(); + modifyTimeBase(); + localDateTime = LocalDateTime.of(1970, 1, 1, 0, 0); + } + String[] timeGrid = new String[6]; + timeGrid = timeContextOrigin.getTimeBase().split("-"); + + int tunitpointer = 5; + while (tunitpointer >= 0 && timeContext.getTunit()[tunitpointer] < 0) { + tunitpointer--; + } + for (int i = 0; i < tunitpointer; i++) { + if (timeContext.getTunit()[i] < 0) + timeContext.getTunit()[i] = Integer.parseInt(timeGrid[i]); + } + String[] resultTmp = new String[6]; + resultTmp[0] = String.valueOf(timeContext.getTunit()[0]); + if (timeContext.getTunit()[0] >= 10 && timeContext.getTunit()[0] < 100) { + resultTmp[0] = "19" + String.valueOf(timeContext.getTunit()[0]); + } + if (timeContext.getTunit()[0] > 0 && timeContext.getTunit()[0] < 10) { + resultTmp[0] = "200" + String.valueOf(timeContext.getTunit()[0]); + } + + for (int i = 1; i < 6; i++) { + resultTmp[i] = String.valueOf(timeContext.getTunit()[i]); + } + if (Integer.parseInt(resultTmp[0]) != -1) { + timeNorm += resultTmp[0] + "年"; + localDateTime = localDateTime.withYear(Integer.valueOf(resultTmp[0])); + if (Integer.parseInt(resultTmp[1]) != -1) { + timeNorm += resultTmp[1] + "月"; + localDateTime = localDateTime.withMonth(Integer.valueOf(resultTmp[1])); + if (Integer.parseInt(resultTmp[2]) != -1) { + timeNorm += resultTmp[2] + "日"; + localDateTime = localDateTime.withDayOfMonth(Integer.valueOf(resultTmp[2])); + if (Integer.parseInt(resultTmp[3]) != -1) { + timeNorm += resultTmp[3] + "时"; + localDateTime = localDateTime.withHour(Integer.valueOf(resultTmp[3])); + if (Integer.parseInt(resultTmp[4]) != -1) { + timeNorm += resultTmp[4] + "分"; + localDateTime = localDateTime.withMinute(Integer.valueOf(resultTmp[4])); + if (Integer.parseInt(resultTmp[5]) != -1) { + timeNorm += resultTmp[5] + "秒"; + localDateTime = localDateTime.withSecond(Integer.valueOf(resultTmp[5])); + } + } + } + } + } + } + timeContextOrigin.setTunit(timeContext.getTunit().clone()); + timeContext.setTimeBase(timeContextOrigin.getTimeBase()); + timeContext.setOldTimeBase(timeContextOrigin.getOldTimeBase()); + time = DateTimeConverterUtil.toDate(localDateTime); + timeNormFormat = DateTimeFormatterUtil.format(localDateTime, DateTimeFormatterUtil.YYYY_MM_DD_HH_MM_SS_FMT); } public Boolean getIsAllDayTime() { @@ -1064,10 +1068,9 @@ public void setIsAllDayTime(Boolean isAllDayTime) { this.isAllDayTime = isAllDayTime; } - @Override public String toString() { - return timeExpression + " ---> " + timeNormFormat; + return timeExpression + " ---> " + timeNormFormat; } /** @@ -1097,35 +1100,38 @@ private void preferFuture(int checkTimeIndex) { LocalDateTime localDateTime = LocalDateTime.now(); if (this.timeContextOrigin.getTimeBase() != null) { String[] ini = this.timeContextOrigin.getTimeBase().split("-"); - localDateTime = LocalDateTime.of(Integer.valueOf(ini[0]).intValue(), Integer.valueOf(ini[1]).intValue(), Integer.valueOf(ini[2]).intValue() - , Integer.valueOf(ini[3]).intValue(), Integer.valueOf(ini[4]).intValue(), Integer.valueOf(ini[5]).intValue()); + localDateTime = LocalDateTime.of(Integer.valueOf(ini[0]).intValue(), Integer.valueOf(ini[1]).intValue(), + Integer.valueOf(ini[2]).intValue() + , Integer.valueOf(ini[3]).intValue(), Integer.valueOf(ini[4]).intValue(), + Integer.valueOf(ini[5]).intValue()); } - + int curTime = localDateTime.get((TemporalField) TUNIT_MAP.get(checkTimeIndex)); //下午时间特殊处理,修复当前时间是上午10点,那么下午三点 会识别为明天下午三点问题 - if(checkTimeIndex == 3 && timeContext.getTunit()[3] >= 0 && timeContext.getTunit()[3] <= 11){ + if (checkTimeIndex == 3 && timeContext.getTunit()[3] >= 0 && timeContext.getTunit()[3] <= 11) { Pattern pattern = RegexEnum.NormHourAfternoon.getPattern(); Matcher match = pattern.matcher(timeExpression); if (match.find()) { - if (curTime < (timeContext.getTunit()[3] + 12)) { - return; - } + if (curTime < (timeContext.getTunit()[3] + 12)) { + return; + } + } + } else { + if (curTime < timeContext.getTunit()[checkTimeIndex]) { + return; } - }else{ - if (curTime < timeContext.getTunit()[checkTimeIndex]) { - return; - } } //准备增加的时间单位是被检查的时间的上一级,将上一级时间+1 localDateTime = localDateTime.plus(1, (TemporalUnit) TUNIT_MAP.get(checkTimeIndex - 1 + 10)); for (int i = 0; i < checkTimeIndex; i++) { - timeContext.getTunit()[i] = localDateTime.get((TemporalField) TUNIT_MAP.get(i)); + timeContext.getTunit()[i] = localDateTime.get((TemporalField) TUNIT_MAP.get(i)); } } /** * 根据上下文时间补充时间信息 + * * @param checkTimeIndex 序号 */ private void checkContextTime(int checkTimeIndex) { @@ -1140,7 +1146,7 @@ private void checkContextTime(int checkTimeIndex) { } isFirstTimeSolveContext = false; } - + /** * 过滤timeNLPList中无用的识别词。无用识别词识别出的时间是1970.01.01 00:00:00(fastTime=-28800000) * @@ -1153,18 +1159,19 @@ public static List filterTimeUnit(List timeNLPList) { } List list = new ArrayList<>(); for (TimeNLP t : timeNLPList) { - if (t!=null &&t.getTime().getTime() != -28800000) { + if (t != null && t.getTime().getTime() != -28800000) { list.add(t); } } return list; } - + /** * 根据修改时间设置unit值 + * * @param localDateTime 时间 */ - private void setUnitValues(LocalDateTime localDateTime){ + private void setUnitValues(LocalDateTime localDateTime) { String s = DateTimeFormatterUtil.format(localDateTime, "yyyy-MM-dd-HH-mm-ss"); String[] timeFin = s.split("-"); timeContext.getTunit()[0] = Integer.parseInt(timeFin[0]); @@ -1177,41 +1184,40 @@ private void setUnitValues(LocalDateTime localDateTime){ // get set - public String getTimeExpression() { - return timeExpression; - } + public String getTimeExpression() { + return timeExpression; + } - public void setTimeExpression(String timeExpression) { - this.timeExpression = timeExpression; - } + public void setTimeExpression(String timeExpression) { + this.timeExpression = timeExpression; + } - public String getTimeNorm() { - return timeNorm; - } + public String getTimeNorm() { + return timeNorm; + } - public void setTimeNorm(String timeNorm) { - this.timeNorm = timeNorm; - } + public void setTimeNorm(String timeNorm) { + this.timeNorm = timeNorm; + } - public String getTimeNormFormat() { - return timeNormFormat; - } + public String getTimeNormFormat() { + return timeNormFormat; + } - public void setTimeNormFormat(String timeNormFormat) { - this.timeNormFormat = timeNormFormat; - } + public void setTimeNormFormat(String timeNormFormat) { + this.timeNormFormat = timeNormFormat; + } - public TimeContext getTimeContext() { - return timeContext; - } + public TimeContext getTimeContext() { + return timeContext; + } - public void setTimeContext(TimeContext timeContext) { - this.timeContext = timeContext; - } + public void setTimeContext(TimeContext timeContext) { + this.timeContext = timeContext; + } - public void setTime(Date time) { - this.time = time; - } - + public void setTime(Date time) { + this.time = time; + } } diff --git a/src/test/java/com/xkzhangsan/time/test/TimeNLPUtilTest.java b/src/test/java/com/xkzhangsan/time/test/TimeNLPUtilTest.java index 6d5455e..057430c 100644 --- a/src/test/java/com/xkzhangsan/time/test/TimeNLPUtilTest.java +++ b/src/test/java/com/xkzhangsan/time/test/TimeNLPUtilTest.java @@ -1,243 +1,254 @@ package com.xkzhangsan.time.test; -import java.util.List; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.Test; - import com.xkzhangsan.time.calculator.DateTimeCalculatorUtil; import com.xkzhangsan.time.formatter.DateTimeFormatterUtil; import com.xkzhangsan.time.nlp.TimeNLP; import com.xkzhangsan.time.nlp.TimeNLPUtil; import com.xkzhangsan.time.utils.CollectionUtil; +import org.junit.Test; + +import java.util.List; +import java.util.concurrent.*; /** * 时间自然语言解析工具类测试 - * + * * @author xkzhangsan */ public class TimeNLPUtilTest { - /** - * 常见实例 - */ - @Test - public void timeNLPTest() { - List timeNLPList = TimeNLPUtil.parse("Hi,all.下周一下午三点开会"); - System.out.println("Hi,all.下周一下午三点开会"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("早上六点起床"); - System.out.println("早上六点起床"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("周一开会"); - System.out.println("周一开会"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("下下周一开会"); - System.out.println("下下周一开会"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("6:30 起床"); - System.out.println("6:30 起床"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("6-3 春游"); - System.out.println("6-3 春游"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("6月3春游"); - System.out.println("6月3春游"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("明天早上跑步"); - System.out.println("明天早上跑步"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("本周日到下周日出差"); - System.out.println("本周日到下周日出差"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(1).getTime()) + "-" - + timeNLPList.get(1).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("周四下午三点到五点开会"); - System.out.println("周四下午三点到五点开会"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(1).getTime()) + "-" - + timeNLPList.get(1).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("《辽宁日报》今日报道,7月18日辽宁召开省委常委扩大会,会议从下午两点半开到六点半,主要议题为:落实中央巡视整改要求。", - "2016-07-19-00-00-00"); - System.out.println("《辽宁日报》今日报道,7月18日辽宁召开省委常委扩大会,会议从下午两点半开到六点半,主要议题为:落实中央巡视整改要求。"); - for (int i = 0; i < timeNLPList.size(); i++) { - System.out.println("时间文本:" + timeNLPList.get(i).getTimeExpression() + ",对应时间:" - + timeNLPList.get(i).getTimeNormFormat()); - } - } - - /** - * 标准时间 - */ - @Test - public void normStandardTimeTest() { - List timeNLPList = TimeNLPUtil.parse("2016-07-19对应时间"); - System.out.println("2016-07-19对应时间"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("2016-07-19 15:30:10对应时间"); - System.out.println("2016-07-19 15:30:10对应时间"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("2016-07-19 15:30对应时间"); - System.out.println("2016-07-19 15:30对应时间"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - } - - /** - * 时分秒,前后解析 - */ - @Test - public void normHourTest() { - System.out.println("当前时间:"+DateTimeCalculatorUtil.getEpochMilliFormat()); - List timeNLPList = TimeNLPUtil.parse("1小时后对应时间"); - System.out.println("1小时后对应时间"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("1钟头后对应时间"); - System.out.println("1钟头后对应时间"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("1个半小时后对应时间"); - System.out.println("1个半小时后对应时间"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("1小时50分钟后对应时间"); - System.out.println("1小时50分钟后对应时间"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("1小时50分后对应时间"); - System.out.println("1小时50分后对应时间"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("10分钟后对应时间"); - System.out.println("10分钟后对应时间"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("10分钟30秒后对应时间"); - System.out.println("10分钟30秒后对应时间"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("50秒后对应时间"); - System.out.println("50秒后对应时间"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("180秒后对应时间"); - System.out.println("180秒后对应时间"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - timeNLPList = TimeNLPUtil.parse("1个半小时前对应时间"); - System.out.println("1个半小时前对应时间"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - } - - /** - * 当前时间是上午10点,那么下午三点 会识别为明天下午三点问题 - */ - @Test - public void normHourTest2() { - System.out.println("当前时间:"+DateTimeCalculatorUtil.getEpochMilliFormat()); - List timeNLPList = TimeNLPUtil.parse("下午3点对应时间"); - System.out.println("下午3点对应时间"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - } - - /** - * 数字解析异常问题验证 - */ - @Test - public void normDecimalStrTest() { - System.out.println("当前时间:"+DateTimeCalculatorUtil.getEpochMilliFormat()); - List timeNLPList = TimeNLPUtil.parse("4.5对应时间"); - System.out.println("4.5对应时间"); - if(CollectionUtil.isNotEmpty(timeNLPList)){ - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - }else{ - System.out.println("无匹配结果"); - } - - timeNLPList = TimeNLPUtil.parse("4.5元日对应时间"); - System.out.println("4.5元日对应时间"); - if(CollectionUtil.isNotEmpty(timeNLPList)){ - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - }else{ - System.out.println("无匹配结果"); - } - - timeNLPList = TimeNLPUtil.parse("4.5日对应时间"); - System.out.println("4.5日对应时间"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - } - - /** - * 并发执行,可限制时间内完成功能 - * - * @throws TimeoutException - * @throws ExecutionException - * @throws InterruptedException - */ - @Test - public void parseConcurrentTest() throws InterruptedException, ExecutionException, TimeoutException { - //1.并发执行 - System.out.println("当前时间:"+DateTimeCalculatorUtil.getEpochMilliFormat()); - List timeNLPList = TimeNLPUtil.parseConcurrent("下午3点对应时间"); - System.out.println("下午3点对应时间"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" - + timeNLPList.get(0).getIsAllDayTime()); - - //2.限时30毫秒完成 - List timeNLPList2 = TimeNLPUtil.parseConcurrent("下午3点对应时间", 30, TimeUnit.MILLISECONDS); - System.out.println("下午3点对应时间"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList2.get(0).getTime()) + "-" - + timeNLPList2.get(0).getIsAllDayTime()); - - //3.使用自定义线程池 - ExecutorService executorService = Executors.newCachedThreadPool(); - List timeNLPList3 = TimeNLPUtil.parseConcurrent("下午3点对应时间", 30,TimeUnit.MILLISECONDS, executorService); - System.out.println("下午3点对应时间"); - System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList3.get(0).getTime()) + "-" - + timeNLPList3.get(0).getIsAllDayTime()); - } + /** + * 常见实例 + */ + @Test + public void timeNLPTest() { + List timeNLPList = TimeNLPUtil.parse("昨天10点去公司开会"); + System.out.println("昨天10点去公司开会"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("前天下午3点"); + System.out.println("前天下午3点"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("大前天上午5点"); + System.out.println("大前天上午5点"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("3个月之前"); + System.out.println("3个月之前"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("早上六点起床"); + System.out.println("早上六点起床"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("周一开会"); + System.out.println("周一开会"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("下下周一开会"); + System.out.println("下下周一开会"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("6:30 起床"); + System.out.println("6:30 起床"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("6-3 春游"); + System.out.println("6-3 春游"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("6月3春游"); + System.out.println("6月3春游"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("明天早上跑步"); + System.out.println("明天早上跑步"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("本周日到下周日出差"); + System.out.println("本周日到下周日出差"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(1).getTime()) + "-" + + timeNLPList.get(1).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("周四下午三点到五点开会"); + System.out.println("周四下午三点到五点开会"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(1).getTime()) + "-" + + timeNLPList.get(1).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("《辽宁日报》今日报道,7月18日辽宁召开省委常委扩大会,会议从下午两点半开到六点半,主要议题为:落实中央巡视整改要求。", + "2016-07-19-00-00-00"); + System.out.println("《辽宁日报》今日报道,7月18日辽宁召开省委常委扩大会,会议从下午两点半开到六点半,主要议题为:落实中央巡视整改要求。"); + for (int i = 0; i < timeNLPList.size(); i++) { + System.out.println("时间文本:" + timeNLPList.get(i).getTimeExpression() + ",对应时间:" + + timeNLPList.get(i).getTimeNormFormat()); + } + } + + /** + * 标准时间 + */ + @Test + public void normStandardTimeTest() { + List timeNLPList = TimeNLPUtil.parse("2016-07-19对应时间"); + System.out.println("2016-07-19对应时间"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("2016-07-19 15:30:10对应时间"); + System.out.println("2016-07-19 15:30:10对应时间"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("2016-07-19 15:30对应时间"); + System.out.println("2016-07-19 15:30对应时间"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + } + + /** + * 时分秒,前后解析 + */ + @Test + public void normHourTest() { + System.out.println("当前时间:" + DateTimeCalculatorUtil.getEpochMilliFormat()); + List timeNLPList = TimeNLPUtil.parse("1小时后对应时间"); + System.out.println("1小时后对应时间"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("1钟头后对应时间"); + System.out.println("1钟头后对应时间"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("1个半小时后对应时间"); + System.out.println("1个半小时后对应时间"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("1小时50分钟后对应时间"); + System.out.println("1小时50分钟后对应时间"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("1小时50分后对应时间"); + System.out.println("1小时50分后对应时间"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("10分钟后对应时间"); + System.out.println("10分钟后对应时间"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("10分钟30秒后对应时间"); + System.out.println("10分钟30秒后对应时间"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("50秒后对应时间"); + System.out.println("50秒后对应时间"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("180秒后对应时间"); + System.out.println("180秒后对应时间"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("1个半小时前对应时间"); + System.out.println("1个半小时前对应时间"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + } + + /** + * 当前时间是上午10点,那么下午三点 会识别为明天下午三点问题 + */ + @Test + public void normHourTest2() { + System.out.println("当前时间:" + DateTimeCalculatorUtil.getEpochMilliFormat()); + List timeNLPList = TimeNLPUtil.parse("下午3点对应时间"); + System.out.println("下午3点对应时间"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + } + + /** + * 数字解析异常问题验证 + */ + @Test + public void normDecimalStrTest() { + System.out.println("当前时间:" + DateTimeCalculatorUtil.getEpochMilliFormat()); + List timeNLPList = TimeNLPUtil.parse("4.5对应时间"); + System.out.println("4.5对应时间"); + if (CollectionUtil.isNotEmpty(timeNLPList)) { + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + } else { + System.out.println("无匹配结果"); + } + + timeNLPList = TimeNLPUtil.parse("4.5元日对应时间"); + System.out.println("4.5元日对应时间"); + if (CollectionUtil.isNotEmpty(timeNLPList)) { + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + } else { + System.out.println("无匹配结果"); + } + + timeNLPList = TimeNLPUtil.parse("4.5日对应时间"); + System.out.println("4.5日对应时间"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + } + + /** + * 并发执行,可限制时间内完成功能 + * + * @throws TimeoutException + * @throws ExecutionException + * @throws InterruptedException + */ + @Test + public void parseConcurrentTest() throws InterruptedException, ExecutionException, TimeoutException { + //1.并发执行 + System.out.println("当前时间:" + DateTimeCalculatorUtil.getEpochMilliFormat()); + List timeNLPList = TimeNLPUtil.parseConcurrent("下午3点对应时间"); + System.out.println("下午3点对应时间"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + //2.限时30毫秒完成 + List timeNLPList2 = TimeNLPUtil.parseConcurrent("下午3点对应时间", 30, TimeUnit.MILLISECONDS); + System.out.println("下午3点对应时间"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList2.get(0).getTime()) + "-" + + timeNLPList2.get(0).getIsAllDayTime()); + + //3.使用自定义线程池 + ExecutorService executorService = Executors.newCachedThreadPool(); + List timeNLPList3 = TimeNLPUtil.parseConcurrent("下午3点对应时间", 30, TimeUnit.MILLISECONDS, + executorService); + System.out.println("下午3点对应时间"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList3.get(0).getTime()) + "-" + + timeNLPList3.get(0).getIsAllDayTime()); + } } From fec2b369324731d2c5ecd2293b70be221ad17ccf Mon Sep 17 00:00:00 2001 From: abroad-crawler <287510038@qq.com> Date: Wed, 1 Sep 2021 16:52:07 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9NLP=E8=AF=86=E5=88=ABbug,?= =?UTF-8?q?=20"=E5=89=8D=E5=B9=B4"=E7=AD=89=E8=AE=A1=E7=AE=97=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/xkzhangsan/time/nlp/TimeNLP.java | 4 ++-- .../com/xkzhangsan/time/test/TimeNLPUtilTest.java | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/xkzhangsan/time/nlp/TimeNLP.java b/src/main/java/com/xkzhangsan/time/nlp/TimeNLP.java index 73fb46f..6018909 100644 --- a/src/main/java/com/xkzhangsan/time/nlp/TimeNLP.java +++ b/src/main/java/com/xkzhangsan/time/nlp/TimeNLP.java @@ -738,14 +738,14 @@ private void normCurRelated() { Matcher match = pattern.matcher(timeExpression); if (match.find()) { flag[0] = true; - localDateTime = localDateTime.minusYears(-2); + localDateTime = localDateTime.minusYears(2); } pattern = RegexEnum.NormCurRelatedYearBefore.getPattern(); match = pattern.matcher(timeExpression); if (match.find()) { flag[0] = true; - localDateTime = localDateTime.minusYears(-1); + localDateTime = localDateTime.minusYears(1); } pattern = RegexEnum.NormCurRelatedYear.getPattern(); diff --git a/src/test/java/com/xkzhangsan/time/test/TimeNLPUtilTest.java b/src/test/java/com/xkzhangsan/time/test/TimeNLPUtilTest.java index 057430c..25370dd 100644 --- a/src/test/java/com/xkzhangsan/time/test/TimeNLPUtilTest.java +++ b/src/test/java/com/xkzhangsan/time/test/TimeNLPUtilTest.java @@ -22,7 +22,18 @@ public class TimeNLPUtilTest { */ @Test public void timeNLPTest() { - List timeNLPList = TimeNLPUtil.parse("昨天10点去公司开会"); + + List timeNLPList = TimeNLPUtil.parse("去年5月"); + System.out.println("去年5月"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("前年3月21日"); + System.out.println("前年3月21日"); + System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + + timeNLPList.get(0).getIsAllDayTime()); + + timeNLPList = TimeNLPUtil.parse("昨天10点去公司开会"); System.out.println("昨天10点去公司开会"); System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList.get(0).getTime()) + "-" + timeNLPList.get(0).getIsAllDayTime());