-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkoshitan.py
52 lines (44 loc) · 1.56 KB
/
koshitan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import random
from collections import defaultdict
class MarkovChain:
def __init__(self):
self.model = defaultdict(lambda: defaultdict(int))
def doTraining(self, data):
for i in range(len(data) - 1):
currentState = data[i]
nextState = data[i + 1]
self.model[currentState][nextState] += 1
def nextCharacter(self, currentState):
nextStates = self.model[currentState]
total = sum(nextStates.values())
randomVal = random.uniform(0, total)
cumulative = 0
for state, count in nextStates.items():
cumulative += count
if randomVal <= cumulative:
return state
return None
def generateString(self, start, length):
result = [start]
currentState = start
for _ in range(length - 1):
nextStates = self.nextCharacter(currentState)
if not nextStates:
break
result.append(nextStates)
currentState = nextStates
return "".join(result)
def generateUntilMatch(self, match, start, length):
counts = 0
while True:
generatedString = self.generateString(start, length)
counts += 1
yield counts, generatedString
if generatedString == match:
break
data = "しかのこのこのここしたんたん"
mc = MarkovChain()
mc.doTraining(data)
generatedText = mc.generateUntilMatch(match=data, start=data[0], length=len(data))
for count, text in generatedText:
print(count, text)