Skip to content

Commit d9e768e

Browse files
committed
Alchemy JSON file fix
The file is now included in the hat file..
1 parent 6ccdb15 commit d9e768e

File tree

4 files changed

+184
-22
lines changed

4 files changed

+184
-22
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@
168168
<profile>
169169
<id>release</id>
170170
<build>
171+
<resources>
172+
<resource>
173+
<directory>src/main/resources</directory>
174+
<includes>
175+
<include>**/*.json</include>
176+
</includes>
177+
</resource>
178+
</resources>
171179
<plugins>
172180
<plugin>
173181
<groupId>org.apache.maven.plugins</groupId>

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

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,17 @@
1616

1717
package com.ibm.watson.developer_cloud.alchemy.v1;
1818

19-
import java.io.IOException;
20-
import java.util.Map;
21-
22-
import org.apache.http.HttpResponse;
23-
import org.apache.http.client.methods.HttpRequestBase;
24-
25-
import com.ibm.watson.developer_cloud.alchemy.v1.model.CombinedResults;
26-
import com.ibm.watson.developer_cloud.alchemy.v1.model.DocumentAuthors;
27-
import com.ibm.watson.developer_cloud.alchemy.v1.model.DocumentSentiment;
28-
import com.ibm.watson.developer_cloud.alchemy.v1.model.DocumentText;
29-
import com.ibm.watson.developer_cloud.alchemy.v1.model.DocumentTitle;
30-
import com.ibm.watson.developer_cloud.alchemy.v1.model.Entities;
31-
import com.ibm.watson.developer_cloud.alchemy.v1.model.Feeds;
32-
import com.ibm.watson.developer_cloud.alchemy.v1.model.Keywords;
33-
import com.ibm.watson.developer_cloud.alchemy.v1.model.Language;
34-
import com.ibm.watson.developer_cloud.alchemy.v1.model.Microformats;
35-
import com.ibm.watson.developer_cloud.alchemy.v1.model.SAORelations;
36-
import com.ibm.watson.developer_cloud.alchemy.v1.model.Taxonomies;
19+
import com.ibm.watson.developer_cloud.alchemy.v1.model.*;
20+
import com.ibm.watson.developer_cloud.alchemy.v1.util.AlchemyEndPoints;
21+
import com.ibm.watson.developer_cloud.alchemy.v1.util.AlchemyEndPoints.AlchemyAPI;
3722
import com.ibm.watson.developer_cloud.service.AlchemyService;
3823
import com.ibm.watson.developer_cloud.service.Request;
39-
import com.ibm.watson.developer_cloud.util.AlchemyEndPoints;
40-
import com.ibm.watson.developer_cloud.util.AlchemyEndPoints.AlchemyAPI;
4124
import com.ibm.watson.developer_cloud.util.ResponseUtil;
25+
import org.apache.http.HttpResponse;
26+
import org.apache.http.client.methods.HttpRequestBase;
27+
28+
import java.io.IOException;
29+
import java.util.Map;
4230

4331
/**
4432
* The Alchemy Language service uses offers 12 text analysis services, each of which uses

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import com.ibm.watson.developer_cloud.alchemy.v1.model.ImageLink;
2929
import com.ibm.watson.developer_cloud.service.AlchemyService;
3030
import com.ibm.watson.developer_cloud.service.Request;
31-
import com.ibm.watson.developer_cloud.util.AlchemyEndPoints;
32-
import com.ibm.watson.developer_cloud.util.AlchemyEndPoints.AlchemyAPI;
31+
import com.ibm.watson.developer_cloud.alchemy.v1.util.AlchemyEndPoints;
32+
import com.ibm.watson.developer_cloud.alchemy.v1.util.AlchemyEndPoints.AlchemyAPI;
3333
import com.ibm.watson.developer_cloud.util.MediaType;
3434
import com.ibm.watson.developer_cloud.util.ResponseUtil;
3535

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/**
2+
* Copyright 2015 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.ibm.watson.developer_cloud.alchemy.v1.util;
18+
19+
import com.google.gson.JsonElement;
20+
import com.google.gson.JsonObject;
21+
import com.google.gson.JsonParseException;
22+
import com.google.gson.JsonParser;
23+
24+
import java.io.InputStream;
25+
import java.io.InputStreamReader;
26+
import java.io.Reader;
27+
import java.util.HashMap;
28+
import java.util.Map;
29+
import java.util.logging.Level;
30+
import java.util.logging.Logger;
31+
32+
/**
33+
* The Class AlchemyEndPoints.
34+
*
35+
* @author Nizar Alseddeg (nmalsedd@us.ibm.com)
36+
*/
37+
public class AlchemyEndPoints {
38+
39+
/** The Constant log. */
40+
private static final Logger log = Logger.getLogger(AlchemyEndPoints.class.getName());
41+
42+
/** The file where alchemy endpoints are described. */
43+
private static final String filePath = "/alchemy_endpoints.json";
44+
45+
46+
/** The alchemy operations. */
47+
private static Map<String, Map<String, String>> operations;
48+
49+
static {
50+
loadEndPointsFromJsonFile();
51+
}
52+
53+
/**
54+
* The AlchemyOperations.
55+
*/
56+
public enum AlchemyAPI {
57+
58+
/** The entities. */
59+
entities,
60+
61+
/** The keywords. */
62+
keywords,
63+
64+
/** The concepts. */
65+
concepts,
66+
67+
/** The sentiment. */
68+
sentiment,
69+
70+
/** The sentiment_targeted. */
71+
sentiment_targeted,
72+
73+
/** The relations. */
74+
relations,
75+
76+
/** The language. */
77+
language,
78+
79+
/** The text. */
80+
text,
81+
82+
/** The text_raw. */
83+
text_raw,
84+
85+
/** The authors. */
86+
authors,
87+
88+
/** The feeds. */
89+
feeds,
90+
91+
/** The microformats. */
92+
microformats,
93+
94+
/** The title. */
95+
title,
96+
97+
/** The taxonomy. */
98+
taxonomy,
99+
100+
/** The combined. */
101+
combined,
102+
103+
/** The image_link. */
104+
image_link,
105+
106+
/** The image_keywords. */
107+
image_keywords,
108+
109+
/** The image_recognition. */
110+
image_recognition
111+
}
112+
113+
/**
114+
* Load the endpoints from json file.
115+
*/
116+
private static void loadEndPointsFromJsonFile() {
117+
log.log(Level.FINE, "Parsing End Points JSON file ");
118+
operations = new HashMap<String, Map<String, String>>();
119+
JsonParser parser = new JsonParser();
120+
try {
121+
Reader fileReader = null;
122+
123+
InputStream is = AlchemyEndPoints.class.getResourceAsStream(filePath);
124+
if (null != is) {
125+
fileReader = new InputStreamReader(is);
126+
}
127+
Object obj = parser.parse(fileReader);
128+
JsonObject jsonObject = (JsonObject) obj;
129+
for (AlchemyAPI object : AlchemyAPI.values()) {
130+
if (jsonObject.get(object.name()) == null)
131+
continue;;
132+
JsonElement elt = jsonObject.get(object.name()).getAsJsonObject();
133+
if (elt.isJsonObject()) {
134+
Map<String, String> records = new HashMap<String, String>();
135+
for (Map.Entry<String, JsonElement> e : elt.getAsJsonObject().entrySet()) {
136+
records.put(e.getKey(), e.getValue().getAsString());
137+
}
138+
operations.put(object.name(), records);
139+
}
140+
}
141+
} catch (JsonParseException e) {
142+
log.log(Level.SEVERE, "Could not parse json file: " + filePath, e);
143+
} catch (NullPointerException e){
144+
log.log(Level.SEVERE, "Not able to locate the end points json file: " + filePath, e);
145+
}
146+
}
147+
148+
/**
149+
* Gets the path based on the operation and input type.
150+
*
151+
* @param operation the operation
152+
* @param inputType the input type
153+
* @return the string that represent the path based on the operation
154+
* and input type
155+
*/
156+
public static String getPath(AlchemyAPI operation, String inputType) {
157+
if ((operations.get(operation.name()) != null) && operations.get(operation.name()).get(inputType) != null)
158+
return operations.get(operation.name()).get(inputType);
159+
else {
160+
String error = "Operation: "+ operation + ", inputType: "+inputType+" not found";
161+
log.log(Level.SEVERE,error);
162+
throw new IllegalArgumentException(error);
163+
}
164+
}
165+
166+
}

0 commit comments

Comments
 (0)