Skip to content

Instantly share code, notes, and snippets.

@Eligijus112
Created March 11, 2020 21:29
Show Gist options
  • Save Eligijus112/57fade4c008ce07a26a66c00f777ad0c to your computer and use it in GitHub Desktop.
Save Eligijus112/57fade4c008ce07a26a66c00f777ad0c to your computer and use it in GitHub Desktop.
A class to convert text strings to tensors for deep learning
mport numpy as np
from keras.preprocessing.sequence import pad_sequences
class TextToTensor():
def __init__(self, tokenizer, max_len):
self.tokenizer = tokenizer
self.max_len = max_len
def string_to_tensor(self, string_list: list) -> list:
"""
A method to convert a string list to a tensor for a deep learning model
"""
string_list = self.tokenizer.texts_to_sequences(string_list)
string_list = pad_sequences(string_list, maxlen=self.max_len)
return string_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment