How can Tensorflow text be used to split the strings by character using unicode_split() in Python?

TensorflowPythonServer Side ProgrammingProgramming

Tensorflow text can be used to split the strings by character using ‘unicode_split’ method, by first encoding the split strings, and then assigning the function call to a variable. This variable holds the result of the function call.

Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?

We will use the Keras Sequential API, which is helpful in building a sequential model that is used to work with a plain stack of layers, where every layer has exactly one input tensor and one output tensor.

A neural network that contains at least one layer is known as a convolutional layer. We can use the Convolutional Neural Network to build learning model. 

TensorFlow Text contains collection of text related classes and ops that can be used with TensorFlow 2.0. The TensorFlow Text can be used to preprocess sequence modelling.

We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.

Tokenization is the method of breaking down a string into tokens. These tokens can be words, numbers, or punctuation.

The important interfaces include Tokenizer and TokenizerWithOffsets each of which have a single method tokenize and tokenize_with_offsets respectively. There are multiple tokenizers, each of which implement TokenizerWithOffsets (which extends the Tokenizer class). This includes an option to get byte offsets into the original string. This helps know the bytes in the original string the token was created from.

Example

print("The encoded characters are split")
tokens = tf.strings.unicode_split([u"仅今年前".encode('UTF-8')], 'UTF-8')
print("The tokenized data is converted to a list")
print(tokens.to_list())

Code credit −https://www.tensorflow.org/tutorials/tensorflow_text/intro

Output

The encoded characters are split
The tokenized data is converted to a list
[[b'\xe4\xbb\x85', b'\xe4\xbb\x8a', b'\xe5\xb9\xb4', b'\xe5\x89\x8d']]

Explanation

  • All tokenizers return RaggedTensors with the inner-most dimension of tokens mapped to the original individual strings.

  • The resulting shape's rank increases by one.

  • When tokenizing languages without using whitespace to segment words, it is common to split by character.

  • This can be done using the unicode_split op found in Tensorflow core.

  • Once the unicode_split is called, the tokenized data is added to a list.

raja
Published on 12-Feb-2021 11:25:40
Advertisements