|
1 | 1 | /** |
2 | 2 | * Copyright 2015 IBM Corp. All Rights Reserved. |
3 | | - * |
| 3 | + * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | 5 | * in compliance with the License. You may obtain a copy of the License at |
6 | | - * |
| 6 | + * |
7 | 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
8 | | - * |
| 8 | + * |
9 | 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License |
10 | 10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | 11 | * or implied. See the License for the specific language governing permissions and limitations under |
12 | 12 | * the License. |
13 | 13 | */ |
14 | 14 | package com.ibm.watson.developer_cloud.document_conversion.v1; |
15 | 15 |
|
| 16 | +import java.io.File; |
| 17 | +import java.io.FileInputStream; |
| 18 | +import java.io.FileNotFoundException; |
| 19 | + |
16 | 20 | import com.google.gson.JsonObject; |
17 | 21 | import com.google.gson.JsonParser; |
18 | 22 | import com.ibm.watson.developer_cloud.document_conversion.v1.model.Answers; |
19 | 23 | import com.ibm.watson.developer_cloud.http.HttpMediaType; |
20 | 24 |
|
21 | | -import java.io.File; |
22 | | -import java.io.FileInputStream; |
23 | | -import java.io.FileNotFoundException; |
24 | | - |
25 | 25 | 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>"); |
32 | 30 |
|
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"); |
41 | 33 |
|
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); |
43 | 42 |
|
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("=================================================="); |
55 | 44 |
|
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(); |
60 | 54 |
|
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); |
62 | 59 |
|
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("=================================================="); |
74 | 61 |
|
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 | + } |
79 | 77 |
|
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; |
83 | 81 | } |
| 82 | + |
| 83 | + final Answers htmlToAnswersWithCustomConfigFromFile = |
| 84 | + service.convertDocumentToAnswer(html, HttpMediaType.TEXT_HTML, customConfigFromFile); |
| 85 | + System.out.println(htmlToAnswersWithCustomConfigFromFile); |
| 86 | + } |
84 | 87 | } |
0 commit comments