|
| 1 | +# Domain tagger demo v3 |
| 2 | +# - noun extraction (no lookup table) |
| 3 | +# - block-unique signal: block domains minus sub-block domains |
| 4 | +# - language stopwords: extracted via Stopwords.find_stopwords on a synthetic |
| 5 | +# Elixir keywords file — infra words that appear there are subtracted |
| 6 | +# |
| 7 | +# run with: mix run scripts/domain_tagger_demo.exs |
| 8 | + |
| 9 | +alias CodeQA.Metrics.{BlockAnalyzer, BlockDetector, TokenNormalizer} |
| 10 | +alias CodeQA.Stopwords |
| 11 | + |
| 12 | +defmodule DomainTagger do |
| 13 | + @verbs MapSet.new(~w[ |
| 14 | + get fetch find list search query count |
| 15 | + create build make add insert |
| 16 | + update patch put set save store upsert |
| 17 | + delete remove destroy drop clear purge flush |
| 18 | + send receive deliver emit broadcast publish |
| 19 | + upload download read write stream parse format render print log |
| 20 | + check validate verify assert ensure guard |
| 21 | + handle process compute calculate apply run exec call invoke dispatch |
| 22 | + init start stop reset open close connect disconnect reload |
| 23 | + is has can will do did should would may |
| 24 | + by with for from to into on at of in out new |
| 25 | + all some many each every any |
| 26 | + a an the ok error nil true false |
| 27 | + ]) |
| 28 | + |
| 29 | + def split(content) do |
| 30 | + content |
| 31 | + |> String.replace(~r/([a-z])([A-Z])/, "\\1_\\2") |
| 32 | + |> String.replace(~r/([A-Z]+)([A-Z][a-z])/, "\\1_\\2") |
| 33 | + |> String.split(~r/[_!?]/, trim: true) |
| 34 | + |> Enum.map(&String.downcase/1) |
| 35 | + |> Enum.reject(&(String.length(&1) <= 1)) |
| 36 | + end |
| 37 | + |
| 38 | + def nouns(content) do |
| 39 | + content |> split() |> Enum.reject(&MapSet.member?(@verbs, &1)) |
| 40 | + end |
| 41 | + |
| 42 | + def tag(tokens) do |
| 43 | + bound = BlockAnalyzer.bound_variables(tokens) |
| 44 | + |
| 45 | + tokens |
| 46 | + |> Enum.filter(&(&1.value == "<ID>")) |
| 47 | + |> Enum.flat_map(&nouns(&1.content)) |
| 48 | + |> Enum.reject(fn noun -> MapSet.member?(bound, noun) end) |
| 49 | + |> Enum.frequencies() |
| 50 | + |> Enum.sort_by(fn {_, c} -> -c end) |
| 51 | + |> Enum.map(fn {word, _} -> String.to_atom(word) end) |
| 52 | + end |
| 53 | +end |
| 54 | + |
| 55 | +# --------------------------------------------------------------------------- |
| 56 | +# Step 1: derive language stopwords from the repository's own .ex files. |
| 57 | +# |
| 58 | +# Stopwords.find_stopwords counts how often each noun appears across files. |
| 59 | +# Nouns present in ≥15% of repo files are treated as infrastructure words. |
| 60 | +# --------------------------------------------------------------------------- |
| 61 | + |
| 62 | +repo_files = |
| 63 | + Path.wildcard("lib/**/*.ex") |
| 64 | + |> Map.new(fn path -> {path, File.read!(path)} end) |
| 65 | + |
| 66 | +noun_extractor = fn content -> |
| 67 | + content |
| 68 | + |> TokenNormalizer.normalize_structural() |
| 69 | + |> DomainTagger.tag() |
| 70 | + |> Enum.map(&Atom.to_string/1) |
| 71 | +end |
| 72 | + |
| 73 | +IO.puts("=== Deriving stopwords from #{map_size(repo_files)} repo files ===") |
| 74 | + |
| 75 | +lang_stopwords = |
| 76 | + Stopwords.find_stopwords(repo_files, noun_extractor) |
| 77 | + |
| 78 | +IO.puts("=== Language stopwords extracted (#{MapSet.size(lang_stopwords)} terms) ===") |
| 79 | +IO.puts(inspect(lang_stopwords |> MapSet.to_list() |> Enum.sort())) |
| 80 | +IO.puts("") |
| 81 | + |
| 82 | +# --------------------------------------------------------------------------- |
| 83 | +# Step 2: analyze all .ex files in the repo |
| 84 | +# --------------------------------------------------------------------------- |
| 85 | + |
| 86 | +target = "lib/codeqa/git.ex" |
| 87 | +sources = [{target, File.read!(target)}] |
| 88 | + |
| 89 | +apply_stopwords = fn domains -> |
| 90 | + domains |
| 91 | + |> Enum.map(&Atom.to_string/1) |
| 92 | + |> Enum.reject(&MapSet.member?(lang_stopwords, &1)) |
| 93 | + |> Enum.map(&String.to_atom/1) |
| 94 | +end |
| 95 | + |
| 96 | +fn_name_of = fn block -> |
| 97 | + block.tokens |
| 98 | + |> Enum.drop_while(&(&1.value != "<ID>" or &1.content not in ["def", "defp"])) |
| 99 | + |> Enum.drop(1) |
| 100 | + |> Enum.find(&(&1.value == "<ID>")) |
| 101 | + |> case do |
| 102 | + nil -> "(unknown)" |
| 103 | + t -> t.content |
| 104 | + end |
| 105 | +end |
| 106 | + |
| 107 | +Enum.each(sources, fn {source_label, content} -> |
| 108 | + IO.puts("=== #{source_label} ===\n") |
| 109 | + |
| 110 | + tokens = TokenNormalizer.normalize_structural(content) |
| 111 | + blocks = BlockDetector.detect_blocks(tokens, language: :unknown) |
| 112 | + |
| 113 | + Enum.each(blocks, fn block -> |
| 114 | + all_domains = DomainTagger.tag(block.tokens) |> apply_stopwords.() |
| 115 | + |
| 116 | + sub_domain_set = |
| 117 | + block.sub_blocks |
| 118 | + |> Enum.flat_map(&DomainTagger.tag(&1.tokens)) |
| 119 | + |> Enum.map(&Atom.to_string/1) |
| 120 | + |> Enum.reject(&MapSet.member?(lang_stopwords, &1)) |
| 121 | + |> MapSet.new() |
| 122 | + |
| 123 | + unique = Enum.reject(all_domains, &MapSet.member?(sub_domain_set, Atom.to_string(&1))) |
| 124 | + |
| 125 | + IO.puts("#{fn_name_of.(block)}/? (lines #{block.start_line}–#{block.end_line})") |
| 126 | + IO.puts(" all : #{inspect(all_domains)}") |
| 127 | + IO.puts(" unique : #{inspect(unique)}") |
| 128 | + Enum.each(block.sub_blocks, fn sb -> |
| 129 | + ds = DomainTagger.tag(sb.tokens) |> apply_stopwords.() |
| 130 | + unless Enum.empty?(ds), do: IO.puts(" sub:#{sb.start_line} : #{inspect(ds)}") |
| 131 | + end) |
| 132 | + IO.puts("") |
| 133 | + end) |
| 134 | +end) |
0 commit comments