Skip to content

Commit fa97def

Browse files
committed
feat(DiscoveryV2): Add support for AnalyzeDocuments
1 parent bd6d619 commit fa97def

14 files changed

+285
-12
lines changed

Scripts/Services/Discovery/V2/DiscoveryService.cs

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* (C) Copyright IBM Corp. 2019, 2020.
2+
* (C) Copyright IBM Corp. 2020.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -1662,6 +1662,104 @@ private void OnUpdateTrainingQueryResponse(RESTConnector.Request req, RESTConnec
16621662
((RequestObject<TrainingQuery>)req).Callback(response, resp.Error);
16631663
}
16641664
/// <summary>
1665+
/// Analyze a Document.
1666+
///
1667+
/// Process a document using the specified collection's settings and return it for realtime use.
1668+
///
1669+
/// **Note:** Documents processed using this method are not added to the specified collection.
1670+
///
1671+
/// **Note:** This method is only supported on IBM Cloud Pak for Data instances of Discovery.
1672+
/// </summary>
1673+
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
1674+
/// <param name="projectId">The ID of the project. This information can be found from the deploy page of the
1675+
/// Discovery administrative tooling.</param>
1676+
/// <param name="collectionId">The ID of the collection.</param>
1677+
/// <param name="file">The content of the document to ingest. The maximum supported file size when adding a file
1678+
/// to a collection is 50 megabytes, the maximum supported file size when testing a configuration is 1 megabyte.
1679+
/// Files larger than the supported size are rejected. (optional)</param>
1680+
/// <param name="filename">The filename for file. (optional)</param>
1681+
/// <param name="fileContentType">The content type of file. (optional)</param>
1682+
/// <param name="metadata">The maximum supported metadata file size is 1 MB. Metadata parts larger than 1 MB are
1683+
/// rejected.
1684+
///
1685+
///
1686+
/// Example: ``` {
1687+
/// "Creator": "Johnny Appleseed",
1688+
/// "Subject": "Apples"
1689+
/// } ```. (optional)</param>
1690+
/// <returns><see cref="AnalyzedDocument" />AnalyzedDocument</returns>
1691+
public bool AnalyzeDocument(Callback<AnalyzedDocument> callback, string projectId, string collectionId, System.IO.MemoryStream file = null, string filename = null, string fileContentType = null, string metadata = null)
1692+
{
1693+
if (callback == null)
1694+
throw new ArgumentNullException("`callback` is required for `AnalyzeDocument`");
1695+
if (string.IsNullOrEmpty(projectId))
1696+
throw new ArgumentNullException("`projectId` is required for `AnalyzeDocument`");
1697+
if (string.IsNullOrEmpty(collectionId))
1698+
throw new ArgumentNullException("`collectionId` is required for `AnalyzeDocument`");
1699+
1700+
RequestObject<AnalyzedDocument> req = new RequestObject<AnalyzedDocument>
1701+
{
1702+
Callback = callback,
1703+
HttpMethod = UnityWebRequest.kHttpVerbPOST,
1704+
DisableSslVerification = DisableSslVerification
1705+
};
1706+
1707+
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
1708+
{
1709+
req.Headers.Add(kvp.Key, kvp.Value);
1710+
}
1711+
1712+
ClearCustomRequestHeaders();
1713+
1714+
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("discovery", "V2", "AnalyzeDocument"))
1715+
{
1716+
req.Headers.Add(kvp.Key, kvp.Value);
1717+
}
1718+
1719+
req.Parameters["version"] = VersionDate;
1720+
req.Forms = new Dictionary<string, RESTConnector.Form>();
1721+
if (file != null)
1722+
{
1723+
req.Forms["file"] = new RESTConnector.Form(file, filename, fileContentType);
1724+
}
1725+
if (!string.IsNullOrEmpty(metadata))
1726+
{
1727+
req.Forms["metadata"] = new RESTConnector.Form(metadata);
1728+
}
1729+
1730+
req.OnResponse = OnAnalyzeDocumentResponse;
1731+
1732+
Connector.URL = GetServiceUrl() + string.Format("/v2/projects/{0}/collections/{1}/analyze", projectId, collectionId);
1733+
Authenticator.Authenticate(Connector);
1734+
1735+
return Connector.Send(req);
1736+
}
1737+
1738+
private void OnAnalyzeDocumentResponse(RESTConnector.Request req, RESTConnector.Response resp)
1739+
{
1740+
DetailedResponse<AnalyzedDocument> response = new DetailedResponse<AnalyzedDocument>();
1741+
foreach (KeyValuePair<string, string> kvp in resp.Headers)
1742+
{
1743+
response.Headers.Add(kvp.Key, kvp.Value);
1744+
}
1745+
response.StatusCode = resp.HttpResponseCode;
1746+
1747+
try
1748+
{
1749+
string json = Encoding.UTF8.GetString(resp.Data);
1750+
response.Result = JsonConvert.DeserializeObject<AnalyzedDocument>(json);
1751+
response.Response = json;
1752+
}
1753+
catch (Exception e)
1754+
{
1755+
Log.Error("DiscoveryService.OnAnalyzeDocumentResponse()", "Exception: {0}", e.ToString());
1756+
resp.Success = false;
1757+
}
1758+
1759+
if (((RequestObject<AnalyzedDocument>)req).Callback != null)
1760+
((RequestObject<AnalyzedDocument>)req).Callback(response, resp.Error);
1761+
}
1762+
/// <summary>
16651763
/// List Enrichments.
16661764
///
16671765
/// List the enrichments available to this project.
@@ -2490,4 +2588,4 @@ private void OnDeleteUserDataResponse(RESTConnector.Request req, RESTConnector.R
24902588
((RequestObject<object>)req).Callback(response, resp.Error);
24912589
}
24922590
}
2493-
}
2591+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* (C) Copyright IBM Corp. 2020.
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+
18+
using System.Collections.Generic;
19+
using Newtonsoft.Json;
20+
21+
namespace IBM.Watson.Discovery.V2.Model
22+
{
23+
/// <summary>
24+
/// An object containing the converted document and any identified enrichments.
25+
/// </summary>
26+
public class AnalyzedDocument
27+
{
28+
/// <summary>
29+
/// Array of document results that match the query.
30+
/// </summary>
31+
[JsonProperty("notices", NullValueHandling = NullValueHandling.Ignore)]
32+
public List<Notice> Notices { get; set; }
33+
/// <summary>
34+
/// Result of the document analysis.
35+
/// </summary>
36+
[JsonProperty("result", NullValueHandling = NullValueHandling.Ignore)]
37+
public AnalyzedResult Result { get; set; }
38+
}
39+
}

