• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1-- 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
5-- $ out/Debug/lua_pictures -q -r $SKP_DIR -l tools/lua/ngrams.lua > /tmp/lua-output
6-- $ lua tools/lua/ngrams_aggregate.lua
7
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.
14-- CHANGEME
15local n = 3
16-- CHANGEME
17
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
66