No more copying of the entire matrix

This commit is contained in:
joachimschmidt557 2019-07-14 15:15:01 +02:00
parent 510fd64c4a
commit e30c22bcdf

View file

@ -9,11 +9,13 @@ proc predict(word:string, matrix:Matrix[uint16],
return "" return ""
let let
w = word2int[word] w = word2int[word]
rowW = matrix.row(w)
var var
maxI = 0 maxI = 0
maxW = 0u16 maxW = 0u16
for i, x in rowW:
# Get the maximum of that row
for i in 0 .. matrix.N - 1:
let x = matrix[w, i]
if x > maxW: if x > maxW:
maxI = i maxI = i
maxW = x maxW = x
@ -22,6 +24,7 @@ proc predict(word:string, matrix:Matrix[uint16],
proc updateMatrix(matrix:var Matrix[uint16], word1:string, word2:string, proc updateMatrix(matrix:var Matrix[uint16], word1:string, word2:string,
word2int:var TableRef[string, int], word2int:var TableRef[string, int],
int2word:var TableRef[int, string]) = int2word:var TableRef[int, string]) =
# Index the words if the aren't in the index yet
if not word2int.hasKey(word1): if not word2int.hasKey(word1):
let newIndex = len(word2int) let newIndex = len(word2int)
word2int[word1] = newIndex word2int[word1] = newIndex
@ -30,10 +33,14 @@ proc updateMatrix(matrix:var Matrix[uint16], word1:string, word2:string,
let newIndex = len(word2int) let newIndex = len(word2int)
word2int[word2] = newIndex word2int[word2] = newIndex
int2word[newIndex] = word2 int2word[newIndex] = word2
# Compute the indices
let let
w1 = word2int[word1] w1 = word2int[word1]
w2 = word2int[word2] w2 = word2int[word2]
if w1 < 20000 and w2 < 20000:
# Check if the matrix is large enough
if w1 < matrix.ld and w2 < matrix.ld:
matrix[w1, w2] += 1 matrix[w1, w2] += 1
proc interactive(matrix:Matrix[uint16], proc interactive(matrix:Matrix[uint16],
@ -50,7 +57,7 @@ proc main() =
statistics = false statistics = false
word = "I" word = "I"
texts:seq[string] texts:seq[string]
matrix = zeros(20000, 20000, uint16) matrix = zeros(20000, 20000, uint16, rowMajor)
word2int = newTable[string, int]() word2int = newTable[string, int]()
int2word = newTable[int, string]() int2word = newTable[int, string]()