本文整理汇总了Python中pymarkovchain.MarkovChain.generateStringWithSeed方法的典型用法代码示例。如果您正苦于以下问题:Python MarkovChain.generateStringWithSeed方法的具体用法?Python MarkovChain.generateStringWithSeed怎么用?Python MarkovChain.generateStringWithSeed使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymarkovchain.MarkovChain
的用法示例。
在下文中一共展示了MarkovChain.generateStringWithSeed方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MarkovBot
# 需要导入模块: from pymarkovchain import MarkovChain [as 别名]
# 或者: from pymarkovchain.MarkovChain import generateStringWithSeed [as 别名]
class MarkovBot(BotPlugin):
def __init__(self):
self.markov = MarkovChain()
@botcmd
def talk(self, mess, args):
""" Generate a sentence based on database """
return self.markov.generateString()
@botcmd
def complete(self, mess, args):
""" Try to complete a sentence """
return self.markov.generateStringWithSeed(args)
@botcmd
def gendbfromfile(self, mess, args):
""" Generate markov chain word database """
try:
with open(args) as txtFile:
txt = txtFile.read()
except IOError as e:
return 'Error: could not open text file'
# At this point, we've got the file contents
if self.markov.generateDatabase(txt):
return 'Done.'
else:
return 'Error: Could not generate database'
@botcmd
def gendbfromstring(self, mess, args):
if self.markov.generateDatabase(args):
return 'Done.'
else:
return 'Error: Could not generate database from String'
@botcmd
def gendbfromurl(self, mess, args):
req = requests.get(args)
if req.ok and self.markov.generateDatabase(req.content):
return 'Done.'
else:
return 'Error: Could not generate database from URL'
示例2: file_get_contents
# 需要导入模块: from pymarkovchain import MarkovChain [as 别名]
# 或者: from pymarkovchain.MarkovChain import generateStringWithSeed [as 别名]
for root, dirs, files in os.walk(_dir, topdown=False):
random.shuffle(files)
for name in files:
num_files -= 1
if num_files > 0:
_path = "{}/{}".format(_dir,name)
train_text += file_get_contents(_path)
#scrub train_text
train_text = re.sub('<[^<]+?>', '', train_text) #remove html
train_text = re.sub(r'[^a-zA-Z. ]+', ' ', train_text).replace('\n', '') #remove non-alphanumeric chars
for word in remove_words:
train_text = train_text.replace(word,'') #remove words
#create markov db
mc.generateDatabase(train_text)
#tweet
for x in range(0,num_tweets):
random.shuffle(seed_words)
status = (ucfirst(mc.generateStringWithSeed(seed_words[0])) + ". ")
if not validate_tweet(status):
continue;
try:
status = api.PostUpdate(status)
except:
pass
time.sleep(wait_time_between_tweets_in_secs)
示例3: MarkovBot
# 需要导入模块: from pymarkovchain import MarkovChain [as 别名]
# 或者: from pymarkovchain.MarkovChain import generateStringWithSeed [as 别名]
class MarkovBot(BotPlugin):
def __init__(self):
super(MarkovBot, self).__init__()
self.sentenceSep = None
self.markov = MarkovChain(dbFilePath="./markovdb")
@botcmd
def talk(self, mess, args):
""" Generate a sentence based on database """
return self.markov.generateString()
@botcmd
def complete(self, mess, args):
""" Try to complete a sentence """
return self.markov.generateStringWithSeed(args)
@botcmd
def gendbfromfile(self, mess, args):
""" Generate markov chain word database based on local file """
try:
with open(args) as txtFile:
txt = txtFile.read()
except IOError as e:
return "Error: could not open text file"
# At this point, we've got the file contents
if self.sentenceSep:
result = self.markov.generateDatabase(txt, self.sentenceSep)
else:
result = self.markov.generateDatabase(txt)
if result:
return "Done."
else:
return "Error: Could not generate database"
@botcmd
def setsentencesep(self, mess, args):
""" Specify how to detect sentence borders """
self.sentenceSep = args
@botcmd
def gendbfromstring(self, mess, args):
""" Generate markov chain word database based on given string """
if self.sentenceSep:
result = self.markov.generateDatabase(args, self.sentenceSep)
else:
result = self.markov.generateDatabase(args)
if result:
return "Done."
else:
return "Error: Could not generate database from String"
@botcmd
def gendbfromurl(self, mess, args):
""" Generate markov chain word database based on contents of url """
response, content = httplib2.Http().request(args, "GET")
if response["status"] == "200":
if self.sentenceSep:
result = self.markov.generateDatabase(content.decode("utf-8"), self.sentenceSep)
else:
result = self.markov.generateDatabase(content.decode("utf-8"))
if result:
return "Done."
else:
return "Error: Could not generate database from URL"
示例4: MarkovChain
# 需要导入模块: from pymarkovchain import MarkovChain [as 别名]
# 或者: from pymarkovchain.MarkovChain import generateStringWithSeed [as 别名]
from pymarkovchain import MarkovChain
# Create an instance of the markov chain. By default, it uses MarkovChain.py's location to
# store and load its database files to. You probably want to give it another location, like so:
mc = MarkovChain("./markovdb.txt")
with open("../formatted.txt", "r") as myfile:
data=myfile.read()
# To generate the markov chain's language model, in case it's not present
mc.generateDatabase(data)
# To let the markov chain generate some text, execute
#print( mc.generateString() )
print( mc.generateStringWithSeed("the") )
示例5: print
# 需要导入模块: from pymarkovchain import MarkovChain [as 别名]
# 或者: from pymarkovchain.MarkovChain import generateStringWithSeed [as 别名]
final += ts
sd = True
seed = ts.split()[-1]
seed = seed.translate(string.maketrans("",""), string.punctuation)
c += 1
print ("\n" + final + "\n")
raw_input("press enter to continue...")
if raw_input("press enter to begin.") == "beta":
pgraph()
sd = False
s = ""
while True:
if not sd:
ts = mc.generateString()
else:
ts = mc.generateStringWithSeed(s)
if countString(ts) >= msl:
os.system("clear")
print ("\n" + ts + "\n")
sd = False
s = raw_input("\npress enter to generate string. : ")
if s == '!pg':
pgraph()
if (len(s) > 0):
if f.find(s) != -1:
sd = True
else:
raw_input('could not find "' + s + '" in database\npress enter to continue')
sd = False
示例6: MarkovChain
# 需要导入模块: from pymarkovchain import MarkovChain [as 别名]
# 或者: from pymarkovchain.MarkovChain import generateStringWithSeed [as 别名]
regex = re.compile('[%s]' % re.escape(string.punctuation))
text = regex.sub(" b", text)
# generate MC data
mc = MarkovChain("./markov")
mc.generateDatabase(text)
f = open("potential_tweets.txt", "a")
# generate and evaluate tweets
while 1:
try:
seed = sys.argv[1]
except:
seed = None
if seed is not None:
tweet = mc.generateStringWithSeed(seed).title()
else:
tweet = mc.generateString().title()
print tweet
answer = raw_input("Tweet this text? (yes|no|edit) ")
if answer == "yes":
f.write(tweet)
break
elif answer == "edit":
tweet = raw_input("Enter in the edited text: ")
f.write(tweet)
break