text.utf8_binarize
Stay organized with collections
Save and categorize content based on your preferences.
Decode UTF8 tokens into code points and return their bits.
text.utf8_binarize(
tokens, word_length=16, bits_per_char=24, replacement_char=65533, name=None
)
See the RetVec paper for details.
Example:
code_points = utf8_binarize("hello", word_length=3, bits_per_char=4)
print(code_points.numpy())
[0. 0. 0. 1. 1. 0. 1. 0. 0. 0. 1. 1.]
The codepoints are encoded bitwise in the little-endian order.
The inner dimension of the output is always word_length * bits_per_char
,
because extra characters are truncated / missing characters are padded,
and bits_per_char
lowest bits of each codepoint is stored.
Decoding errors (which in applications are often replaced with the character
U+65533 "REPLACEMENT CHARACTER") are represented with replacement_char
's
bits_per_char
lowest bits.
Args |
tokens
|
A Tensor of tokens (strings) with any shape.
|
word_length
|
Number of Unicode characters to process per word (the rest are
silently ignored; the output is zero-padded).
|
bits_per_char
|
The number of lowest bits of the Unicode codepoint to encode.
|
replacement_char
|
The Unicode codepoint to use on decoding errors.
|
name
|
The op name (optional).
|
Returns |
A tensor of floating-point zero and one values corresponding to the bits
of the token characters' Unicode code points.
|
Shape
|
[<shape of tokens>, word_length * bits_per_char] .
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-19 UTC.
[{
"type": "thumb-down",
"id": "missingTheInformationINeed",
"label":"Missing the information I need"
},{
"type": "thumb-down",
"id": "tooComplicatedTooManySteps",
"label":"Too complicated / too many steps"
},{
"type": "thumb-down",
"id": "outOfDate",
"label":"Out of date"
},{
"type": "thumb-down",
"id": "samplesCodeIssue",
"label":"Samples / code issue"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Other"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Easy to understand"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Solved my problem"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Other"
}]
{"lastModified": "Last updated 2024-07-19 UTC."}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-19 UTC."],[],[]]