Markov chain text generator: Difference between revisions

Add Swift
m (→‎{{header|Go}}: fix typo induced bug)
(Add Swift)
Line 874:
you're a humbug? asked Dorothy. A balloon, said Oz, for I have no right to command them
once</pre>
 
=={{header|Swift}}==
{{trans|Python}}
{{works with|Swift|4.2}}
<lang swift>import Foundation
 
func makeRule(input: String, keyLength: Int) -> [String: [String]] {
let words = input.components(separatedBy: " ")
var rules = [String: [String]]()
var i = keyLength
for word in words[i...] {
let key = words[i-keyLength..<i].joined(separator: " ")
rules[key, default: []].append(word)
i += 1
}
return rules
}
 
func makeString(rule: [String: [String]], length: Int) -> String {
var oldWords = rule.keys.randomElement()!.components(separatedBy: " ")
var string = oldWords.joined(separator: " ") + " "
for _ in 0..<length {
let key = oldWords.joined(separator: " ")
guard let newWord = rule[key]?.randomElement() else { return string }
string += newWord + " "
for ii in 0..<oldWords.count {
oldWords[ii] = oldWords[(ii + 1) % oldWords.count]
}
oldWords[oldWords.index(before: oldWords.endIndex)] = newWord
}
return string
}
 
let inputLoc = CommandLine.arguments.dropFirst().first!
let input = FileManager.default.contents(atPath: inputLoc)!
let inputStr = String(data: input, encoding: .utf8)!
let rule = makeRule(input: inputStr, keyLength: 3)
let str = makeString(rule: rule, length: 300)
 
print(str)</lang>
 
{{out}}
<pre>$ ./main /path/to/alice_oz.txt
with a crash, whereupon the Scarecrow's clothes fell out of the clouds to rule over us. Still, for many days they grieved over the loss of my heart. While I was in love I was the happiest man on earth; but no one came near them nor spoke to them because of the great beam the house rested on, two feet were sticking out, shod in silver shoes with pointed toes. Oh, dear! Oh, dear! cried Dorothy, clasping her hands together in dismay. The house must have fallen on her. Whatever shall we do? There is nothing to be done, I wonder?' As she said this, she looked up, but it was the first to break the silence. 'What day of the month is it?' he said, turning to Alice as it spoke. 'As wet as ever,' said Alice in a piteous tone. And she thought of herself, 'I wish the creatures wouldn't be so stingy about it, you know-' She had quite forgotten the Duchess by this time, and was going to dive in among the leaves, which she found to be nothing but the stars over them; and they rested very well indeed. In the morning they traveled on until they came to the great Throne Room, where he saw, sitting in the emerald throne, a most lovely Lady. She was dressed in a green uniform and wearing a long green beard. Here are strangers, said the Guardian of the Gates lived. This officer unlocked their spectacles to put them back in his great box, and then he struck at the Tin Woodman passed safely under it. Come on! he shouted to the others. These she also led to rooms, and each one of them can explain it,' said the King, 'and don't look at me like that!' He got behind</pre>