Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 8341c5c

Browse files
authored
[Samples] Add 40.timex-resolution sample (#1145)
* Add pom file * Add root files * Add Startup class * Add package-info file * Add documentation files * Change HashMap to LinkedHashMap in Recognizers-Text Datetime due to an issue * Add sample as module in pom.xml of samples folder
1 parent 1248b45 commit 8341c5c

File tree

14 files changed

+1004
-2
lines changed

14 files changed

+1004
-2
lines changed

libraries/bot-dialogs/src/main/java/com/microsoft/recognizers/text/datetime/parsers/BaseMergedDateTimeParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ public SortedMap<String, Object> dateTimeResolution(DateTimeParseResult slot) {
438438
}
439439

440440
List<Map<String, String>> resolutions = new ArrayList<>();
441-
Map<String, Object> res = new HashMap<>();
441+
LinkedHashMap<String, Object> res = new LinkedHashMap<>();
442442

443443
DateTimeResolutionResult val = (DateTimeResolutionResult)slot.getValue();
444444
if (val == null) {

libraries/bot-dialogs/src/main/java/com/microsoft/recognizers/text/datetime/utilities/TimexUtility.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.time.temporal.ChronoUnit;
1414
import java.util.ArrayList;
1515
import java.util.HashMap;
16+
import java.util.LinkedHashMap;
1617
import java.util.List;
1718
import java.util.Map;
1819
import java.util.stream.Collectors;
@@ -281,7 +282,7 @@ public static String mergeTimexAlternatives(String timex1, String timex2) {
281282
return timex1 + Constants.CompositeTimexDelimiter + timex2;
282283
}
283284

284-
public static Map<String, Object> processDoubleTimex(Map<String, Object> resolutionDic, String futureKey, String pastKey, String originTimex) {
285+
public static LinkedHashMap<String, Object> processDoubleTimex(LinkedHashMap<String, Object> resolutionDic, String futureKey, String pastKey, String originTimex) {
285286
String[] timexes = originTimex.split(Constants.CompositeTimexSplit);
286287

287288
if (!resolutionDic.containsKey(futureKey) || !resolutionDic.containsKey(pastKey) || timexes.length != 2) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Microsoft Corporation. All rights reserved.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE

samples/40.timex-resolution/README.md

Lines changed: 357 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>com.microsoft.bot.sample</groupId>
9+
<artifactId>bot-timex-resolution</artifactId>
10+
<version>sample</version>
11+
<packaging>jar</packaging>
12+
13+
<name>${project.groupId}:${project.artifactId}</name>
14+
<description>This package contains a Java Timex Resolution sample.</description>
15+
<url>http://maven.apache.org</url>
16+
17+
<parent>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-parent</artifactId>
20+
<version>2.4.0</version>
21+
<relativePath/>
22+
</parent>
23+
24+
<licenses>
25+
<license>
26+
<name>MIT License</name>
27+
<url>http://www.opensource.org/licenses/mit-license.php</url>
28+
</license>
29+
</licenses>
30+
31+
<developers>
32+
<developer>
33+
<name>Bot Framework Development</name>
34+
<email></email>
35+
<organization>Microsoft</organization>
36+
<organizationUrl>https://dev.botframework.com/</organizationUrl>
37+
</developer>
38+
</developers>
39+
40+
<properties>
41+
<java.version>1.8</java.version>
42+
<maven.compiler.target>1.8</maven.compiler.target>
43+
<maven.compiler.source>1.8</maven.compiler.source>
44+
<start-class>com.microsoft.bot.sample.timex.resolution.Application</start-class>
45+
</properties>
46+
47+
<dependencies>
48+
<dependency>
49+
<groupId>com.microsoft.bot</groupId>
50+
<artifactId>bot-dialogs</artifactId>
51+
<version>4.6.0-preview9</version>
52+
</dependency>
53+
</dependencies>
54+
55+
<profiles>
56+
<profile>
57+
<id>build</id>
58+
<activation>
59+
<activeByDefault>true</activeByDefault>
60+
</activation>
61+
<build>
62+
<resources>
63+
<resource>
64+
<directory>src/main/resources</directory>
65+
<filtering>false</filtering>
66+
</resource>
67+
</resources>
68+
<plugins>
69+
<plugin>
70+
<artifactId>maven-compiler-plugin</artifactId>
71+
<version>3.8.1</version>
72+
</plugin>
73+
<plugin>
74+
<artifactId>maven-war-plugin</artifactId>
75+
<version>3.2.3</version>
76+
<configuration>
77+
<warSourceDirectory>src/main/webapp</warSourceDirectory>
78+
</configuration>
79+
</plugin>
80+
<plugin>
81+
<groupId>org.springframework.boot</groupId>
82+
<artifactId>spring-boot-maven-plugin</artifactId>
83+
<executions>
84+
<execution>
85+
<goals>
86+
<goal>repackage</goal>
87+
</goals>
88+
<configuration>
89+
<mainClass>com.microsoft.bot.sample.timex.resolution.Application</mainClass>
90+
</configuration>
91+
</execution>
92+
</executions>
93+
</plugin>
94+
<plugin>
95+
<groupId>com.microsoft.azure</groupId>
96+
<artifactId>azure-webapp-maven-plugin</artifactId>
97+
<version>1.12.0</version>
98+
<configuration>
99+
<schemaVersion>V2</schemaVersion>
100+
<resourceGroup>${groupname}</resourceGroup>
101+
<appName>${botname}</appName>
102+
<appSettings>
103+
<property>
104+
<name>JAVA_OPTS</name>
105+
<value>-Dserver.port=80</value>
106+
</property>
107+
</appSettings>
108+
<runtime>
109+
<os>linux</os>
110+
<javaVersion>Java 8</javaVersion>
111+
<webContainer>Java SE</webContainer>
112+
</runtime>
113+
<deployment>
114+
<resources>
115+
<resource>
116+
<directory>${project.basedir}/target</directory>
117+
<includes>
118+
<include>*.jar</include>
119+
</includes>
120+
</resource>
121+
</resources>
122+
</deployment>
123+
</configuration>
124+
</plugin>
125+
</plugins>
126+
</build>
127+
</profile>
128+
129+
<profile>
130+
<id>publish</id>
131+
<build>
132+
<plugins>
133+
134+
<plugin>
135+
<groupId>org.apache.maven.plugins</groupId>
136+
<artifactId>maven-compiler-plugin</artifactId>
137+
</plugin>
138+
<plugin>
139+
<artifactId>maven-war-plugin</artifactId>
140+
<version>3.2.3</version>
141+
<configuration>
142+
<warSourceDirectory>src/main/webapp</warSourceDirectory>
143+
</configuration>
144+
</plugin>
145+
146+
<plugin>
147+
<groupId>org.sonatype.plugins</groupId>
148+
<artifactId>nexus-staging-maven-plugin</artifactId>
149+
<version>1.6.8</version>
150+
<extensions>true</extensions>
151+
<configuration>
152+
<skipRemoteStaging>true</skipRemoteStaging>
153+
<serverId>ossrh</serverId>
154+
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
155+
<autoReleaseAfterClose>true</autoReleaseAfterClose>
156+
</configuration>
157+
</plugin>
158+
159+
<plugin>
160+
<groupId>org.apache.maven.plugins</groupId>
161+
<artifactId>maven-gpg-plugin</artifactId>
162+
<executions>
163+
<execution>
164+
<id>sign-artifacts</id>
165+
<phase>verify</phase>
166+
<goals>
167+
<goal>sign</goal>
168+
</goals>
169+
</execution>
170+
</executions>
171+
</plugin>
172+
<plugin>
173+
<groupId>org.apache.maven.plugins</groupId>
174+
<artifactId>maven-source-plugin</artifactId>
175+
<executions>
176+
<execution>
177+
<id>attach-sources</id>
178+
<goals>
179+
<goal>jar</goal>
180+
</goals>
181+
</execution>
182+
</executions>
183+
</plugin>
184+
<plugin>
185+
<groupId>org.apache.maven.plugins</groupId>
186+
<artifactId>maven-javadoc-plugin</artifactId>
187+
<configuration>
188+
<source>8</source>
189+
<failOnError>false</failOnError>
190+
</configuration>
191+
<executions>
192+
<execution>
193+
<id>attach-javadocs</id>
194+
<goals>
195+
<goal>jar</goal>
196+
</goals>
197+
</execution>
198+
</executions>
199+
</plugin>
200+
</plugins>
201+
</build>
202+
</profile>
203+
</profiles>
204+
</project>
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.bot.sample.timex.resolution;
5+
6+
import com.microsoft.recognizers.text.Culture;
7+
import com.microsoft.recognizers.text.ModelResult;
8+
import com.microsoft.recognizers.text.datetime.DateTimeRecognizer;
9+
10+
import java.util.LinkedHashSet;
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
/**
15+
* TIMEX expressions are designed to represent ambiguous rather than definite dates.
16+
* For example:
17+
* "Monday" could be any Monday ever.
18+
* "May 5th" could be any one of the possible May 5th in the past or the future.
19+
* TIMEX does not represent ambiguous times. So if the natural language mentioned 4 o'clock
20+
* it could be either 4AM or 4PM. For that the recognizer (and by extension LUIS) would return two TIMEX expressions.
21+
* A TIMEX expression can include a date and time parts. So ambiguity of date can be combined with multiple results.
22+
* Code that deals with TIMEX expressions is frequently dealing with sets of TIMEX expressions.
23+
*/
24+
public final class Ambiguity {
25+
26+
private Ambiguity() {
27+
}
28+
29+
/**
30+
* This method avoid ambiguity obtaining 2 values, backwards and forwards in the calendar.
31+
*/
32+
public static void dateAmbiguity() {
33+
// Run the recognizer.
34+
List<ModelResult> results = DateTimeRecognizer.recognizeDateTime(
35+
"Either Saturday or Sunday would work.",
36+
Culture.English);
37+
38+
// We should find two results in this example.
39+
for (ModelResult result : results) {
40+
// The resolution includes two example values: going backwards and forwards from NOW in the calendar.
41+
LinkedHashSet<String> distinctTimexExpressions = new LinkedHashSet<>();
42+
List<Map<String, String>> values = (List<Map<String, String>>) result.resolution.get("values");
43+
for (Map<String, String> value : values) {
44+
// Each result includes a TIMEX expression that captures the inherent date but not time ambiguity.
45+
// We are interested in the distinct set of TIMEX expressions.
46+
String timex = value.get("timex");
47+
if (timex != null) {
48+
distinctTimexExpressions.add(timex);
49+
}
50+
51+
// There is also either a "value" property on each value or "start" and "end".
52+
// If you use ToString() on a TimeProperty object you will get same "value".
53+
}
54+
55+
// The TIMEX expression captures date ambiguity so there will be a single distinct
56+
// expression for each result.
57+
String output = String.format("%s ( %s )", result.text, String.join(",", distinctTimexExpressions));
58+
System.out.println(output);
59+
60+
// The result also includes a reference to the original string
61+
// but note the start and end index are both inclusive.
62+
}
63+
}
64+
65+
/**
66+
* This method avoid ambiguity obtaining 2 values, one for AM and one for PM.
67+
*/
68+
public static void timeAmbiguity() {
69+
// Run the recognizer.
70+
List<ModelResult> results = DateTimeRecognizer.recognizeDateTime(
71+
"We would like to arrive at 4 o'clock or 5 o'clock.",
72+
Culture.English);
73+
74+
// We should find two results in this example.
75+
for (ModelResult result : results) {
76+
// The resolution includes two example values: one for AM and one for PM.
77+
LinkedHashSet<String> distinctTimexExpressions = new LinkedHashSet<>();
78+
List<Map<String, String>> values = (List<Map<String, String>>) result.resolution.get("values");
79+
for (Map<String, String> value : values) {
80+
// Each result includes a TIMEX expression that captures the inherent date but not time ambiguity.
81+
// We are interested in the distinct set of TIMEX expressions.
82+
String timex = value.get("timex");
83+
if (timex != null) {
84+
distinctTimexExpressions.add(timex);
85+
}
86+
}
87+
88+
// TIMEX expressions don't capture time ambiguity so there will be two distinct expressions for each result.
89+
String output = String.format("%s ( %s )", result.text, String.join(",", distinctTimexExpressions));
90+
System.out.println(output);
91+
}
92+
}
93+
94+
/**
95+
* This method avoid ambiguity obtaining 4 different values,
96+
* backwards and forwards in the calendar and then AM and PM.
97+
*/
98+
public static void dateTimeAmbiguity() {
99+
// Run the recognizer.
100+
List<ModelResult> results = DateTimeRecognizer.recognizeDateTime(
101+
"It will be ready Wednesday at 5 o'clock.",
102+
Culture.English);
103+
104+
// We should find a single result in this example.
105+
for (ModelResult result : results) {
106+
// The resolution includes four example values: backwards and forward in the calendar and then AM and PM.
107+
LinkedHashSet<String> distinctTimexExpressions = new LinkedHashSet<>();
108+
List<Map<String, String>> values = (List<Map<String, String>>) result.resolution.get("values");
109+
for (Map<String, String> value : values) {
110+
// Each result includes a TIMEX expression that captures the inherent date but not time ambiguity.
111+
// We are interested in the distinct set of TIMEX expressions.
112+
String timex = value.get("timex");
113+
if (timex != null) {
114+
distinctTimexExpressions.add(timex);
115+
}
116+
}
117+
118+
// TIMEX expressions don't capture time ambiguity so there will be two distinct expressions for each result.
119+
String output = String.format("%s ( %s )", result.text, String.join(",", distinctTimexExpressions));
120+
System.out.println(output);
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)