Introduction
Text tokenization breaks sentences into smaller pieces called tokens. This helps computers understand and work with text.
from tensorflow.keras.preprocessing.text import Tokenizer # Create a tokenizer tokenizer = Tokenizer(num_words=100) # Fit tokenizer on texts texts = ['I love machine learning', 'Tokenization is useful'] tokenizer.fit_on_texts(texts) # Convert texts to sequences sequences = tokenizer.texts_to_sequences(texts)
tokenizer = Tokenizer(num_words=5) texts = ['Hello world', 'Hello TensorFlow'] tokenizer.fit_on_texts(texts) print(tokenizer.word_index)
sequences = tokenizer.texts_to_sequences(['Hello TensorFlow world']) print(sequences)
tokenizer.fit_on_texts(['I like apples', 'You like oranges']) print(tokenizer.word_counts)
from tensorflow.keras.preprocessing.text import Tokenizer texts = ['I love machine learning', 'Machine learning is fun', 'I love coding'] # Create tokenizer to keep top 10 words tokenizer = Tokenizer(num_words=10) # Learn word indexes from texts tokenizer.fit_on_texts(texts) # Show word index print('Word Index:', tokenizer.word_index) # Convert texts to sequences sequences = tokenizer.texts_to_sequences(texts) print('Sequences:', sequences) # Show word counts print('Word Counts:', tokenizer.word_counts)
Tokenizer in TensorFlow text processing?tf.keras.preprocessing.text.from tensorflow.keras.preprocessing.text import Tokenizer texts = ['hello world', 'hello tensorflow'] tokenizer = Tokenizer() tokenizer.fit_on_texts(texts) sequences = tokenizer.texts_to_sequences(['hello tensorflow world']) print(sequences)
from tensorflow.keras.preprocessing.text import Tokenizer tokenizer = Tokenizer() texts = ['cat dog', 'dog mouse'] sequences = tokenizer.texts_to_sequences(texts) print(sequences)
tokenizer.fit_on_texts(texts) before texts_to_sequences.'apple banana apple orange' to sequences?num_words=3 limits tokenizer to top 3 frequent words.Pass the test and this pattern moves unknown