blob: fee6b902bfd8aef70cb9a9c508b2ebd71cde7af5 [file] [log] [blame]
borenet93558dc2015-06-25 07:47:40 -07001-- Generate n-grams of Skia API calls from SKPs.
2
3-- To test this locally, run:
4-- $ GYP_DEFINES="skia_shared_lib=1" make lua_pictures
borenet8cd8f942015-06-29 12:54:25 -07005-- $ out/Debug/lua_pictures -q -r $SKP_DIR -l tools/lua/ngrams.lua > /tmp/lua-output
6-- $ lua tools/lua/ngrams_aggregate.lua
borenet93558dc2015-06-25 07:47:40 -07007
8-- To run on Cluster Telemetry, copy and paste the contents of this file into
9-- the box at https://skia-tree-status.appspot.com/skia-telemetry/lua_script,
10-- and paste the contents of ngrams_aggregate.lua into the "aggregator script"
11-- box on the same page.
12
13-- Change n as desired.
borenet3691e662015-06-30 10:24:03 -070014-- CHANGEME
borenet93558dc2015-06-25 07:47:40 -070015local n = 3
borenet3691e662015-06-30 10:24:03 -070016-- CHANGEME
borenet93558dc2015-06-25 07:47:40 -070017
18-- This algorithm uses a list-of-lists for each SKP. For API call, append a
19-- list containing just the verb to the master list. Then, backtrack over the
20-- last (n-1) sublists in the master list and append the verb to those
21-- sublists. At the end of execution, the master list contains a sublist for
22-- every verb in the SKP file. Each sublist has length n, with the exception of
23-- the last n-1 sublists, which are discarded in the summarize() function,
24-- which generates counts for each n-gram.
25
26local ngrams = {}
27local currentFile = ""
28
29function sk_scrape_startcanvas(c, fileName)
30 currentFile = fileName
31 ngrams[currentFile] = {}
32end
33
34function sk_scrape_endcanvas(c, fileName)
35end
36
37function sk_scrape_accumulate(t)
38 table.insert(ngrams[currentFile], {t.verb})
39 for i = 1, n-1 do
40 local idx = #ngrams[currentFile] - i
41 if idx > 0 then
42 table.insert(ngrams[currentFile][idx], t.verb)
43 end
44 end
45end
46
47function sk_scrape_summarize()
48 -- Count the n-grams.
49 local counts = {}
50 for file, ngramsInFile in pairs(ngrams) do
51 for i = 1, #ngramsInFile - (n-1) do
52 local ngram = table.concat(ngramsInFile[i], " ")
53 if counts[ngram] == nil then
54 counts[ngram] = 1
55 else
56 counts[ngram] = counts[ngram] + 1
57 end
58 end
59 end
60
61 -- Write out code for aggregating.
62 for ngram, count in pairs(counts) do
63 io.write("if counts['", ngram, "'] == nil then counts['", ngram, "'] = ", count, " else counts['", ngram, "'] = counts['", ngram, "'] + ", count, " end\n")
64 end
65end