-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCodeLink 17.py
More file actions
98 lines (68 loc) · 3.01 KB
/
CodeLink 17.py
File metadata and controls
98 lines (68 loc) · 3.01 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
93
94
95
96
97
98
# THE COMPUTER ALWAYS WINS
# MIT PRESS
#
# CodeLink 17
# Imitation
# last revised 5/6/26
# There are countless ways to build an imitation engine using the ideas from
# Chapter Ten. I suggest starting with something simple that is easy to debug,
# and then later adding more complicated details. For instance, maybe start
# by searching the sample text to identify every unique 4-character phrase.
# Then, for each phrase, take note of what characters come next, and how often.
# Using that information, you can generate text that follows similar patterns.
#
# In that same spirit, use simple sample text at first. Once your code is working,
# then you can bring Taylor Swift and William Shakespeare into the mix.
#
import random
def makeChain (sample, chainLength):
# Use this function to build a chain based on some sample text.
# One approach is create one link for every unique phrase found in the text.
# For example, suppose you are building 4-character phrases. One link might
# be the 4 characters "c-o-n-s" and that link would then note that,
# after "cons", we sometimes have letter 't' ("construct", "constant"), we sometimes
# have letter 'i' ("consider"), and so on. Another link might then focus on the
# 4 characters "o-n-s-t" and record that, after "onst", we somtimes have the
# letter 'a' ("constant"), sometimes the letter 'r' ("construct"), etc.
chain = []
print("This needs to be implemented!")
return chain
def talk (chain, phrase, chain_length):
lettersUsed = 0
# start with a phrase that is in the sample
print(phrase, end="")
while lettersUsed < 1000:
# find the phrase in the chain, then choose from the options!
print("Needs to be implemented")
# main program
# ############
# Use simple sample text for now, so that you can easily debug.
# Later, you can add more complicated, longer samples like Taylor Swift lyrics or Shakespeare.
sample = "This dog. This cat. This dog. This horse. This dog. This mouse. This dog. This rat. This dog."
# When you are ready to add longer text, paste that text into a file named "sample.txt" in the same folder
# as this Python code, and then uncomment the below two lines of code. The code will then access your file
# and copy your text into the 'sample' variable.
# with open('sample.txt', 'r') as file:
# sample = file.read()
# define variables
trainedChain = []
# ask the user how many characters to use in each link
# when debugging, start with 4, and then think about whether to move to 1 or 2 or even 10
chainLength = int(input("How long do you want the chain? "))
print("")
trainedChain = makeChain(sample, chainLength)
print ("Here's the chain I created.")
for counter in range(len(trainedChain)):
print(trainedChain[counter])
print ("")
print()
# choose a starting phrase
startingPhrase = trainedChain[0][0]
# we are now ready to generate some text
print ("Now, let's talk.")
print ("")
talk(trainedChain, startingPhrase, chainLength)
print("")
print(" ... Enough.")
print()
print()