borenet | 93558dc | 2015-06-25 07:47:40 -0700 | [diff] [blame] | 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 |
borenet | 8cd8f94 | 2015-06-29 12:54:25 -0700 | [diff] [blame] | 5 | -- $ out/Debug/lua_pictures -q -r $SKP_DIR -l tools/lua/ngrams.lua > /tmp/lua-output |
| 6 | -- $ lua tools/lua/ngrams_aggregate.lua |
borenet | 93558dc | 2015-06-25 07:47:40 -0700 | [diff] [blame] | 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. |
borenet | 3691e66 | 2015-06-30 10:24:03 -0700 | [diff] [blame] | 14 | -- CHANGEME |
borenet | 93558dc | 2015-06-25 07:47:40 -0700 | [diff] [blame] | 15 | local n = 3 |
borenet | 3691e66 | 2015-06-30 10:24:03 -0700 | [diff] [blame] | 16 | -- CHANGEME |
borenet | 93558dc | 2015-06-25 07:47:40 -0700 | [diff] [blame] | 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 | |
| 26 | local ngrams = {} |
| 27 | local currentFile = "" |
| 28 | |
| 29 | function sk_scrape_startcanvas(c, fileName) |
| 30 | currentFile = fileName |
| 31 | ngrams[currentFile] = {} |
| 32 | end |
| 33 | |
| 34 | function sk_scrape_endcanvas(c, fileName) |
| 35 | end |
| 36 | |
| 37 | function 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 |
| 45 | end |
| 46 | |
| 47 | function 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 |
| 65 | end |