-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsuedo.txt
76 lines (57 loc) · 1.98 KB
/
psuedo.txt
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function findTok(subVec, tok){
for i from 0 to length(subVec)
if subVec[i].token == tok
return i
return -1
}
function genMatrix(string input, M, tokens){
last = ""
while !input.end() //populate matrix
tok = ""
do
input >> tok
while tok == "" AND !input.end()
if tok == "" //trailing whitespaces at the end of the file will slip through the do-while
break
tokInd = findTok(tokens, tok) //find index of the current token
if tokInd == -1
newWord = {length(M), 0, tok}
tokens.push(newWord)
tokInd = length(M)
M.push([])
parentVec = findTok(tokens, last)
if parentVec != -1
ind = inVec(M[parentVec], tok)
if ind != -1
M[parentVec][ind].prob++
else
M[parentVec].push({1.0, tok})
tokens[parentVec].occurrences++
last = tok
for i from 0 to length(M) //calculate transition probabilites
for j from 0 to length(M[i])
M[i][j].prob = M[i][j].prob / tokens[i].occurrences
}
function genText(M, tokens, size){
first = rand(length(M)) //pick a random first word from matrix
choice = 0
curWord = M[first].token
for i from 0 to size
print(curWord)
curIndex = findTok(tokens, curWord)
probSum = 0.0
choice = rand(100)
if length(M[curIndex]) == 0 //if there are no transitions available
for j from 0 to length(tokens)
probsum += 100 * (tokens[j].occurrences / length(tokens))
if choice < probsum
curWord = tokens[j].token
break
else
for j from 0 to length(M[curIndex])
probsum += M[curIndex][j].prob * 100
if choice < probsum
curWord = M[curIndex][j].token
break
print(curWord)
}