Skip to content

Commit 69b3bb3

Browse files
[alchemy] Number format exception when there is no tag returned by the
API #216
1 parent 7d36a0e commit 69b3bb3

File tree

3 files changed

+113
-10
lines changed

3 files changed

+113
-10
lines changed

src/main/java/com/ibm/watson/developer_cloud/alchemy/v1/AlchemyVision.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
import java.io.File;
1717
import java.net.URL;
1818
import java.util.HashMap;
19+
import java.util.ListIterator;
1920
import java.util.Map;
2021

2122
import com.ibm.watson.developer_cloud.alchemy.v1.model.AlchemyGenericModel;
2223
import com.ibm.watson.developer_cloud.alchemy.v1.model.ImageFaces;
24+
import com.ibm.watson.developer_cloud.alchemy.v1.model.ImageKeyword;
2325
import com.ibm.watson.developer_cloud.alchemy.v1.model.ImageKeywords;
2426
import com.ibm.watson.developer_cloud.alchemy.v1.model.ImageLink;
2527
import com.ibm.watson.developer_cloud.alchemy.v1.model.ImageSceneText;
@@ -71,6 +73,9 @@ public class AlchemyVision extends AlchemyService {
7173
/** The Constant URL. (value is "url") */
7274
public static final String URL = "url";
7375

76+
/** The Constant NO_TAGS. (value is "NO_TAGS") */
77+
private static final String NO_TAGS = "NO_TAGS";
78+
7479
/**
7580
* Executes the request and return the POJO that represent the response.
7681
*
@@ -93,11 +98,11 @@ private <T extends AlchemyGenericModel> T executeRequest(Map<String, Object> par
9398
params.put(IMAGE_POST_MODE, RAW);
9499
final File image = (File) params.get(IMAGE);
95100
if (!image.exists()) {
96-
throw new IllegalArgumentException("The file: " + image.getAbsolutePath()
97-
+ " does not exist.");
101+
throw new IllegalArgumentException(
102+
"The file: " + image.getAbsolutePath() + " does not exist.");
98103
} else {
99-
requestBuilder.withBody(RequestBody.create(HttpMediaType.BINARY_FILE,
100-
(File) params.get(IMAGE)));
104+
requestBuilder
105+
.withBody(RequestBody.create(HttpMediaType.BINARY_FILE, (File) params.get(IMAGE)));
101106
params.remove(IMAGE);
102107
}
103108
}
@@ -133,22 +138,23 @@ public ImageSceneText getImageSceneText(File image) {
133138

134139
return executeRequest(params, AlchemyAPI.image_scene_text, ImageSceneText.class);
135140
}
136-
141+
137142
/**
138-
* Identifies text in an image specified by URL or in the primary image in a web page specified by URL
143+
* Identifies text in an image specified by URL or in the primary image in a web page specified by
144+
* URL
139145
*
140146
* @param url the image URL
141147
* @return {@link ImageSceneText}
142148
*/
143149
public ImageSceneText getImageSceneText(URL url) {
144150
Validate.notNull(url, "url cannot be null");
145-
151+
146152
final Map<String, Object> params = new HashMap<String, Object>();
147153
params.put(URL, url);
148-
154+
149155
return executeRequest(params, AlchemyAPI.image_scene_text, ImageSceneText.class);
150156
}
151-
157+
152158
/**
153159
* Extracts keywords from an image
154160
*
@@ -170,7 +176,16 @@ public ImageKeywords getImageKeywords(File image, Boolean forceShowAll, Boolean
170176
if (knowledgeGraph != null)
171177
params.put(KNOWLEDGE_GRAPH, knowledgeGraph ? 1 : 0);
172178

173-
return executeRequest(params, AlchemyAPI.image_keywords, ImageKeywords.class);
179+
ImageKeywords imageKeywords = executeRequest(params, AlchemyAPI.image_keywords, ImageKeywords.class);
180+
181+
// Remove the NO_TAGS keywords
182+
ListIterator<ImageKeyword> iter = imageKeywords.getImageKeywords().listIterator();
183+
while (iter.hasNext()){
184+
if (iter.next().getText().equals(NO_TAGS)){
185+
iter.remove();
186+
}
187+
}
188+
return imageKeywords;
174189
}
175190

176191

src/main/java/com/ibm/watson/developer_cloud/alchemy/v1/model/ImageKeyword.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
*/
1414
package com.ibm.watson.developer_cloud.alchemy.v1.model;
1515

16+
import com.google.gson.annotations.JsonAdapter;
1617
import com.ibm.watson.developer_cloud.alchemy.v1.AlchemyVision;
18+
import com.ibm.watson.developer_cloud.alchemy.v1.util.ImageKeywordTypeAdapter;
1719
import com.ibm.watson.developer_cloud.service.model.GenericModel;
1820

1921
/**
2022
* ImageKeyword by the {@link AlchemyVision} service.
2123
*
2224
*/
25+
@JsonAdapter(ImageKeywordTypeAdapter.class)
2326
public class ImageKeyword extends GenericModel {
2427

2528

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Copyright 2015 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package com.ibm.watson.developer_cloud.alchemy.v1.util;
15+
16+
import java.io.IOException;
17+
18+
import com.google.gson.TypeAdapter;
19+
import com.google.gson.stream.JsonReader;
20+
import com.google.gson.stream.JsonToken;
21+
import com.google.gson.stream.JsonWriter;
22+
import com.ibm.watson.developer_cloud.alchemy.v1.model.ImageKeyword;
23+
24+
/**
25+
* Type Adapter for the {@link ImageKeyword} class.
26+
*/
27+
public class ImageKeywordTypeAdapter extends TypeAdapter<ImageKeyword> {
28+
29+
/*
30+
* (non-Javadoc)
31+
*
32+
* @see com.google.gson.TypeAdapter#read(com.google.gson.stream.JsonReader)
33+
*/
34+
@Override
35+
public ImageKeyword read(JsonReader reader) throws IOException {
36+
if (reader.peek() == JsonToken.NULL) {
37+
reader.nextNull();
38+
return null;
39+
}
40+
41+
final ImageKeyword ImageKeyword = new ImageKeyword();
42+
reader.beginObject();
43+
while (reader.hasNext()) {
44+
final String name = reader.nextName();
45+
46+
if (name.equals("text")) {
47+
final String text = reader.nextString();
48+
ImageKeyword.setText(text);
49+
} else if (name.equals("score")) {
50+
final String score = reader.nextString();
51+
if (score != null && !score.isEmpty())
52+
ImageKeyword.setScore(Double.valueOf(score));
53+
} else {
54+
reader.skipValue();
55+
}
56+
}
57+
reader.endObject();
58+
return ImageKeyword;
59+
}
60+
61+
/*
62+
* (non-Javadoc)
63+
*
64+
* @see com.google.gson.TypeAdapter#write(com.google.gson.stream.JsonWriter, java.lang.Object)
65+
*/
66+
@Override
67+
public void write(JsonWriter writer, ImageKeyword value) throws IOException {
68+
if (value == null) {
69+
writer.nullValue();
70+
return;
71+
}
72+
73+
writer.beginObject();
74+
75+
if (value.getScore() != null)
76+
writer.name("score").value(value.getScore());
77+
if (value.getText() != null)
78+
writer.name("text").value(value.getText());
79+
80+
writer.endObject();
81+
writer.flush();
82+
}
83+
84+
85+
}

0 commit comments

Comments
 (0)