Merge pull request #7 from jlengrand/feature/create_name_entity

Feature/create name entity
This commit is contained in:
julien Lengrand-Lambert
2019-10-16 16:05:45 +02:00
committed by GitHub
9 changed files with 140 additions and 101 deletions

1
.gitignore vendored
View File

@@ -52,3 +52,4 @@ resources/templates/css/**
resources/templates/js/**
resources/templates/fonts/**
dist
.classpath

View File

@@ -1,49 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@@ -51,3 +51,4 @@ resources/templates/css/**
resources/templates/js/**
resources/templates/fonts/**
dist
.classpath

View File

@@ -1,77 +1,31 @@
import java.io.IOException;
import java.util.List;
import com.google.cloud.dialogflow.v2beta1.EntityType;
import com.google.cloud.dialogflow.v2beta1.EntityTypeName;
import com.google.cloud.dialogflow.v2beta1.EntityTypesClient;
import com.google.cloud.dialogflow.v2beta1.ProjectAgentName;
import com.google.cloud.dialogflow.v2beta1.Agent;
import com.google.cloud.dialogflow.v2beta1.AgentsClient;
import com.google.cloud.dialogflow.v2beta1.ProjectName;
import com.google.cloud.dialogflow.v2beta1.EntityType.Entity;
public class CreateAgent {
private static final String GOOGLE_AUTH_ENV_NAME = "GOOGLE_APPLICATION_CREDENTIALS";
private static final String PROJECT_NAME = "dialogflow-fun";
private static final String AGENT_NAME = "dialogflow-fun-agent";
private static final String AGENT_ID = "ac522b80-e75b-40cd-9493-269fbb4ef634";
private static final String ENTITY_TYPE_ID = "Developer";
public static void main(String[] args) {
private static EntityCreator entityCreator = new EntityCreator();
public static void main(String[] args) throws IOException {
if (!googleAuthOk()) {
System.out.println("Error with Google Authentication. Exiting...");
System.exit(0);
}
getAgentInfo();
getEntitiesInfo();
entityCreator.deleteAllEntityTypes();
entityCreator.createContactEntityType();
System.out.println("Done");
}
public static void getEntitiesInfo() {
// ENTITY TYPES
// https://googleapis.dev/java/google-cloud-clients/latest/index.html?com/google/cloud/dialogflow/v2/package-summary.html
// * **System** - entities that are defined by the Dialogflow API for common
// data types such as date, time, currency, and so on. A system entity is
// represented by the `EntityType` type.
// * **Developer** - entities that are defined by you that represent actionable
// data that is meaningful to your application. For example, you could define a
// `pizza.sauce` entity for red or white pizza sauce, a `pizza.cheese` entity
// for the different types of cheese on a pizza, a `pizza.topping` entity for
// different toppings, and so on. A developer entity is represented by the
// `EntityType` type.
// * **User** - entities that are built for an individual user such as
// favorites, preferences, playlists, and so on. A user entity is represented by
// the [SessionEntityType][google.cloud.dialogflow.v2.SessionEntityType] type.
System.out.println("==========");
System.out.println("Getting Entities info");
try {
EntityTypesClient entityTypesClient = EntityTypesClient.create();
ProjectAgentName parent = ProjectAgentName.of(PROJECT_NAME);
for (EntityType entityType : entityTypesClient.listEntityTypes(parent).iterateAll()) {
System.out.println(entityType.getDisplayName());
List<Entity> entities = entityType.getEntitiesList();
entities.forEach(CreateAgent::printEntity);
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("==========");
}
public static void printEntity(Entity entity) {
System.out.println(entity.getValue());
}
public static void getAgentInfo() {
System.out.println("==========");
System.out.println("Getting Agent info");

View File

@@ -0,0 +1,76 @@
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import com.google.cloud.dialogflow.v2beta1.EntityType;
import com.google.cloud.dialogflow.v2beta1.EntityTypesClient;
import com.google.cloud.dialogflow.v2beta1.ProjectAgentName;
import com.google.cloud.dialogflow.v2beta1.EntityType.Entity;
import com.google.cloud.dialogflow.v2beta1.EntityType.Kind;
import data.ReadNames;
public class EntityCreator {
private static final String CONTACT_ENTITY_NAME = "names_composite";
private static final String PROJECT_NAME = "dialogflow-fun";
private ReadNames namesReader = new ReadNames();
public void createContactEntityType() {
try {
EntityTypesClient entityTypesClient = EntityTypesClient.create();
ProjectAgentName parent = ProjectAgentName.of(PROJECT_NAME);
List<String> names = namesReader.getListOfNames();
List<Entity> contactEntities = names.stream().map(n -> Entity.newBuilder().setValue(n).build())
.collect(Collectors.toList());
EntityType contactEntityType = EntityType.newBuilder().setDisplayName(CONTACT_ENTITY_NAME)
.setKind(Kind.KIND_MAP).addAllEntities(contactEntities).build();
entityTypesClient.createEntityType(parent, contactEntityType);
} catch (IOException e) {
e.printStackTrace();
}
}
public void deleteAllEntityTypes() throws IOException {
EntityTypesClient entityTypesClient = EntityTypesClient.create();
ProjectAgentName parent = ProjectAgentName.of(PROJECT_NAME);
for (EntityType entityType : entityTypesClient.listEntityTypes(parent).iterateAll()) {
System.out.println("Deleting " + entityType.getDisplayName() + " with name " + entityType.getName());
entityTypesClient.deleteEntityType(entityType.getName());
}
}
public void getEntitiesInfo() {
System.out.println("==========");
System.out.println("Getting Entities info");
try {
EntityTypesClient entityTypesClient = EntityTypesClient.create();
ProjectAgentName parent = ProjectAgentName.of(PROJECT_NAME);
for (EntityType entityType : entityTypesClient.listEntityTypes(parent).iterateAll()) {
System.out.println(entityType.getDisplayName());
List<Entity> entities = entityType.getEntitiesList();
entities.forEach(EntityCreator::printEntity);
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("==========");
}
public static void printEntity(Entity entity) {
System.out.println(entity.getValue());
}
}

View File

@@ -0,0 +1,27 @@
package data;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ReadNames {
private static final String NAMES_FILE_LOCATION = "names2";
public List<String> getListOfNames() throws FileNotFoundException {
List<String> names = new ArrayList<>();
File namesFile = new File(getClass().getClassLoader().getResource(NAMES_FILE_LOCATION).getFile());
Scanner scanner = new Scanner(namesFile);
while (scanner.hasNextLine()) {
names.add(scanner.nextLine().trim());
}
scanner.close();
return names;
}
}

View File

@@ -247,4 +247,4 @@ Allyn Claire  
Magdalene Michna  
Leticia Jorgenson  
Jaleesa Dally  
Candace Tootle   
Candace Tootle   

View File

@@ -0,0 +1,10 @@
mum
dad
my mum
my dad
my @sys.person:person
uncle @sys.person:person
aunt @sys.person:person
grandma @sys.person:person
grandpa @sys.person:person
@sys.person:person

19
pom.xml Normal file
View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" ?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<groupId>fr.lengrand</groupId>
<artifactId>dialogflow-fun-complete</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<java.version>11</java.version>
</properties>
<modules>
<module>dialogflow-fun-java</module>
</modules>
</project>