I created a Java application that incorporates Dialogflow, so The procedure is briefly summarized as a memorandum. When actually using it, I think that you will write the one described here + Swing etc.
Dialogflow
Maven
Dialogflow is a natural language understanding platform that facilitates the design of conversational user interfaces and integration into mobile apps, web applications, devices, bots, interactive voice response systems, and more. Source: Official Documents
It is used for natural language analysis of Google Home, and is used for AWS [Amazon Lex](https://aws.amazon.com/jp/lex/'https://aws.amazon.com/jp Similar systems include / lex /').
Create a GCP project to connect Dialogflow.
Select Project
-> New Project
.project name
and click Create
.
Create Agent
.Japanese --ja
for DEFAULT LANGUAGE
and created project
for GOOGLE PROJECT
, and press CREATE
.
Now let's create a Dialogflow interaction model. For information on creating an interaction model, see the Official Documentation (https://cloud.google.com/dialogflow/docs/quick/build-agent'https://cloud.google.com/dialogflow/docs/quick/build) -agent') and others There is a lot of information on the net, so I will omit it.
This time, it returns "Hello Dialogflow!" To the utterance "test". I have created a test interaction model.
to which the Dialogflow interaction model is connected
from the top of the screen, and open the service account
from ʻIAM and management`.Create Service Account
and enter the Service Account Name
.Select Role``, select
Dialogflow->
Dialogflow API Client` and continue.Create Key
, select JSON
and click Create
.File
-> New
-> Maven Project
to create a maven project.Create a simple project
and proceed to the next.group Id
and artifact Id
and press Finish.Create packages and class files in the created Maven project.
src / main / java
and select New
-> Package
.Name
and click Finish
.created package
and select New
-> Class
.Name
, checkpublic static void main (String [] args)
, and click Finish
.You have now created a package and a class with the main methods in your Maven project.
Rewrite the file called pom.xml in the created Maven project as follows.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<!--groupId and artifactId...Replace with the one you entered when you created the Maven project.-->
<modelVersion>4.0.0</modelVersion>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<finalName>...</finalName>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!--of mainClass...Is the package name.The class name.-->
<mainClass>...</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>mark-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>5.1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-dialogflow</artifactId>
</dependency>
</dependencies>
</project>
When you're done, save and apply pom.xml.
pom.xml
and select Maven
-> `Update Project``.created Maven project
from the available Maven codebase and continue with ʻOK`.Rewrite the class file containing the main method created earlier as follows.
Main.java
//Replace the package name as appropriate.
package tokyo.penguin_syan.dialogflowTest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import com.google.api.client.util.Maps;
import com.google.cloud.dialogflow.v2.DetectIntentResponse;
import com.google.cloud.dialogflow.v2.QueryInput;
import com.google.cloud.dialogflow.v2.QueryResult;
import com.google.cloud.dialogflow.v2.SessionName;
import com.google.cloud.dialogflow.v2.SessionsClient;
import com.google.cloud.dialogflow.v2.TextInput;
import com.google.cloud.dialogflow.v2.TextInput.Builder;
public class Main {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
List<String> text = new ArrayList<String>();
String input;
Map<String, QueryResult> ret;
//sessionId is a suitable number for testing.
int sessionId = 123456789;
do {
System.out.print("\nYou : ");
input = userInput.next();
if(input.equals("exit"))
System.exit(0);
text.add(input);
ret = new HashMap<>();
try {
//In the argument---Is the project described in the downloaded json file_Please replace with id.
ret.putAll(detectIntentTexts(---,
text, String.valueOf(sessionId++), "ja"));
System.out.format("Agent: %s\n", ret.get(input).getFulfillmentText());
} catch (Exception e) {
e.printStackTrace();
}
}while(true);
}
public static Map<String, QueryResult> detectIntentTexts(
String projectId,
List<String> texts,
String sessionId,
String languageCode) throws Exception {
Map<String, QueryResult> queryResults = Maps.newHashMap();
try (SessionsClient sessionsClient = SessionsClient.create()) {
SessionName session = SessionName.of(projectId, sessionId);
for (String text : texts) {
Builder textInput = TextInput.newBuilder().setText(text).setLanguageCode(languageCode);
QueryInput queryInput = QueryInput.newBuilder().setText(textInput).build();
DetectIntentResponse response = sessionsClient.detectIntent(session, queryInput);
QueryResult queryResult = response.getQueryResult();
queryResults.put(text, queryResult);
}
}
return queryResults;
}
}
Maven project
in the Package Explorer.Run
-> Maven install
.BUILD SUCCESS
is displayed on the console.Create a bat file for execution.
dialogflowTest_java.bat
@echo off
rem ---Enter the name of the downloaded json file for authentication.
set GOOGLE_APPLICATION_CREDENTIALS=---.json
java -jar test-jar-with-dependencies.jar
Organize the directory structure for execution.
test-jar-with-dependencies.jar
, downloaded json file
, and created bat file
all in the same folder.
(Test-jar-with-dependencies.jar is in the target folder in the Maven project)Directory structure
.
├ test-jar-with-dependencies.jar
├ ---.json
└ dialogflowTest_java.bat
To execute it, start the created bat file.
I was able to execute it safely. ٩ (๑ • ㅂ •) ۶ Wow
Recommended Posts