TehMillhouse / PyMarkovChain

Simple markov chain implementation in python
Other
97 stars 17 forks source link

Example not working #3

Closed abelsonlive closed 11 years ago

abelsonlive commented 11 years ago

When I run your example code:

from pymarkovchain import MarkovChain
MarkovChain().generateDatabase("This is some language to analyze")
MarkovChain().generateString()

I get the following error:

Database file not found, using empty database

Database file could not be writtenDatabase file not found, using empty database

Traceback (most recent call last):
  File "markov.py", line 8, in <module>
    print MarkovChain().generateString()
  File "/Library/Python/2.7/site-packages/pymarkovchain/MarkovChain.py", line 98, in generateString
    return self._accumulateWithSeed('')
  File "/Library/Python/2.7/site-packages/pymarkovchain/MarkovChain.py", line 122, in _accumulateWithSeed
    nextWord = self._nextWord(seed)
  File "/Library/Python/2.7/site-packages/pymarkovchain/MarkovChain.py", line 130, in _nextWord
    probmap = self.db[lastword]
KeyError: ''
TehMillhouse commented 11 years ago

Ah, I see what the problem is here. You don't seem to have write access to wherever MarkovChain.py is sitting. This results in the database generated from the given string not being saved to disk.

When you're trying to generate a string from this, it's thus forced to use an empty database, which (obviously) doesn't yield any strings. I'll adjust the example so it works. For now, try giving the MarkovChain another parameter specifying a path to where it should store its database file (to a place where you have the necessary privileges). Like this:

from pymarkovchain import MarkovChain
mc = MarkovChain("./markov")
mc.generateDatabase("This is another string of Text. It's automatically separated at question marks, periods, newlines and exclamation marks. This can be changed by giving generateDatabase an optional sentenceSep parameter")
mc.generateString()