Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.ibm.watson.developer_cloud.alchemy.v1.model;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* Created by nastacio on 7/2/2016
*/
public class TypedEntitiesAdapter extends TypeAdapter<List<TypedEntity>> {

@Override
public void write(JsonWriter writer, List<TypedEntity> value) throws IOException {
// No coding the serialization as there is no code relying on this
// type to serialize payloads
}

@Override
public List<TypedEntity> read(JsonReader reader) throws IOException {
List<TypedEntity> es = new ArrayList<TypedEntity>();

reader.beginArray(); // arguments
while (reader.hasNext()) {
reader.beginObject(); // argument
while (reader.hasNext()) {
String name = reader.nextName();
if ("entities".equals(name)) {
reader.beginArray();
while (reader.hasNext()) {
TypedEntity e = new TypedEntity();
reader.beginObject();
while (reader.hasNext()) {
String name1 = reader.nextName();
if ("text".equals(name1)) {
e.setText(reader.nextString());
} else if ("type".equals(name1)) {
e.setType(reader.nextString());
} else if ("id".equals(name1)) {
e.setId(reader.nextString());
} else {
reader.skipValue();
}
}
reader.endObject();
es.add(e);
}
reader.endArray();
} else {
reader.skipValue();
}
}
reader.endObject();
}
reader.endArray();

return es;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import java.util.List;

import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.ibm.watson.developer_cloud.alchemy.v1.AlchemyLanguage;

/**
Expand All @@ -27,7 +29,10 @@ public class TypedRelation {
private String text;
private String type;
private Double score;
private List<Entity> entities;

@JsonAdapter(TypedEntitiesAdapter.class)
@SerializedName("arguments")
private List<TypedEntity> entities;

/**
* Gets the text.
Expand Down Expand Up @@ -84,20 +89,16 @@ public void setScore(Double score) {
}

/**
* Gets the entities.
*
* @return The entities
* @return the entities
*/
public List<Entity> getEntities() {
public List<TypedEntity> getEntities() {
return entities;
}

/**
* Sets the entities.
*
* @param entities The entities
* @param entities the entities to set
*/
public void setEntities(List<Entity> entities) {
public void setEntities(List<TypedEntity> entities) {
this.entities = entities;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Assert;
Expand All @@ -40,6 +41,8 @@
import com.ibm.watson.developer_cloud.alchemy.v1.model.Microformats;
import com.ibm.watson.developer_cloud.alchemy.v1.model.SAORelations;
import com.ibm.watson.developer_cloud.alchemy.v1.model.Taxonomies;
import com.ibm.watson.developer_cloud.alchemy.v1.model.TypedEntity;
import com.ibm.watson.developer_cloud.alchemy.v1.model.TypedRelation;
import com.ibm.watson.developer_cloud.alchemy.v1.model.TypedRelations;

/**
Expand Down Expand Up @@ -484,12 +487,24 @@ public void testGetTypedRelationsHTML() {
*/
@Test
public void testGetTypedRelationsText() {
final Map<String, Object> params = new HashMap<String, Object>();
params.put(AlchemyLanguage.TEXT, "Jake is one of the developers in the team.");
params.put(AlchemyLanguage.MODEL_ID, "en-us-tir");
final TypedRelations typedRelations = service.getTypedRelations(params).execute();
Assert.assertNotNull(typedRelations);
Assert.assertNotNull(typedRelations.getTypedRelations());
final Map<String, Object> params = new HashMap<String, Object>();
params.put(AlchemyLanguage.TEXT, "Leiming Qian lives in New York.");
params.put(AlchemyLanguage.MODEL_ID, "ie-en-news");
final TypedRelations typedRelations = service.getTypedRelations(params).execute();
Assert.assertNotNull(typedRelations);
List<TypedRelation> trs = typedRelations.getTypedRelations();
Assert.assertNotNull(trs);
Assert.assertFalse(trs.isEmpty());
for (TypedRelation tr : trs) {
Assert.assertNotNull(tr.getType());
Assert.assertNotNull(tr.getEntities());
Assert.assertFalse(tr.getEntities().isEmpty());
for (TypedEntity e : tr.getEntities()) {
Assert.assertNotNull(e.getId());
Assert.assertNotNull(e.getText());
Assert.assertNotNull(e.getType());
}
}
}

/**
Expand Down