-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmarkov.html
More file actions
92 lines (78 loc) · 2.88 KB
/
markov.html
File metadata and controls
92 lines (78 loc) · 2.88 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Markov Chain Story Generator</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
max-width: 800px;
margin: 20px auto;
padding: 20px;
}
#story {
margin-top: 20px;
padding: 10px;
border: 1px solid #ccc;
}
button {
padding: 10px 20px;
background-color: #006400;
color: white;
border: none;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Markov Chain Story Generator</h1>
<p>Click the button below to generate a random story based on the combined centos text.</p>
<button onclick="generateStory()">Generate Story</button>
<div id="story"></div>
<script>
// Seed text from the combined centos story
const seedText = `Reflecting on my childhood, I realize that raising me on the unworry drug robbed me of any chance. Nathen appeared next, a thin young man with straight black hair...`;
// Function to build the Markov Chain model
function buildMarkovChain(text) {
const words = text.split(/\s+/);
const chain = {};
for (let i = 0; i < words.length - 1; i++) {
const word = words[i].toLowerCase();
const nextWord = words[i + 1].toLowerCase();
if (!chain[word]) {
chain[word] = [];
}
chain[word].push(nextWord);
}
return chain;
}
// Function to generate a random story based on the Markov Chain
function generateStory() {
const markovChain = buildMarkovChain(seedText);
let currentWord = getRandomWord(Object.keys(markovChain));
let story = [capitalize(currentWord)];
// Generate 100 words in the story
for (let i = 0; i < 100; i++) {
const nextWords = markovChain[currentWord.toLowerCase()];
if (!nextWords || nextWords.length === 0) {
break; // End the story if no next words
}
currentWord = getRandomWord(nextWords);
story.push(currentWord);
}
// Display the story
document.getElementById('story').innerHTML = story.join(' ') + '.';
}
// Helper function to get a random word from an array
function getRandomWord(words) {
return words[Math.floor(Math.random() * words.length)];
}
// Helper function to capitalize the first word
function capitalize(word) {
return word.charAt(0).toUpperCase() + word.slice(1);
}
</script>
</body>
</html>