Skip to content

Commit cc5bc8d

Browse files
[document-conversion] fix example
1 parent 41de597 commit cc5bc8d

File tree

1 file changed

+59
-56
lines changed

1 file changed

+59
-56
lines changed
Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,87 @@
11
/**
22
* Copyright 2015 IBM Corp. All Rights Reserved.
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
55
* in compliance with the License. You may obtain a copy of the License at
6-
*
6+
*
77
* http://www.apache.org/licenses/LICENSE-2.0
8-
*
8+
*
99
* Unless required by applicable law or agreed to in writing, software distributed under the License
1010
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
1111
* or implied. See the License for the specific language governing permissions and limitations under
1212
* the License.
1313
*/
1414
package com.ibm.watson.developer_cloud.document_conversion.v1;
1515

16+
import java.io.File;
17+
import java.io.FileInputStream;
18+
import java.io.FileNotFoundException;
19+
1620
import com.google.gson.JsonObject;
1721
import com.google.gson.JsonParser;
1822
import com.ibm.watson.developer_cloud.document_conversion.v1.model.Answers;
1923
import com.ibm.watson.developer_cloud.http.HttpMediaType;
2024

21-
import java.io.File;
22-
import java.io.FileInputStream;
23-
import java.io.FileNotFoundException;
24-
2525
public class DocumentConversionCustomConfigExample {
26-
public static void main(String[] args) {
27-
final String versionDate = "2015-12-14";
28-
DocumentConversion service = new DocumentConversion(versionDate);
29-
service.setUsernameAndPassword("<username>", "<password>");
30-
31-
final File html = new File("src/test/resources/document_conversion/html-with-extra-content-input.htm");
26+
public static void main(String[] args) {
27+
final String versionDate = "2015-12-14";
28+
DocumentConversion service = new DocumentConversion(versionDate);
29+
service.setUsernameAndPassword("<username>", "<password>");
3230

33-
// Run a conversion with no configuration specified. The Document Conversion service will use
34-
// its default configuration when no configuration is specified. For this example, the
35-
// Document Conversion service will section a HTML document by h1, h2, h3, h4, h5, and h6 tags.
36-
// Those sections will be returned as Answers
37-
System.out.println("Convert html document to Answer Units using default configuration");
38-
final Answers htmlToAnswersWithDefaultConfig =
39-
service.convertDocumentToAnswer(html, HttpMediaType.TEXT_HTML);
40-
System.out.println(htmlToAnswersWithDefaultConfig);
31+
final File html =
32+
new File("src/test/resources/document_conversion/html-with-extra-content-input.htm");
4133

42-
System.out.println("==================================================");
34+
// Run a conversion with no configuration specified. The Document Conversion service will use
35+
// its default configuration when no configuration is specified. For this example, the
36+
// Document Conversion service will section a HTML document by h1, h2, h3, h4, h5, and h6 tags.
37+
// Those sections will be returned as Answers
38+
System.out.println("Convert html document to Answer Units using default configuration");
39+
final Answers htmlToAnswersWithDefaultConfig =
40+
service.convertDocumentToAnswer(html, HttpMediaType.TEXT_HTML);
41+
System.out.println(htmlToAnswersWithDefaultConfig);
4342

44-
// Run a conversion with a custom configuration. The next example shows how to convert this same
45-
// document with a custom configuration. Instead of sectioning by the default settings (h1, h2,
46-
// h3, h4, h5, and h6), the following example shows how to section a HTML document by only the
47-
// h1 tag. This will result in Answers that are sectioned by h1 tags.
48-
String configAsString = "{\n" +
49-
" \"answer_units\": {\n" +
50-
" \"selector_tags\": [\"h1\"]\n" +
51-
" }\n" +
52-
"}";
53-
JsonParser jsonParser = new JsonParser();
54-
JsonObject customConfig = jsonParser.parse(configAsString).getAsJsonObject();
43+
System.out.println("==================================================");
5544

56-
System.out.println("Convert html document to Answer Units using custom configuration");
57-
final Answers htmlToAnswersWithCustomConfig =
58-
service.convertDocumentToAnswer(html, HttpMediaType.TEXT_HTML, customConfig);
59-
System.out.println(htmlToAnswersWithCustomConfig);
45+
// Run a conversion with a custom configuration. The next example shows how to convert this same
46+
// document with a custom configuration. Instead of sectioning by the default settings (h1, h2,
47+
// h3, h4, h5, and h6), the following example shows how to section a HTML document by only the
48+
// h1 tag. This will result in Answers that are sectioned by h1 tags.
49+
String configAsString =
50+
"{\n" + " \"answer_units\": {\n" + " \"selector_tags\": [\"h1\"]\n" + " }\n"
51+
+ "}";
52+
JsonParser jsonParser = new JsonParser();
53+
JsonObject customConfig = jsonParser.parse(configAsString).getAsJsonObject();
6054

61-
System.out.println("==================================================");
55+
System.out.println("Convert html document to Answer Units using custom configuration");
56+
final Answers htmlToAnswersWithCustomConfig =
57+
service.convertDocumentToAnswer(html, HttpMediaType.TEXT_HTML, customConfig);
58+
System.out.println(htmlToAnswersWithCustomConfig);
6259

63-
// Run a conversion with a custom configuration that is loaded from a file. This example is similar
64-
// to the previous one above. The custom configuration from the file will section a HTML document
65-
// by only the h2 tag. This will result in Answers that are sectioned by h2 tags.
66-
System.out.println("Convert html document to Answer Units using custom configuration loaded from a file");
67-
String customConfigFilePath = "src/test/resources/document_conversion/answer_unit_config_selector_h2.json";
68-
JsonObject customConfigFromFile = null;
69-
try {
70-
customConfigFromFile = service.loadCustomConfig(new FileInputStream(customConfigFilePath));
71-
} catch(FileNotFoundException e ) {
72-
e.printStackTrace();
73-
}
60+
System.out.println("==================================================");
7461

75-
if(customConfigFilePath == null) {
76-
System.err.println("ERROR - Unable to load custom config from file " + customConfigFilePath);
77-
return;
78-
}
62+
// Run a conversion with a custom configuration that is loaded from a file. This example is
63+
// similar
64+
// to the previous one above. The custom configuration from the file will section a HTML
65+
// document
66+
// by only the h2 tag. This will result in Answers that are sectioned by h2 tags.
67+
System.out
68+
.println("Convert html document to Answer Units using custom configuration loaded from a file");
69+
String customConfigFilePath =
70+
"src/test/resources/document_conversion/answer_unit_config_selector_h2.json";
71+
JsonObject customConfigFromFile = null;
72+
try {
73+
customConfigFromFile = service.loadCustomConfig(new FileInputStream(customConfigFilePath));
74+
} catch (FileNotFoundException e) {
75+
e.printStackTrace();
76+
}
7977

80-
final Answers htmlToAnswersWithCustomConfigFromFile =
81-
service.convertDocumentToAnswer(html, HttpMediaType.TEXT_HTML, customConfigFromFile);
82-
System.out.println(htmlToAnswersWithCustomConfigFromFile);
78+
if (customConfigFromFile == null) {
79+
System.err.println("ERROR - Unable to load custom config from file " + customConfigFilePath);
80+
return;
8381
}
82+
83+
final Answers htmlToAnswersWithCustomConfigFromFile =
84+
service.convertDocumentToAnswer(html, HttpMediaType.TEXT_HTML, customConfigFromFile);
85+
System.out.println(htmlToAnswersWithCustomConfigFromFile);
86+
}
8487
}

0 commit comments

Comments
 (0)