-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompose_template.py
More file actions
45 lines (37 loc) · 1.1 KB
/
Copy pathcompose_template.py
File metadata and controls
45 lines (37 loc) · 1.1 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
import os
import re
import string
import random
from graph import Graph, Vertex
def get_words_from_text(text_path):
with open(text_path, "rb") as file:
text = file.read().decode("utf-8")
text = " ".join(text.split())
text = text.lower()
text = text.translate(str.maketrans("", "", string.punctuation))
words = text.split()
return words[:1000]
def make_graph(words):
g = Graph()
prev_word = None
for word in words:
word_vertex = g.get_vertex(word)
if prev_word:
prev_word.increment_edge(word_vertex)
prev_word = word_vertex
g.generate_probability_mappings()
return g
def compose(g, words, length=50):
composition = []
current = g.get_vertex(random.choice(words))
for _ in range(length):
composition.append(current.value)
current = g.get_next_word(current)
return composition
def main():
words = get_words_from_text("hp_socerer_stone.txt")
graph = make_graph(words)
composition = compose(graph, words, 100)
print(" ".join(composition))
if __name__ == '__main__':
main()