使用 Analyze API 隨需應變分析資料

使用 Analyze API 透過 Discovery 服務的強化管線來處理文字文件,而不儲存來源文件中的任何資料。

只有企業方案部署及已安裝的部署才支援 Analyze API。

此方法很適合商業自動化用途。 例如,如果您想要分類電子郵件,則可以使用 Analyze API 來同步呼叫 Discovery,以取得電子郵件的分類。 然後,您可以在商業邏輯中使用該分類的輸出。

Analyze API 僅支援 JSON 文件。

當您使用 API 來分析文件時,您可以指定集合來與分析產生關聯,以指出您要如何處理文件。 文件未儲存在集合中。 相反地,集合的配置設定會套用至文件。 例如,如果您想要在文件中尋找實體參照,請針對套用 實體 強化的集合執行 Analyze API。 產生的文件分析會識別文件中的任何實體提及項目。

只針對一個集合提交分析要求,該集合已配置您要用來隨需應變分析文件的強化。 請記住,集合中的文件並不重要。 它是針對重要集合定義的強化。 如果您向數個集合提交要求,則會同時起始數個模型,這可能會導致要求失敗。

Analyze API 支援下列強化:

如需各語言支援的完整增益清單,請參閱 語言支援。

如需相關資訊,請參閱 Discovery API 參考資料。

分析範例

您提交進行分析的資料必須是 JSON 格式。 文字必須指定為字串; 不能指定為陣列。 例如,下列 JSON 檔案在 Quote 欄位中包含一個引號,您要分析以尋找文字中的任何關鍵字提及項目。

{
  "Author": "Jane Austen",
  "Book": "Pride and Prejudice",
  "Quote": "From this day you must be a stranger to one of your parents. Your mother will never see you again if you do not marry Mr. Collins, and I will never see you again if you do.",
  "Year": "1813/01/01",
  "Subject":"Parental love",
  "Speaker": "Mr. Bennett",
  "url": "https://www.gutenberg.org/files/1342/1342-h/1342-h.htm#link2HCH0020"
}

您知道專案中集合的名稱,其中 關鍵字 強化已配置為套用至集合中的文件。 您可以使用 API 來 列出集合,以尋找與您依名稱尋找之集合相關聯的 ID。

取得集合 ID 之後,請將它併入您提交的 POST 要求中,以將集合中的配置設定套用至 JSON 檔案。 例如,下列要求會在名為 favorites2.json 的檔案中提交 JSON Snippet,以進行關鍵字分析。

curl --location --request POST \
'https://my-cloud-pak-for-data-cluster/discovery/zen-wd/instances/{instance-id}/api/v2/ \
projects/{project-id}/collections/{collection-id}/analyze?version=2020-08-30' \
--header 'Authorization: Bearer ...' \
--form 'file=@"/quotations/favorites2.json"'

結果包含引號中可辨識的關鍵字清單。

{
  "result": {
    "enriched_Quote": [
      {
        "keywords": [
          {
            "text": "day",
            "mentions": [
              {
                "text": "day",
                "location": {
                  "begin": 10,
                  "end": 13
                }
              }
            ],
            "relevance": 0.673739
          },
          {
            "text": "stranger",
            "mentions": [
              {
                "text": "stranger",
                "location": {
                  "begin": 28,
                  "end": 36
                }
              }
            ],
            "relevance": 0.596757
          },
          {
            "text": "parents",
            "mentions": [
              {
                "text": "parents",
                "location": {
                  "begin": 52,
                  "end": 59
                }
              }
            ],
            "relevance": 0.568336
          },
          {
            "text": "mother",
            "mentions": [
              {
                "text": "mother",
                "location": {
                  "begin": 66,
                  "end": 72
                }
              }
            ],
            "relevance": 0.755562
          },
          {
            "text": "Mr. Collins",
            "mentions": [
              {
                "text": "Mr. Collins",
                "location": {
                  "begin": 118,
                  "end": 129
                }
              }
            ],
            "relevance": 0.945891
          }
        ]
      }
    ],
    "url": "https://www.gutenberg.org/files/1342/1342-h/1342-h.htm#link2HCH0020",
    "Subject": "Parental love",
    "Year": "1813/01/01",
    "Book": "Pride and Prejudice",
    "Author": "Jane Austen",
    "Quote": [
      "From this day you must be a stranger to one of your parents. Your mother will never see you again if you do not marry Mr. Collins, and I will never see you again if you do."
    ],
    "metadata": {
      "name": "favorites2.json"
    },
    "Speaker": "Mr. Bennett"
  },
  "notices": []
}

