ngrams.lua 2.1 KB

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