OLD | NEW |
(Empty) | |
| 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-ou
tput && lua tools/lua/ngrams_aggregate.lua |
| 6 |
| 7 -- To run on Cluster Telemetry, copy and paste the contents of this file into |
| 8 -- the box at https://skia-tree-status.appspot.com/skia-telemetry/lua_script, |
| 9 -- and paste the contents of ngrams_aggregate.lua into the "aggregator script" |
| 10 -- box on the same page. |
| 11 |
| 12 -- Change n as desired. |
| 13 local n = 3 |
| 14 |
| 15 -- This algorithm uses a list-of-lists for each SKP. For API call, append a |
| 16 -- list containing just the verb to the master list. Then, backtrack over the |
| 17 -- last (n-1) sublists in the master list and append the verb to those |
| 18 -- sublists. At the end of execution, the master list contains a sublist for |
| 19 -- every verb in the SKP file. Each sublist has length n, with the exception of |
| 20 -- the last n-1 sublists, which are discarded in the summarize() function, |
| 21 -- which generates counts for each n-gram. |
| 22 |
| 23 local ngrams = {} |
| 24 local currentFile = "" |
| 25 |
| 26 function sk_scrape_startcanvas(c, fileName) |
| 27 currentFile = fileName |
| 28 ngrams[currentFile] = {} |
| 29 end |
| 30 |
| 31 function sk_scrape_endcanvas(c, fileName) |
| 32 end |
| 33 |
| 34 function sk_scrape_accumulate(t) |
| 35 table.insert(ngrams[currentFile], {t.verb}) |
| 36 for i = 1, n-1 do |
| 37 local idx = #ngrams[currentFile] - i |
| 38 if idx > 0 then |
| 39 table.insert(ngrams[currentFile][idx], t.verb) |
| 40 end |
| 41 end |
| 42 end |
| 43 |
| 44 function sk_scrape_summarize() |
| 45 -- Count the n-grams. |
| 46 local counts = {} |
| 47 for file, ngramsInFile in pairs(ngrams) do |
| 48 for i = 1, #ngramsInFile - (n-1) do |
| 49 local ngram = table.concat(ngramsInFile[i], " ") |
| 50 if counts[ngram] == nil then |
| 51 counts[ngram] = 1 |
| 52 else |
| 53 counts[ngram] = counts[ngram] + 1 |
| 54 end |
| 55 end |
| 56 end |
| 57 |
| 58 -- Write out code for aggregating. |
| 59 for ngram, count in pairs(counts) do |
| 60 io.write("if counts['", ngram, "'] == nil then counts['", ngram, "'] = ", co
unt, " else counts['", ngram, "'] = counts['", ngram, "'] + ", count, " end\n") |
| 61 end |
| 62 end |
OLD | NEW |