Generating text using NLP based on parameters

I want to generate some text based on the value of certain parameters. For instance, let's say I want to generate descriptions of video games. So, besides real descriptions as training data, I would like that the model takes in account the following parameters (for example) about the game:

• Violent: yes
• Multiplatform: yes
• Drugs: no

So that if the game has drugs content, the output text has some phrase referring to it.

Is this possible? If so, how could I do it in Python? I was going to use LSTM neural networks in Tensorflow.

• Is there no other input to the generation of the description but these 3 binary features? – ncasas Mar 5 '20 at 13:45
• There may be more binary features, and a little manual introduction (i.e. "Call of duty is") or a categorical feature with the name – Javier López Tomás Mar 5 '20 at 14:22

1 Answer

A direct way would be to encode any binary input features as embedded vectors and add them together as the initial hidden state for the LSTM, and then you train it as a normal language model.

The "little manual introduction" could be supplied to the language model (together with the initial hidden state created from the binary features) at inference time and then use it autoregressively to generate the description.

Given that you are going to deal with proper nouns (i.e. the name of the game), you should use a vocabulary that does not lead to out-of-vocabulary words. I would suggest using a subword-level vocabulary (e.g. byte-pair encoding).

For the implementation, you could do it in Pytorch, using as a starting point their language model example.

• Thank you very much. It would be possible to do it in Keras instead of PyTorch or Keras is too high level for this kind of tasks? – Javier López Tomás Mar 9 '20 at 18:27
• It should be possible to do it in Keras. You could create a custom Layer class (inheriting from keras.engine.topology.Layer), add there a parameter with self.add_weight and then just tile it and concatenate it with the input. I have never done something like this, though, so maybe I am missing something. – ncasas Mar 9 '20 at 18:42