

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 步驟 3：將實體分析輸出格式化為 Amazon Kendra 中繼資料
<a name="tutorial-search-metadata-format-output"></a>

若要將 Amazon Comprehend 擷取的實體轉換為 Amazon Kendra 索引所需的中繼資料格式，請執行 Python 3 指令碼。轉換的結果會存放在 Amazon S3 儲存貯體的 `metadata` 資料夾中。

如需 Amazon Kendra 中繼資料格式和結構的詳細資訊，請參閱 [S3 文件中繼資料](https://docs.aws.amazon.com/kendra/latest/dg/s3-metadata.html)。

**Topics**
+ [下載並擷取 Amazon Comprehend 輸出](#tutorial-search-metadata-format-output-download-extract)
+ [將輸出上傳到 S3 儲存貯體](#tutorial-search-metadata-format-output-upload)
+ [將輸出轉換為 Amazon Kendra 中繼資料格式](#tutorial-search-metadata-format-output-script)
+ [清除 Amazon S3 儲存貯體](#tutorial-search-metadata-format-output-cleanup)

## 下載並擷取 Amazon Comprehend 輸出
<a name="tutorial-search-metadata-format-output-download-extract"></a>

若要格式化 Amazon Comprehend 實體分析輸出，您必須先下載 Amazon Comprehend 實體分析`output.tar.gz`封存，並擷取實體分析檔案。

### 下載並擷取輸出檔案 （主控台）
<a name="tutorial-search-metadata-download-extract-console"></a>

1. 在 Amazon Comprehend 主控台導覽窗格中，導覽至**分析任務**。

1. 選擇您的實體分析任務 `data-entities-analysis`。

1. 在**輸出**下，選擇**輸出資料位置**旁顯示的連結。這會將您重新導向至 S3 儲存貯體中的`output.tar.gz`封存。

1. 在**概觀**索引標籤中，選擇**下載**。
**提示**  
所有 Amazon Comprehend 分析任務的輸出都具有相同的名稱。重新命名封存可協助您更輕鬆地追蹤。

1. 解壓縮下載的 Amazon Comprehend 檔案並解壓縮到您的裝置。

### 下載並擷取輸出檔案 (AWS CLI)
<a name="tutorial-search-metadata-download-extract-cli"></a>

1. 若要存取 S3 儲存貯體中包含實體分析任務結果的 Amazon Comprehend 自動產生資料夾的名稱，請使用 [describe-entities-detection-job](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/comprehend/describe-entities-detection-job.html) 命令：

------
#### [ Linux ]

   ```
   aws comprehend describe-entities-detection-job \
             --job-id entities-job-id \
             --region aws-region
   ```

   其中：
   + *entities-job-id* 是從 `comprehend-job-id` 儲存的 [步驟 2：在 Amazon Comprehend 上執行實體分析任務](tutorial-search-metadata-entities-analysis.md)，
   + *aws-region* 是您的 AWS 區域。

------
#### [ macOS ]

   ```
   aws comprehend describe-entities-detection-job \
             --job-id entities-job-id \
             --region aws-region
   ```

   其中：
   + *entities-job-id* 是從 `comprehend-job-id` 儲存[步驟 2：在 Amazon Comprehend 上執行實體分析任務](tutorial-search-metadata-entities-analysis.md)的
   + *aws-region* 是您的 AWS 區域。

------
#### [ Windows ]

   ```
   aws comprehend describe-entities-detection-job ^
             --job-id entities-job-id ^
             --region aws-region
   ```

   其中：
   + *entities-job-id* 是從 `comprehend-job-id` 儲存[步驟 2：在 Amazon Comprehend 上執行實體分析任務](tutorial-search-metadata-entities-analysis.md)的
   + *aws-region* 是您的 AWS 區域。

------

1. 從實體任務描述中的`OutputDataConfig`物件中，將`S3Uri`值複製並儲存為文字編輯器`comprehend-S3uri`上的 。
**注意**  
此`S3Uri`值的格式類似於 *s3：//amzn-s3-demo-bucket/.../output/output.tar.gz*。

1. 若要下載實體輸出封存，請使用[複製](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/cp.html)命令：

------
#### [ Linux ]

   ```
   aws s3 cp s3://amzn-s3-demo-bucket/.../output/output.tar.gz path/output.tar.gz
   ```

   其中：
   + *s3：//amzn-s3-demo-bucket/.../output/output.tar.gz* 是您儲存為 `S3Uri`的值`comprehend-S3uri`，
   + *path/* 是您要儲存輸出的本機目錄。

------
#### [ macOS ]

   ```
   aws s3 cp s3://amzn-s3-demo-bucket/.../output/output.tar.gz path/output.tar.gz
   ```

   其中：
   + *s3：//amzn-s3-demo-bucket/.../output/output.tar.gz* 是您儲存為 `S3Uri`的值`comprehend-S3uri`，
   + *path/* 是您要儲存輸出的本機目錄。

------
#### [ Windows ]

   ```
   aws s3 cp s3://amzn-s3-demo-bucket/.../output/output.tar.gz path/output.tar.gz
   ```

   其中：
   + *s3：//amzn-s3-demo-bucket/.../output/output.tar.gz* 是您儲存為 `S3Uri`的值`comprehend-S3uri`，
   + *path/* 是您要儲存輸出的本機目錄。

------

1. 若要擷取實體輸出，請在終端機視窗上執行下列命令：

------
#### [ Linux ]

   ```
   tar -xf path/output.tar.gz -C path/
   ```

   其中：
   + *path/* 是本機裝置上下載`output.tar.gz`封存的檔案路徑。

------
#### [ macOS ]

   ```
   tar -xf path/output.tar.gz -C path/
   ```

   其中：
   + *path/* 是本機裝置上下載`output.tar.gz`封存的檔案路徑。

------
#### [ Windows ]

   ```
   tar -xf path/output.tar.gz -C path/
   ```

   其中：
   + *path/* 是本機裝置上下載`output.tar.gz`封存的檔案路徑。

------

在此步驟結束時，您的裝置上應該有一個名為 的檔案，`output`其中包含 Amazon Comprehend 識別的實體清單。

## 將輸出上傳到 S3 儲存貯體
<a name="tutorial-search-metadata-format-output-upload"></a>

下載並擷取 Amazon Comprehend 實體分析檔案後，您可以將擷取`output`的檔案上傳至 Amazon S3 儲存貯體。

### 上傳解壓縮的 Amazon Comprehend 輸出檔案 （主控台）
<a name="tutorial-search-metadata-upload-output-console"></a>

1. 開啟位於 [https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/) 的 Amazon S3 主控台。

1. 在**儲存貯**體中，按一下儲存貯體的名稱，然後選擇**上傳**。

1. 在**檔案和資料夾中**，選擇**新增檔案**。

1. 在對話方塊中，導覽至您裝置中解壓縮`output`的檔案，選取檔案，然後選擇**開啟**。

1. 保留**目的地**、**許可**和**屬性**的預設設定。

1. 選擇**上傳**。

### 上傳解壓縮的 Amazon Comprehend 輸出檔案 (AWS CLI)
<a name="tutorial-search-metadata-upload-output-cli"></a>

1. 若要將解壓縮`output`的檔案上傳至您的儲存貯體，請使用[複製](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/cp.html)命令：

------
#### [ Linux ]

   ```
   aws s3 cp path/output s3://amzn-s3-demo-bucket/output
   ```

   其中：
   + *path/* 是解壓縮檔案的本機`output`檔案路徑，
   + amzn-s3-demo-bucket 是 S3 儲存貯體的名稱。

------
#### [ macOS ]

   ```
   aws s3 cp path/output s3://amzn-s3-demo-bucket/output
   ```

   其中：
   + *path/* 是解壓縮檔案的本機`output`檔案路徑，
   + amzn-s3-demo-bucket 是 S3 儲存貯體的名稱。

------
#### [ Windows ]

   ```
   aws s3 cp path/output s3://amzn-s3-demo-bucket/output
   ```

   其中：
   + *path/* 是解壓縮檔案的本機`output`檔案路徑，
   + amzn-s3-demo-bucket 是 S3 儲存貯體的名稱。

------

1. 若要確保`output`檔案已成功上傳至 S3 儲存貯體，請使用 [list](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/ls.html) 命令檢查其內容：

------
#### [ Linux ]

   ```
   aws s3 ls s3://amzn-s3-demo-bucket/
   ```

   其中：
   + amzn-s3-demo-bucket 是 S3 儲存貯體的名稱。

------
#### [ macOS ]

   ```
   aws s3 ls s3://amzn-s3-demo-bucket/
   ```

   其中：
   + amzn-s3-demo-bucket 是 S3 儲存貯體的名稱。

------
#### [ Windows ]

   ```
   aws s3 ls s3://amzn-s3-demo-bucket/
   ```

   其中：
   + amzn-s3-demo-bucket 是 S3 儲存貯體的名稱。

------

## 將輸出轉換為 Amazon Kendra 中繼資料格式
<a name="tutorial-search-metadata-format-output-script"></a>

若要將 Amazon Comprehend 輸出轉換為 Amazon Kendra 中繼資料，請執行 Python 3 指令碼。如果您使用的是 主控台，則使用 AWS CloudShell 進行此步驟。

### 執行 Python 3 指令碼 （主控台）
<a name="tutorial-search-metadata-format-output-console"></a>

1. 在您的裝置上下載 [converter.py.zip](https://docs.aws.amazon.com/kendra/latest/dg/samples/converter.py.zip) ziped 檔案。

1. 解壓縮 Python 3 檔案 `converter.py`。

1. 登入 [AWS 管理主控台](https://aws.amazon.com/console/)，並確保您的 AWS 區域設定為與 S3 儲存貯體和 Amazon Comprehend 分析任務相同的區域。

1. 在頂端導覽列的**搜尋**方塊中選擇**AWS CloudShell 圖示**或輸入 **AWS CloudShell** 以啟動環境。
**注意**  
第一次在新的瀏覽器視窗中 AWS CloudShell 啟動時，歡迎面板會顯示並列出主要功能。關閉此面板並顯示命令提示後， shell 已準備好進行互動。

1. 終端機準備好後，從導覽窗格中選擇**動作**，然後從功能表中選擇**上傳檔案**。

1. 在開啟的對話方塊中，選擇**選取檔案**，然後從`converter.py`您的裝置中選擇下載的 Python 3 檔案。選擇**上傳**。

1. 在 AWS CloudShell 環境中，輸入下列命令：

   ```
   python3 converter.py
   ```

1. 當 shell 界面提示您**輸入 S3 儲存貯體的名稱**時，請輸入 S3 儲存貯體的名稱，然後按 Enter 鍵。

1. 當 shell 界面提示您**輸入 Comprehend 輸出檔案的完整檔案路徑**時，請輸入 **output**並按 Enter 鍵。

1. 當 shell 界面提示您**輸入中繼資料資料夾的完整 filepath** 時，請輸入 **metadata/**並按 Enter 鍵。

**重要**  
若要正確格式化中繼資料，步驟 8-10 中的輸入值必須完全正確。

### 執行 Python 3 指令碼 (AWS CLI)
<a name="tutorial-search-metadata-format-output-cli"></a>

1. 若要下載 Python 3 檔案 `converter.py`，請在終端機視窗上執行下列命令：

------
#### [ Linux ]

   ```
   curl -o path/converter.py.zip https://docs.aws.amazon.com/kendra/latest/dg/samples/converter.py.zip
   ```

   其中：
   + *path/* 是您要儲存壓縮檔案的位置的檔案路徑。

------
#### [ macOS ]

   ```
   curl -o path/converter.py.zip https://docs.aws.amazon.com/kendra/latest/dg/samples/converter.py.zip
   ```

   其中：
   + *path/* 是您要儲存壓縮檔案的位置的檔案路徑。

------
#### [ Windows ]

   ```
   curl -o path/converter.py.zip https://docs.aws.amazon.com/kendra/latest/dg/samples/converter.py.zip
   ```

   其中：
   + *path/* 是您要儲存壓縮檔案的位置的檔案路徑。

------

1. 若要擷取 Python 3 檔案，請在終端機視窗上執行下列命令：

------
#### [ Linux ]

   ```
   unzip path/converter.py.zip -d path/
   ```

   其中：
   + *path/* 是您已儲存 的檔案路徑`converter.py.zip`。

------
#### [ macOS ]

   ```
   unzip path/converter.py.zip -d path/
   ```

   其中：
   + *path/* 是您已儲存 的檔案路徑`converter.py.zip`。

------
#### [ Windows ]

   ```
   tar -xf path/converter.py.zip -C path/
   ```

   其中：
   + *path/* 是您已儲存 的檔案路徑`converter.py.zip`。

------

1. 執行下列命令，確認 Boto3 已安裝在您的裝置上。

------
#### [ Linux ]

   ```
   pip3 show boto3
   ```

------
#### [ macOS ]

   ```
   pip3 show boto3
   ```

------
#### [ Windows ]

   ```
   pip3 show boto3
   ```

------
**注意**  
如果您尚未安裝 Boto3，請執行 `pip3 install boto3` 進行安裝。

1. 若要執行 Python 3 指令碼來轉換`output`檔案，請執行下列命令。

------
#### [ Linux ]

   ```
   python path/converter.py
   ```

   其中：
   + *path/* 是您已儲存 的檔案路徑`converter.py.zip`。

------
#### [ macOS ]

   ```
   python path/converter.py
   ```

   其中：
   + *path/* 是您已儲存 的檔案路徑`converter.py.zip`。

------
#### [ Windows ]

   ```
   python path/converter.py
   ```

   其中：
   + *path/* 是您已儲存 的檔案路徑`converter.py.zip`。

------

1. 當 AWS CLI 提示您 時`Enter the name of your S3 bucket`，輸入 S3 儲存貯體的名稱，然後按 Enter 鍵。

1. 當 AWS CLI 提示您輸入 時`Enter the full filepath to your Comprehend output file`，輸入 **output**並按 Enter 鍵。

1. 當 AWS CLI 提示您輸入 時`Enter the full filepath to your metadata folder`，輸入 **metadata/**並按 Enter 鍵。

**重要**  
若要正確格式化中繼資料，步驟 5-7 中的輸入值必須完全正確。

在此步驟結束時，格式化的中繼資料會存放在 S3 儲存貯體中的 `metadata` 資料夾內。

## 清除 Amazon S3 儲存貯體
<a name="tutorial-search-metadata-format-output-cleanup"></a>

由於 Amazon Kendra 索引會同步存放在儲存貯體中的所有檔案，因此建議您清除 Amazon S3 儲存貯體，以防止多餘的搜尋結果。

### 清除 Amazon S3 儲存貯體 （主控台）
<a name="tutorial-search-metadata-cleanup-bucket-console"></a>

1. 開啟位於 [https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/) 的 Amazon S3 主控台。

1. 在**儲存貯體**中，選擇您的儲存貯體，然後選取 Amazon Comprehend 實體分析輸出資料夾、Amazon Comprehend 實體分析`.temp`檔案，以及解壓縮的 Amazon Comprehend `output` 檔案。

1. 從**概觀**索引標籤選擇**刪除**。

1. 在**刪除物件**中，選擇**永久刪除物件？****permanently delete**，然後在文字輸入欄位中輸入 。

1. 選擇 **Delete objects** (刪除物件)。

### 清除 Amazon S3 儲存貯體 (AWS CLI)
<a name="tutorial-search-metadata-cleanup-bucket-cli"></a>

1. 若要刪除 S3 儲存貯體中的所有檔案和資料夾，但 `data`和 `metadata` 資料夾除外，請使用 中的 [Remove](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/rm.html) 命令 AWS CLI：

------
#### [ Linux ]

   ```
   aws s3 rm s3://amzn-s3-demo-bucket/ --recursive --exclude "data/*" --exclude "metadata/*"
   ```

   其中：
   + amzn-s3-demo-bucket 是 S3 儲存貯體的名稱。

------
#### [ macOS ]

   ```
   aws s3 rm s3://amzn-s3-demo-bucket/ --recursive --exclude "data/*" --exclude "metadata/*"
   ```

   其中：
   + amzn-s3-demo-bucket 是 S3 儲存貯體的名稱。

------
#### [ Windows ]

   ```
   aws s3 rm s3://amzn-s3-demo-bucket/ --recursive --exclude "data/*" --exclude "metadata/*"
   ```

   其中：
   + amzn-s3-demo-bucket 是 S3 儲存貯體的名稱。

------

1. 若要確保物件已成功從 S3 儲存貯體中刪除，請使用 [list](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/ls.html) 命令檢查其內容：

------
#### [ Linux ]

   ```
   aws s3 ls s3://amzn-s3-demo-bucket/
   ```

   其中：
   + amzn-s3-demo-bucket 是 S3 儲存貯體的名稱。

------
#### [ macOS ]

   ```
   aws s3 ls s3://amzn-s3-demo-bucket/
   ```

   其中：
   + amzn-s3-demo-bucket 是 S3 儲存貯體的名稱。

------
#### [ Windows ]

   ```
   aws s3 ls s3://amzn-s3-demo-bucket/
   ```

   其中：
   + amzn-s3-demo-bucket 是 S3 儲存貯體的名稱。

------

在此步驟結束時，您已將 Amazon Comprehend 實體分析輸出轉換為 Amazon Kendra 中繼資料。您現在已準備好建立 Amazon Kendra 索引。