|
| 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.document_conversion.v1; |
| 15 | + |
| 16 | +import com.google.gson.JsonObject; |
| 17 | +import com.google.gson.JsonParser; |
| 18 | +import com.ibm.watson.developer_cloud.document_conversion.v1.model.Answers; |
| 19 | +import com.ibm.watson.developer_cloud.http.HttpMediaType; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | + |
| 23 | +public class DocumentConversionCustomConfigExample { |
| 24 | + public static void main(String[] args) { |
| 25 | + final String versionDate = "2015-12-14"; |
| 26 | + DocumentConversion service = new DocumentConversion(versionDate); |
| 27 | + service.setUsernameAndPassword("<username>", "<password>"); |
| 28 | + |
| 29 | + final File html = new File("src/test/resources/document_conversion/html-with-extra-content-input.htm"); |
| 30 | + |
| 31 | + // Run a conversion with no configuration specified. The Document Conversion service will use |
| 32 | + // its default configuration when no configuration is specified. For this example, the |
| 33 | + // Document Conversion service will section a HTML document by h1, h2, h3, h4, h5, and h6 tags. |
| 34 | + // Those sections will be returned as Answers |
| 35 | + System.out.println("Convert html document to Answer Units using default configuration"); |
| 36 | + final Answers htmlToAnswersWithDefaultConfig = |
| 37 | + service.convertDocumentToAnswer(html, HttpMediaType.TEXT_HTML); |
| 38 | + System.out.println(htmlToAnswersWithDefaultConfig); |
| 39 | + |
| 40 | + System.out.println("=================================================="); |
| 41 | + |
| 42 | + // Run a conversion with a custom configuration. The next example shows how to convert this same |
| 43 | + // document with a custom configuration. Instead of sectioning by the default settings (h1, h2, |
| 44 | + // h3, h4, h5, and h6), the following example shows how to section a HTML document by only the |
| 45 | + // h1 tag. This will result in Answers that are sectioned by h1 tags. |
| 46 | + String configAsString = "{\n" + |
| 47 | + " \"answer_units\": {\n" + |
| 48 | + " \"selector_tags\": [\"h1\"]\n" + |
| 49 | + " }\n" + |
| 50 | + "}"; |
| 51 | + JsonParser jsonParser = new JsonParser(); |
| 52 | + JsonObject customConfig = jsonParser.parse(configAsString).getAsJsonObject(); |
| 53 | + |
| 54 | + System.out.println("Convert html document to Answer Units using custom configuration"); |
| 55 | + final Answers htmlToAnswersWithCustomConfig = |
| 56 | + service.convertDocumentToAnswer(html, HttpMediaType.TEXT_HTML, customConfig); |
| 57 | + System.out.println(htmlToAnswersWithCustomConfig); |
| 58 | + } |
| 59 | +} |
0 commit comments