poetic.predictor module¶
Making poetic predictions using models.
The predictor module provides interfaces for poetry predictions with the Predictor class. To make prediction, an instance of the Predictor is needed with the necessary model and gensim dictionary is needed.
Examples
The most common use case for the predictor module is with its default settings. To make a prediction with string, below is an example:
import poetic
pred = poetic.Predictor()
result = pred.predict("This is an example.")
To make a prediction with a text file, use the following codes:
import poetic
pred = poetic.Predictor()
result = pred.predict_file("<PATH>")
-
class
poetic.predictor.
Predictor
(model: Optional[tensorflow.keras.Model] = None, dict: Optional[gensim.corpora.dictionary.Dictionary] = None, force_download_assets: Optional[bool] = False)¶ Bases:
object
The
Predictor()
class processes and predicts inputs for poetic scores. It can be used as the single interface of the package with other modules built as helpers.- Parameters
model (tensorflow.keras.Model, optional) – A pre-trained keras model. The default model will be loaded if no model is supplied. If a custom model is supplied, a custom gensim dictionary is recommended for it to work correctly although not strictly enforced.
dict (gensim.corpora.dictionary.Dictionary, optional) – Gensim dictionary for word IDs. If nothing is supplied, the default dictionary will be loaded. The default dictionary will be required for the the default model to work correctly although it is not strictly enforced.
force_download_assets (bool, optional) – Wheher to download assets (the default models) without asking/user input.
-
model
¶ The pre-trained keras model.
- Type
tensorflow.keras.Model
-
dict
¶ Gensim dictionary for word IDs.
- Type
gensim.corpora.dictionary.Dictionary
-
force_download_assets
¶ Wheher to download assets without asking.
- Type
bool
-
predict
(input: str) → poetic.predictor.Predictions¶ Predict poetic score from string.
- Parameters
input (str) – Text content to be predicted.
- Returns
A Predictions object with predicted scores of the given input.
- Return type
- Raises
poetic.exceptions.InputLengthError – Error for processing input length of zero.
-
predict_file
(path: str) → poetic.predictor.Predictions¶ Predict poetic score from file.
This method essentially loads the text file into a string of text and then calls the predict method.
- Parameters
path (str) – The path to the text file.
- Returns
A Predictions object with predicted scores of the given input.
- Return type
- Raises
poetic.exceptions.InputLengthError – Error for processing empty file, resulting in input length of zero.
-
preprocess
(input: str) → numpy.ndarray¶ Preprocess inputs: tokenize, to lower, and padding.
- Parameters
input (str) – Text either in a single string or a list of strings.
- Returns
A 2-d numpy array of processed inputs.
- Return type
numpy.ndarray
- Raises
poetic.exceptions.InputLengthError – Error for processing input length of zero.
-
tokenize
(input: str) → List[List[str]]¶ Tokenize text input into sentences and then words.
- Parameters
input (str) – A string or list of strings of text.
- Returns
A 2-d list of tokenized words.
- Return type
list(str)
-
word_id
(input: List[List[str]]) → List[List[int]]¶ Convert tokenized words to word IDs using a gensim dictionary.
- Parameters
input (list) – A 2-d list of tokenized words.
- Returns
A 2-d list of word ids.
- Return type
list