Use TableRef instead of copying tables all the time

This commit is contained in:
joachimschmidt557 2019-07-14 14:37:30 +02:00
parent b04df0cce1
commit 510fd64c4a

View file

@ -3,8 +3,8 @@ import parseopt, tables, strutils
import neo import neo
proc predict(word:string, matrix:Matrix[uint16], proc predict(word:string, matrix:Matrix[uint16],
word2int:Table[string, int], word2int:TableRef[string, int],
int2word:Table[int, string]):string = int2word:TableRef[int, string]):string =
if not word2int.hasKey(word): if not word2int.hasKey(word):
return "" return ""
let let
@ -20,8 +20,8 @@ proc predict(word:string, matrix:Matrix[uint16],
return int2word[maxI] return int2word[maxI]
proc updateMatrix(matrix:var Matrix[uint16], word1:string, word2:string, proc updateMatrix(matrix:var Matrix[uint16], word1:string, word2:string,
word2int:var Table[string, int], word2int:var TableRef[string, int],
int2word:var Table[int, string]) = int2word:var TableRef[int, string]) =
if not word2int.hasKey(word1): if not word2int.hasKey(word1):
let newIndex = len(word2int) let newIndex = len(word2int)
word2int[word1] = newIndex word2int[word1] = newIndex
@ -37,8 +37,8 @@ proc updateMatrix(matrix:var Matrix[uint16], word1:string, word2:string,
matrix[w1, w2] += 1 matrix[w1, w2] += 1
proc interactive(matrix:Matrix[uint16], proc interactive(matrix:Matrix[uint16],
word2int:Table[string, int], word2int:TableRef[string, int],
int2word:Table[int, string]) = int2word:TableRef[int, string]) =
echo "-= Interactive prediction =-" echo "-= Interactive prediction =-"
for line in stdin.lines: for line in stdin.lines:
echo predict(line, matrix, word2int, int2word) echo predict(line, matrix, word2int, int2word)
@ -51,8 +51,8 @@ proc main() =
word = "I" word = "I"
texts:seq[string] texts:seq[string]
matrix = zeros(20000, 20000, uint16) matrix = zeros(20000, 20000, uint16)
word2int = initTable[string, int]() word2int = newTable[string, int]()
int2word = initTable[int, string]() int2word = newTable[int, string]()
# Parse arguments # Parse arguments
while true: while true: