在 TensorFlow 和 Python 中,什麼是 Unicode 指令碼?


每個 Unicode 碼位都屬於一個唯一的碼位集合,稱為指令碼。字元的指令碼決定了該字元所屬的語言。TensorFlow 帶有“strings.unicode_script”方法,可以幫助查詢給定碼位將使用哪個指令碼。指令碼程式碼是 int32 值,可以對映到 Unicode 國際元件 (ICU) 的 UScriptCode 值。

閱讀更多: 什麼是 TensorFlow 以及 Keras 如何與 TensorFlow 協作建立神經網路?

我們將瞭解如何使用 Python 表示 Unicode 字串,以及如何使用 Unicode 等價物來操作它們。首先,在指令碼檢測的幫助下,根據指令碼檢測將 Unicode 字串分成標記,並藉助標準字串操作的 Unicode 等價物。

我們正在使用 Google Colaboratory 來執行以下程式碼。Google Colab 或 Colaboratory 幫助在瀏覽器上執行 Python 程式碼,無需任何配置,並且可以免費訪問 GPU(圖形處理單元)。Colaboratory 建立在 Jupyter Notebook 之上。

print("The below represent '芸' and 'Б' respectively")
uscript = tf.strings.unicode_script([33464, 1041])  
print(uscript.numpy())   # [17, 8] == [USCRIPT_HAN, USCRIPT_CYRILLIC]
print("Applying to multidimensional strings")
print(tf.strings.unicode_script(batch_chars_ragged))

程式碼來源:https://www.tensorflow.org/tutorials/load_data/unicode

輸出

The below represent '芸' and 'Б' respectively
[17   8]
Applying to multidimensional strings
<tf.RaggedTensor [[25, 25, 25, 25, 25], [25, 25, 25, 25, 0, 25, 25, 0, 25, 25, 25, 0, 25, 25, 25, 25, 25, 25, 25, 0, 25, 25, 25, 25, 25, 25, 25, 25], [25, 25, 25, 25, 25, 25, 25, 25, 25], [0]]>

解釋

  • 每個 Unicode 碼位都屬於一個唯一的碼位集合,稱為指令碼。
  • 字元的指令碼有助於確定該字元可能所屬的語言。
  • TensorFlow 提供 tf.strings.unicode_script 操作來查詢給定碼位將使用哪個指令碼。
  • 指令碼程式碼是 int32 值,對映到 Unicode 國際元件 (ICU) 的 UScriptCode 值。
  • tf.strings.unicode_script 操作也可以應用於多維 tf.Tensors 或 tf.RaggedTensors 碼位。

更新於: 2021年2月20日

91 次檢視

開啟您的 職業生涯

完成課程獲得認證

立即開始
廣告

© . All rights reserved.