This commit is contained in:
Dominic Zimmer 2021-07-11 12:18:42 +02:00
parent a0de5b03b7
commit eba5259afb
9 changed files with 0 additions and 116 deletions

34
foo.py
View File

@ -1,34 +0,0 @@
#!/bin/python3.10
from dataclasses import dataclass, field
from typing import Union, Iterator
@dataclass(frozen=True)
class Infty:
def __repr__(self):
return ""
inf = Infty()
@dataclass(frozen=True)
class WrappedNumber:
value: int = 5
def __add__(self, other):
match other:
case Infty():
return inf
case WrappedNumber(val):
return WrappedNumber(self.value + val)
case _:
raise Exception("Invalid argument")
def __mul__(self, other):
return WrappedNumber(self.value + other.value)
def __matmul__(self, other):
return WrappedNumber(self.value ** other.value)
W = WrappedNumber
MYNat = WrappedNumber | Infty

82
main.py
View File

@ -1,82 +0,0 @@
CORPUS = "craze.txt"
from dataclasses import dataclass, field
from itertools import chain
from random import choices
from typing import Optional
from time import sleep
import sys
@dataclass(frozen=True)
class Token:
value: str
@dataclass(frozen=True)
class PureToken(Token):
pass
@dataclass(frozen=True)
class EndToken(Token):
pass
def tokenize(lines: list[str]) -> list[Token]:
tokenlists = [ line.strip().split() for line in lines ]
words = chain.from_iterable(tokenlists) # flattened
return list(map(Token, words))
class МарковNode:
def __init__(self, token):
self.successors = {} # othertoken -> count
self.count = 0 # total number of successors
self.token = token
def insert(self, other):
self.successors[other] = self.successors.get(other, 0) + 1
self.count += 1
def step(self) -> Optional[Token]:
# TODO: Tag token if 2-gram was deterministic
match (choices(list(self.successors.keys()), list(self.successors.values()))):
case [x]:
return x
case _:
return None
class Марков:
# tokens : list[Token] = []
def __init__(self, filename):
with open(filename, "r") as f:
lines = f.readlines()
self.tokens : list[Token]= tokenize(lines)
self.chain = { token: МарковNode(token) for token in set(self.tokens) } # Token -> MarkowNode
for (a,b) in zip(self.tokens, self.tokens[1:]):
self.chain[a].insert(b)
def step(self, token: Token) -> Optional[Token]:
return self.chain[token].step()
mc = Марков(CORPUS)
def gen(s: Optional[Token]):
while s:
yield s
s = mc.step(s)
def ihateGen(s):
s = mc.step(s)
return [s, lambda: ihateGen(s)]
g = gen(Token("The"))
while True:
print(next(g).value, end=" ")
sys.stdout.flush()
sleep(0.2)
#
#

View File

View File