Scripts/Services/Discovery/V2/Model/AnalyzedDocument.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* (C) Copyright IBM Corp. 2020.
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+
18+
using System.Collections.Generic;
19+
using IBM.Cloud.SDK.Model;
20+
using Newtonsoft.Json;
21+
22+
namespace IBM.Watson.Discovery.V2.Model
23+
{
24+
/// <summary>
25+
/// Result of the document analysis.
26+
/// </summary>
27+
public class AnalyzedResult: DynamicModel<object>
28+
{
29+
/// <summary>
30+
/// Metadata of the document.
31+
/// </summary>
32+
[JsonProperty("metadata", NullValueHandling = NullValueHandling.Ignore)]
33+
public Dictionary<string, object> Metadata { get; set; }
34+
}
35+
}

Scripts/Services/Discovery/V2/Model/AnalyzedResult.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/Services/Discovery/V2/Model/Completions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2018, 2019 IBM Corp. All Rights Reserved.
2+
* (C) Copyright IBM Corp. 2020.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,7 @@ namespace IBM.Watson.Discovery.V2.Model
2626
public class Completions
2727
{
2828
/// <summary>
29-
/// Array of autcomplete suggestion based on the provided prefix.
29+
/// Array of autocomplete suggestion based on the provided prefix.
3030
/// </summary>
3131
[JsonProperty("completions", NullValueHandling = NullValueHandling.Ignore)]
3232
public List<string> _Completions { get; set; }

Scripts/Services/Discovery/V2/Model/DefaultQueryParams.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ public class DefaultQueryParams
5151
[JsonProperty("suggested_refinements", NullValueHandling = NullValueHandling.Ignore)]
5252
public DefaultQueryParamsSuggestedRefinements SuggestedRefinements { get; set; }
5353
/// <summary>
54-
/// When `true`, a spelling suggestions for the query are retuned by default.
54+
/// When `true`, a spelling suggestions for the query are returned by default.
5555
/// </summary>
5656
[JsonProperty("spelling_suggestions", NullValueHandling = NullValueHandling.Ignore)]
5757
public bool? SpellingSuggestions { get; set; }
5858
/// <summary>
59-
/// When `true`, a highlights for the query are retuned by default.
59+
/// When `true`, a highlights for the query are returned by default.
6060
/// </summary>
6161
[JsonProperty("highlight", NullValueHandling = NullValueHandling.Ignore)]
6262
public bool? Highlight { get; set; }

Scripts/Services/Discovery/V2/Model/DefaultQueryParamsPassages.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class DefaultQueryParamsPassages
3636
[JsonProperty("count", NullValueHandling = NullValueHandling.Ignore)]
3737
public long? Count { get; set; }
3838
/// <summary>
39-
/// An array of field names to perfom the passage search on.
39+
/// An array of field names to perform the passage search on.
4040
/// </summary>
4141
[JsonProperty("fields", NullValueHandling = NullValueHandling.Ignore)]
4242
public List<string> Fields { get; set; }

Scripts/Services/Discovery/V2/Model/DefaultQueryParamsSuggestedRefinements.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace IBM.Watson.Discovery.V2.Model
2525
public class DefaultQueryParamsSuggestedRefinements
2626
{
2727
/// <summary>
28-
/// When `true`, a suggested refinements for the query are retuned by default.
28+
/// When `true`, a suggested refinements for the query are returned by default.
2929
/// </summary>
3030
[JsonProperty("enabled", NullValueHandling = NullValueHandling.Ignore)]
3131
public bool? Enabled { get; set; }

Scripts/Services/Discovery/V2/Model/DefaultQueryParamsTableResults.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace IBM.Watson.Discovery.V2.Model
2525
public class DefaultQueryParamsTableResults
2626
{
2727
/// <summary>
28-
/// When `true`, a table results for the query are retuned by default.
28+
/// When `true`, a table results for the query are returned by default.
2929
/// </summary>
3030
[JsonProperty("enabled", NullValueHandling = NullValueHandling.Ignore)]
3131
public bool? Enabled { get; set; }

Scripts/Services/Discovery/V2/Model/ProjectListDetailsRelevancyTrainingStatus.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class ProjectListDetailsRelevancyTrainingStatus
3535
[JsonProperty("total_examples", NullValueHandling = NullValueHandling.Ignore)]
3636
public long? TotalExamples { get; set; }
3737
/// <summary>
38-
/// When `true`, sufficent label diversity is present to allow training for this project.
38+
/// When `true`, sufficient label diversity is present to allow training for this project.
3939
/// </summary>
4040
[JsonProperty("sufficient_label_diversity", NullValueHandling = NullValueHandling.Ignore)]
4141
public bool? SufficientLabelDiversity { get; set; }
@@ -50,7 +50,7 @@ public class ProjectListDetailsRelevancyTrainingStatus
5050
[JsonProperty("minimum_examples_added", NullValueHandling = NullValueHandling.Ignore)]
5151
public bool? MinimumExamplesAdded { get; set; }
5252
/// <summary>
53-
/// The time that the most recent successful training occured.
53+
/// The time that the most recent successful training occurred.
5454
/// </summary>
5555
[JsonProperty("successfully_trained", NullValueHandling = NullValueHandling.Ignore)]
5656
public string SuccessfullyTrained { get; set; }

Scripts/Services/Discovery/V2/Model/QueryResponse.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* (C) Copyright IBM Corp. 2018, 2020.
2+
* (C) Copyright IBM Corp. 2020.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,5 +60,10 @@ public class QueryResponse
6060
/// </summary>
6161
[JsonProperty("table_results", NullValueHandling = NullValueHandling.Ignore)]
6262
public List<QueryTableResult> TableResults { get; set; }
63+
/// <summary>
64+
/// Passages returned by Discovery.
65+
/// </summary>
66+
[JsonProperty("passages", NullValueHandling = NullValueHandling.Ignore)]
67+
public List<QueryResponsePassage> Passages { get; set; }
6368
}
6469
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* (C) Copyright IBM Corp. 2020.
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+
18+
using Newtonsoft.Json;
19+
20+
namespace IBM.Watson.Discovery.V2.Model
21+
{
22+
/// <summary>
23+
/// A passage query response.
24+
/// </summary>
25+
public class QueryResponsePassage
26+
{
27+
/// <summary>
28+
/// The content of the extracted passage.
29+
/// </summary>
30+
[JsonProperty("passage_text", NullValueHandling = NullValueHandling.Ignore)]
31+
public string PassageText { get; set; }
32+
/// <summary>
33+
/// The confidence score of the passage's analysis. A higher score indicates greater confidence.
34+
/// </summary>
35+
[JsonProperty("passage_score", NullValueHandling = NullValueHandling.Ignore)]
36+
public double? PassageScore { get; set; }
37+
/// <summary>
38+
/// The unique identifier of the ingested document.
39+
/// </summary>
40+
[JsonProperty("document_id", NullValueHandling = NullValueHandling.Ignore)]
41+
public string DocumentId { get; set; }
42+
/// <summary>
43+
/// The unique identifier of the collection.
44+
/// </summary>
45+
[JsonProperty("collection_id", NullValueHandling = NullValueHandling.Ignore)]
46+
public string CollectionId { get; set; }
47+
/// <summary>
48+
/// The position of the first character of the extracted passage in the originating field.
49+
/// </summary>
50+
[JsonProperty("start_offset", NullValueHandling = NullValueHandling.Ignore)]
51+
public long? StartOffset { get; set; }
52+
/// <summary>
53+
/// The position of the last character of the extracted passage in the originating field.
54+
/// </summary>
55+
[JsonProperty("end_offset", NullValueHandling = NullValueHandling.Ignore)]
56+
public long? EndOffset { get; set; }
57+
/// <summary>
58+
/// The label of the field from which the passage has been extracted.
59+
/// </summary>
60+
[JsonProperty("field", NullValueHandling = NullValueHandling.Ignore)]
61+
public string Field { get; set; }
62+
}
63+
}

Scripts/Services/Discovery/V2/Model/QueryResponsePassage.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)