Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make_sentence_that_contains #52

Open
ekt1701 opened this issue Jan 11, 2017 · 8 comments
Open

make_sentence_that_contains #52

ekt1701 opened this issue Jan 11, 2017 · 8 comments

Comments

@ekt1701
Copy link

ekt1701 commented Jan 11, 2017

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
Copy link
Owner

jsvine commented Jan 12, 2017

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)

@jsvine jsvine closed this as completed Jan 12, 2017
@ekt1701
Copy link
Author

ekt1701 commented Jan 12, 2017

Thanks, I will give that a try.

@voltaxvoltax
Copy link

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
Copy link
Owner

jsvine commented Jan 27, 2020

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.

@jsvine jsvine reopened this Jan 27, 2020
@caerulius
Copy link

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
Copy link

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
Copy link
Contributor

brienna commented May 16, 2020

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants