@simontiger/

Markov Text

Python

No description

fork
loading
Files
  • main.py

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.3988743518751221","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"aW1wb3J0IHJhbmRvbQ0KDQp0ZXh0ID0gIiINCg0KcHJpbnQoIlR5cGUgeW91ciB0ZXh0IGhlcmUgKHR5cGUgRU5EIHRvIGVuZCBpdCk6IikNCg0Kd2hpbGUgVHJ1ZToNCiAgbGluZSA9IGlucHV0KCIiKQ0KICBpZiBsaW5lICE9ICJFTkQiOiB0ZXh0ICs9IGxpbmUgKyAiXG4iDQogIGVsc2U6IGJyZWFrDQoNCnRleHQgPSB0ZXh0WzpsZW4odGV4dCktMV0NCg0Kb3JkZXIgPSBpbnQoaW5wdXQoJ1R5cGUgdGhlIG9yZGVyIChob3cgbXVjaCBpdCBtYWtlcyBzZW5zZSkgaGVyZTogJykpDQpudW0gPSBpbnQoaW5wdXQoIlR5cGUgdGhlIG51bWJlciBvZiBjaGFyYWN0ZXJzIGhlcmU6ICIpKQ0KDQppbnB1dCgiR2VuZXJhdGUgbWUgYSBiZWF1dGlmdWwgdGV4dCIpDQoNCmRlZiBtYXJrb3ZfYmxhbmtldCh0ZXh0LCBvcmRlcik6DQogIHJlc3VsdCA9IHt9DQoNCiAgZm9yIGkgaW4gcmFuZ2UobGVuKHRleHQpIC0gb3JkZXIgKyAxKToNCiAgICBuZ3JhbSA9ICIiDQogICAgZm9yIG9mZiBpbiByYW5nZShvcmRlcik6DQogICAgICBuZ3JhbSArPSB0ZXh0W2krb2ZmXQ0KICAgIA0KICAgIGlmIG5vdCBuZ3JhbSBpbiByZXN1bHQ6DQogICAgICByZXN1bHRbbmdyYW1dID0gW10NCiAgICBpZiBpIDwgbGVuKHRleHQpIC0gb3JkZXI6DQogICAgICByZXN1bHRbbmdyYW1dLmFwcGVuZCh0ZXh0W2krb3JkZXJdKQ0KICANCiAgcmV0dXJuIHJlc3VsdA0KDQpkZWYgbWFya292X2NoYWluKGJsYW5rZXQsIG51bSk6DQogIGtleXMgPSBibGFua2V0LmtleXMoKQ0KICBuZ3JhbSA9IHJhbmRvbS5jaG9pY2UobGlzdChrZXlzKSkNCiAgbmV3X3RleHQgPSBuZ3JhbQ0KICBmb3IgaSBpbiByYW5nZShudW0pOg0KICAgIHRyeToNCiAgICAgIG54dCA9IHJhbmRvbS5jaG9pY2UoYmxhbmtldFtuZ3JhbV0pDQogICAgICBuZXdfdGV4dCArPSBueHQNCiAgICAgIG5ncmFtICs9IG54dA0KICAgICAgbmdyYW0gPSBuZ3JhbVsxOl0NCiAgICBleGNlcHQgSW5kZXhFcnJvcjoNCiAgICAgIGJyZWFrDQogIHJldHVybiBuZXdfdGV4dA0KDQpuZXdfdGV4dCA9IG1hcmtvdl9jaGFpbihtYXJrb3ZfYmxhbmtldCh0ZXh0LCBvcmRlciksIG51bSkNCnByaW50KG5ld190ZXh0KQ=="},"asBuffer":null},"loaded":true}}
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
import random

text = ""

print("Type your text here (type END to end it):")

while True:
  line = input("")
  if line != "END": text += line + "\n"
  else: break

text = text[:len(text)-1]

order = int(input('Type the order (how much it makes sense) here: '))
num = int(input("Type the number of characters here: "))

input("Generate me a beautiful text")

def markov_blanket(text, order):
  result = {}

  for i in range(len(text) - order + 1):
    ngram = ""
    for off in range(order):
      ngram += text[i+off]
    
    if not ngram in result:
      result[ngram] = []
    if i < len(text) - order:
      result[ngram].append(text[i+order])
  
  return result

def markov_chain(blanket, num):
  keys = blanket.keys()
  ngram = random.choice(list(keys))
  new_text = ngram
  for i in range(num):
    try:
      nxt = random.choice(blanket[ngram])
      new_text += nxt
      ngram += nxt
      ngram = ngram[1:]
    except IndexError:
      break
  return new_text

new_text = markov_chain(markov_blanket(text, order), num)
print(new_text)