jsvine / markovify

A simple, extensible Markov chain generator.
MIT License
3.31k stars 350 forks source link

make_sentence_that_contains #52

Open ekt1701 opened 7 years ago

ekt1701 commented 7 years ago

Is it possible to create a Markov chain that contains a key word?

I'm thinking of a chat bot situation. For example, the word "computer" is used in the utterance, then this command would be called: text_model.make_sentence_that_contains("computer")

If so, could you please either add this to markovify, or show me how I could write the code for my personal use.

Many thanks.

jsvine commented 7 years ago

There's no built-in method to do this in markovify. The easiest way for you to do it would probably be to have a while generate sentences until it generates one that contains the word you want. E.g.,

while True:
    sentence = text_model.make_sentence()
    if "computer" in sentence: break
print(sentence)
ekt1701 commented 7 years ago

Thanks, I will give that a try.

voltaxvoltax commented 4 years ago

I'm sorry to re-open this Feature Request, but I've been trying the solution approach of using the while loop and with a corpus of 6mb it takes almost 30 seconds (or more) to generate a sentence containing the specific word. It's somehow possible for you to implement this feature in code so it would be more fast to create sentences?

Thank you very much, and continue the good work!

jsvine commented 4 years ago

Hi @voltaxvoltax, and thanks for reminding me about this thread. Implementing a more efficient version of make_sentence_that_contains would require a nontrivial amount of experimentation and testing. That said, I’m open to including such a feature in markovify. If you (or anyone else reading this thread) would like to try coding a proposal, drop a note here and we can discuss how the feature might work.

Assuming the word is fish, to construct a sentence containing that word, we’d need to generate the following:

  1. The words that come after fish. This is easy, since we can just use .make_sentence_with_start(‘fish’, strict = False).

  2. The words that come before fish. This is more complicated; we would first need to calculate the reverse Markov probabilities for the corpus. That logic isn’t currently built into markovify.

caerulius commented 4 years ago

Just to +1 interest in this, I have a project right now with this exact use case. I agree with your analysis that the only way to achieve this is a reverse markov probability, but it would be highly useful. Just to describe my goal quickly, it would be to 'half-simulate' a conversation between different models. The first one generates a sentence, the second one generates a sentence containing some word from the first one, and so on.

Thanks for the consideration. I'd be very tolerant to longer model processing time if there were a second reverse model processed at the same time.

JGCoelho commented 4 years ago

You could generate a sentence that begins with the keyword and them generate another random sentence, cut it on a verb (using nltk) and append it at the beggining of the other sentence.

brienna commented 4 years ago

This works fine for me on a corpus of 14 MB.

generated_headlines = []
while len(generated_headlines) < 25:
    headline = model.make_sentence(tries=100)
    if headline is not None:
        if word.lower() in headline.split(' '):
            generated_headlines.append(headline)

It helps to also check the Markov chain to see how often that word appears. If it appears at a very low frequency, that might be why it takes a long time for the model to generate a sentence with the word in it. There's nothing we can do about it in that case, other than to choose a different word or to substantially increase the size of the corpus.

If make_sentence_that_contains becomes a feature, a word-frequency check might help.

Sylv-Lej commented 4 years ago

I'm working in a naive approach on my fork.

To build the reversed matrix, i reverse the sentences then i process it, probably not the best idea but it looks like it's working. We will probably need another method in order to create that matrix.

In order to make it work i combine make_sentence_that_finish with .make_sentence_with_start(‘fish’, strict = False) as @jsvine explained.

Still working in multi words containing, it's experimental right now.

Any suggestions are welcome