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_with_start() returns error if only one word is used #34

Closed
Kyu opened this issue Oct 19, 2016 · 5 comments
Closed

make_sentence_with_start() returns error if only one word is used #34

Kyu opened this issue Oct 19, 2016 · 5 comments

Comments

@Kyu
Copy link

Kyu commented Oct 19, 2016

It used to just use memory and do nothing, but now it gives:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Me\AppData\Local\Programs\Python\Python35-32\lib\site-packages\mark
ovify\text.py", line 140, in make_sentence_with_start
    return self.make_sentence(init_state, **kwargs)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python35-32\lib\site-packages\mark
ovify\text.py", line 122, in make_sentence
    words = prefix + self.chain.walk(init_state)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python35-32\lib\site-packages\mark
ovify\chain.py", line 114, in walk
    return list(self.gen(init_state))
  File "C:\Users\Me\AppData\Local\Programs\Python\Python35-32\lib\site-packages\mark
ovify\chain.py", line 103, in gen
    next_word = self.move(state)
  File "C:\Users\Me\AppData\Local\Programs\Python\Python35-32\lib\site-packages\mark
ovify\chain.py", line 89, in move
    choices, weights = zip(*self.model[state].items())
KeyError: ('I',)

This is in Python 3.5.2

Code:

with open("BlocSpeaks.txt", "r", encoding="utf-8") as txt:
    text = text.read()

text_model = markovify.Text(text)

text_model.make_sentence_with_start("I")
#Returns error as seen above

text_model.make_sentence_with_start("I dont")

'''"I dont play BLOC tho fucking homofag I can not longer stand Commy's defiance he is unk
illable He only was because she hadn\u2019t done anything."'''

text_model.make_sentence_with_start("I will")
'''I will ban you without a special kind of dull tbh back If you need to check bloc?''''
@jsvine
Copy link
Owner

jsvine commented Oct 19, 2016

What version of markovify are you using? You'll need v0.4.1 or later to be able to pass one-word beginnings.

@Kyu
Copy link
Author

Kyu commented Oct 20, 2016

Yep, that was the issue 🤦

@Kyu
Copy link
Author

Kyu commented Oct 20, 2016

It's now returning ('_BEGIN', 'word') , but I guess that's an issue with not having enough sample to use.

Do you have any tips on managing memory usage? cpu usage spikes up and down 90% when this is used, and memory usage doubles and stays high. This is with a 2mb txt file

@jsvine
Copy link
Owner

jsvine commented Oct 20, 2016

Hmmm, haven't noticed memory problems like that lately, but it's possible I've overlooked an edge case. Have you tried profiling your program?

If you send over the script and corpus, I can also try taking a look.

@Kyu
Copy link
Author

Kyu commented Oct 20, 2016

async def bloc_speaks(prompt=""):
    try:
        with open(os.getcwd() + '/BlocSim/BlocSpeaks.txt',
                  'r', encoding='utf-8') as f:
            text = f.read()
    except FileNotFoundError:
        print("File not found")
        return
    text_model = markovify.Text(text)
    try:
        if prompt:
            sentence = text_model.make_sentence_with_start(prompt)
            return sentence
        else:
            sentence = text_model.make_sentence()
            return sentence
    except Exception as e:
        return (e)

corpus

@jsvine jsvine closed this as completed Mar 31, 2017
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

2 participants