您無法提交物件陣列作為輸入。 例如,您可能想要分析多個報價,因此您的來源可能如下所示:

{
  "quotations":[
    {
      "Author": "Jane Austen",
      "Book": "Sense and Sensibility",
      "Quote": "Is there a felicity in the world superior to this?",
      "Year": "1811/01/01",
      "Subject": "Nature",
      "Speaker": "Marianne Dashwood",
      "url": "https://www.gutenberg.org/files/1342/1342-h/1342-h.htm#link2HCH0059"
    },
    {
      "Author": "Jane Austen",
      "Book": "Persuasion",
      "Quote": "A man does not recover from such a devotion of the heart to such a woman. He ought not; he does not.",
      "Subject": "Romantic love",
      "Year": "1818/01/01",
      "Speaker": "Captain Wentworth",
      "url": "https://www.gutenberg.org/files/105/105-h/105-h.htm#chap20"
    }
  ]
}

如果是的話,請將每一個物件分成個別檔案,並個別分析每一個檔案。

分析文字 Snippet

當您使用如下的語法以 JSON 格式指定文字時,可以提交文字進行分析:

{
"text":"The text that you want to analyze."
}

下列範例要求顯示如何分析您在要求中指定的文字,而不是在實體檔中傳遞的文字。

curl --location --request POST \
'https://my-cloud-pak-for-data-cluster/discovery/zen-wd/instances/{instance-id}/api/v2/ \
projects/{project-id}/collections/{collection-id}/analyze?version=2020-08-30' \
--header 'Authorization: Bearer ...' \
--form 'file={"text": "ISO 9000 is a standard."}'

回應可能如下所示。

{
  "result" : {
    "enriched_text" : [ {
      "entities" : [ {
        "text" : "ISO 9000",
        "type" : "my_iso_pattern",
        "mentions" : [ {
          "text" : "ISO 9000",
          "confidence" : 1.0,
          "location" : {
            "begin" : 0,
            "end" : 8
          }
        } ],
        "model_name" : "My ISO Pattern"
      }, {
        "text" : "9000",
        "type" : "Number",
        "mentions" : [ {
          "text" : "9000",
          "confidence" : 0.8,
          "location" : {
            "begin" : 4,
            "end" : 8
          }
        } ],
        "model_name" : "natural_language_understanding"
      } ]
    } ],
    "metadata" : { },
    "text" : [ "ISO 9000 is a standard." ]
  },
  "notices" : [ ]
}

分析 HTML 內容

當您使用如下的語法以 JSON 格式提交 HTML 時,可以分析 HTML:

{
"html":"<p>My html content.</p>"
}

下列範例要求顯示如何分析您在要求中指定的文字,而不是在實體檔中傳遞的文字。

要求所針對的集合使用下列強化,這表示這些強化會套用至您隨 API 要求提交的內容:

  • 實體
  • 關鍵字
  • Table Understanding

要求範例

要求的內文包含名稱為 file 的 form-data。 值是要分析的 JSON 內容。

curl --location --request POST \
'https://cpd-abc.example.com/discovery/abc-wd/instances/1671204318684041/api/v2/projects/d457fcd9-a4ce-4637-a340-33123b5cbe2c/collections/2d47dbcc-64c7-84e9-0000-01851bb9d998/analyze?version=2020-08-30' \
--header 'Authorization: Bearer ...' \
--form 'file={
  "html":"<html><head>This is my html file</head><body><p>My file contains a table.</p><table><tbody><tr><th>Holiday</th><th>Popular greeting</th></tr><tr><td>Christmas</td><td>Merry Christma!s</td></tr></tbody></table></body></html>",
  "text":"This is a sentence that contains key words, such as George Washington and Boston, MA."
}'

結果

