Skip to content

Commit

Permalink
chore: use CountTable for weight mode
Browse files Browse the repository at this point in the history
  • Loading branch information
bit0r1n committed Aug 3, 2023
1 parent 3ae52f3 commit eb58d7e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion nimkov.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "1.2.1"
version = "1.2.2"
author = "bit0r1n"
description = "Text generator, based on Markov Chains (Markov text generator)"
license = "MIT"
Expand Down
22 changes: 10 additions & 12 deletions nimkov/generator.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type MarkovGenerator* = ref object
of mgtSimple:
seqModel: Table[string, seq[string]]
of mgtWeighted:
weightModel: Table[string, Table[string, int]]
weightModel: Table[string, CountTable[string]]

proc filterString(str: string): string =
var subResult = newSeq[string]()
Expand Down Expand Up @@ -54,11 +54,8 @@ proc add*(generator: MarkovGenerator, sample: string) =
generator.seqModel[currentFrame].add(nextFrame)
of mgtWeighted:
if currentFrame notin generator.weightModel:
generator.weightModel[currentFrame] = initTable[
string, int]()
if nextFrame notin generator.weightModel[currentFrame]:
generator.weightModel[currentFrame][nextFrame] = 1
else: generator.weightModel[currentFrame][nextFrame] += 1
generator.weightModel[currentFrame] = initCountTable[string]()
generator.weightModel[currentFrame].inc(nextFrame)
proc add*(generator: MarkovGenerator, samples: seq[string]) =
## Adds seqence of strings to samples.
for sample in samples:
Expand All @@ -67,15 +64,16 @@ proc add*(generator: MarkovGenerator, samples: seq[string]) =
proc samples*(generator: MarkovGenerator): seq[string] = generator.samples
## Returns all samples of generator.

proc model*(generator: MarkovGenerator): Table[string, seq[string]] =
proc model*(generator: MarkovGenerator): Table[string, CountTable[string]] =
## Returns model of generator.
case generator.kind
of mgtSimple:
result = generator.seqModel
of mgtWeighted:
result = initTable[string, seq[string]]()
for key, value in generator.weightModel.pairs:
result[key] = value.keys.toSeq
result = generator.weightModel
of mgtSimple:
for key, value in generator.seqModel:
result[key] = initCountTable[string]()
for frame in value:
result[key][frame] = 1 # always will be 1 since in simple model frames are unique

proc clear*(generator: MarkovGenerator) =
## Clears generator.
Expand Down

0 comments on commit eb58d7e

Please sign in to comment.