結果會顯示已提交之 text 及 html 欄位上「實體」、「關鍵字」及「表格理解」強化的輸出。

{
    "result": {
        "text": [
            "This is a sentence that contains key words, such as George Washington and Boston, MA."
        ],
        "enriched_text": [
            {
                "keywords": [
                    {
                        "text": "George Washington",
                        "mentions": [
                            {
                                "text": "George Washington",
                                "location": {
                                    "begin": 52,
                                    "end": 69
                                }
                            }
                        ],
                        "relevance": 0.952591
                    },
                    {
                        "text": "Boston",
                        "mentions": [
                            {
                                "text": "Boston",
                                "location": {
                                    "begin": 74,
                                    "end": 80
                                }
                            }
                        ],
                        "relevance": 0.578079
                    },
                    {
                        "text": "MA",
                        "mentions": [
                            {
                                "text": "MA",
                                "location": {
                                    "begin": 82,
                                    "end": 84
                                }
                            }
                        ],
                        "relevance": 0.146905
                    }
                ],
                "entities": [
                    {
                        "text": "George Washington",
                        "type": "Location",
                        "mentions": [
                            {
                                "text": "George Washington",
                                "confidence": 0.54922265,
                                "location": {
                                    "begin": 52,
                                    "end": 69
                                }
                            }
                        ],
                        "model_name": "natural_language_understanding"
                    },
                    {
                        "text": "Boston, MA",
                        "type": "Location",
                        "mentions": [
                            {
                                "text": "Boston, MA",
                                "confidence": 0.66049105,
                                "location": {
                                    "begin": 74,
                                    "end": 84
                                }
                            }
                        ],
                        "model_name": "natural_language_understanding"
                    }
                ]
            }
        ],
        "metadata": {},
        "enriched_html": [
            {
                "tables": [
                    {
                        "body_cells": [
                            {}
                        ],
                        "location": {
                            "begin": 99,
                            "end": 183
                        },
                        "row_headers": [],
                        "key_value_pairs": [],
                        "section_title": {},
                        "contexts": [],
                        "text": "Holiday Popular greeting Christmas Merry Christmas!",
                        "table_headers": [],
                        "title": {},
                        "column_headers": []
                    }
                ]
            }
        ],
        "html": [
            "<html><head>This is my html file</head><body><p>My file contains a table.</p><table><tbody><tr><th>Holiday</th><th>Popular greeting</th></tr><tr><td>Christmas</td><td>Merry Christmas!</td></tr></tbody></table></body></html>"
        ]
    },
    "notices": []
}

分析 API 限制

下表顯示 Analyze API 的檔案大小及使用限制。

套用至 Analyze API 用法的限制
部署類型 檔案大小限制 並行集合限制 每個集合限制的並行查詢數
Cloud Pak for Data 已安裝部署 無限制 無限制 無限制
企業計劃受管理部署 50 KB 5 5

從 Discovery Cartridge for IBM Cloud Pak for Data 使用 Analyze API 會影響授權使用情況。 如需相關資訊,請參閱 授權資訊。

監控使用 IBM Cloud Pak for DataIBM Software Hub

您可以從 API 使用情形 頁面監視 Analyze API 的使用情形。

API 使用情形 頁面僅可從已安裝的部署中使用。 對於企業方案,分析方法呼叫資訊會與查詢方法呼叫資訊結合,並報告為查詢度量的一部分。

若要存取 API 使用頁面,請開啟「專案」頁面,選擇「資料使用」,然後選擇「API 使用」。

開始日期
API 呼叫監視期間的開始日期。
結束日期
API 呼叫監視期間的結束日期。
30 天通話總計
在 開始日期 和 結束日期所指出的 30 天時間間隔內,對 Analyze API 進行的呼叫數。 時間間隔是透過計算 API 呼叫次數最多的連續時間來決定。 30 天視窗更新為 API 呼叫變更最多的時間間隔。

API 使用量在 API 使用量監控開始後一段時間才會顯示。 即使列出的 30 天期間包括現行日期,也可能會延遲顯示 30 天呼叫總數 的最終總數。


  1. 若要讓表格瞭解強化產生任何結果,輸入必須包含要分析的 <table> HTML 元素。 ↩︎