|
| 1 | +class AutoTaggingService |
| 2 | + # Default rules used to seed tag_rules when no rules exist in the database. |
| 3 | + # Format: { tag: "tag name", rules: [{ pattern: "text", match_type: "keyword"|"phrase" }] } |
| 4 | + DEFAULT_RULES = [ |
| 5 | + { tag: "parking", rules: [ { pattern: "parking", match_type: "keyword" } ] }, |
| 6 | + { tag: "taxes", rules: [ { pattern: "tax abate", match_type: "phrase" }, |
| 7 | + { pattern: "property tax", match_type: "phrase" }, |
| 8 | + { pattern: "payroll tax", match_type: "phrase" }, |
| 9 | + { pattern: "real estate tax", match_type: "phrase" } ] }, |
| 10 | + { tag: "payroll", rules: [ { pattern: "payroll", match_type: "keyword" }, |
| 11 | + { pattern: "salary", match_type: "keyword" }, |
| 12 | + { pattern: "salaries", match_type: "keyword" } ] }, |
| 13 | + { tag: "fees", rules: [ { pattern: "fee schedule", match_type: "phrase" } ] }, |
| 14 | + { tag: "street safety", rules: [ { pattern: "vehicles and traffic", match_type: "phrase" } ] }, |
| 15 | + { tag: "bike lanes", rules: [ { pattern: "bicycle", match_type: "keyword" }, |
| 16 | + { pattern: "bike", match_type: "keyword" }, |
| 17 | + { pattern: "greenway", match_type: "keyword" }, |
| 18 | + { pattern: "scooter", match_type: "phrase" } ] }, |
| 19 | + { tag: "environmental", rules: [ { pattern: "soil remed", match_type: "phrase" }, |
| 20 | + { pattern: "chromate", match_type: "keyword" }, |
| 21 | + { pattern: "environmental protection", match_type: "phrase" }, |
| 22 | + { pattern: "contaminat", match_type: "phrase" }, |
| 23 | + { pattern: "flood hazard", match_type: "phrase" }, |
| 24 | + { pattern: "underground storage tank", match_type: "phrase" } ] }, |
| 25 | + { tag: "building", rules: [ { pattern: "redevelopment plan", match_type: "phrase" }, |
| 26 | + { pattern: "land development", match_type: "phrase" } ] }, |
| 27 | + { tag: "leases", rules: [ { pattern: "lease", match_type: "keyword" } ] }, |
| 28 | + { tag: "Infrastructure", rules: [ { pattern: "department of infrastructure", match_type: "phrase" }, |
| 29 | + { pattern: "division of engineering", match_type: "phrase" } ] }, |
| 30 | + { tag: "budget", rules: [ { pattern: "budget", match_type: "keyword" }, |
| 31 | + { pattern: "appropriation", match_type: "phrase" } ] }, |
| 32 | + { tag: "cannabis", rules: [ { pattern: "cannabis", match_type: "keyword" } ] }, |
| 33 | + { tag: "parks", rules: [ { pattern: "park maintenance", match_type: "phrase" }, |
| 34 | + { pattern: "recreation and youth", match_type: "phrase" }, |
| 35 | + { pattern: "pershing field", match_type: "phrase" }, |
| 36 | + { pattern: "liberty state park", match_type: "phrase" } ] }, |
| 37 | + { tag: "housing", rules: [ { pattern: "affordable housing", match_type: "phrase" }, |
| 38 | + { pattern: "housing trust", match_type: "phrase" } ] }, |
| 39 | + { tag: "appointments", rules: [ { pattern: "appointing", match_type: "keyword" }, |
| 40 | + { pattern: "appointment of", match_type: "phrase" } ] }, |
| 41 | + { tag: "claims", rules: [ { pattern: "payment of a claim", match_type: "phrase" }, |
| 42 | + { pattern: "settlement of the action", match_type: "phrase" } ] }, |
| 43 | + { tag: "grants", rules: [ { pattern: "community development block grant", match_type: "phrase" }, |
| 44 | + { pattern: "grant fund", match_type: "phrase" } ] }, |
| 45 | + { tag: "public safety", rules: [ { pattern: "department of public safety", match_type: "phrase" }, |
| 46 | + { pattern: "division of fire", match_type: "phrase" }, |
| 47 | + { pattern: "division of police", match_type: "phrase" } ] }, |
| 48 | + { tag: "senior services", rules: [ { pattern: "senior citizen", match_type: "phrase" }, |
| 49 | + { pattern: "senior center", match_type: "phrase" }, |
| 50 | + { pattern: "senior meal", match_type: "phrase" }, |
| 51 | + { pattern: "senior lunch", match_type: "phrase" }, |
| 52 | + { pattern: "senior affair", match_type: "phrase" }, |
| 53 | + { pattern: "congregate", match_type: "phrase" } ] }, |
| 54 | + { tag: "water", rules: [ { pattern: "water agreement", match_type: "phrase" }, |
| 55 | + { pattern: "jcmua", match_type: "keyword" } ] } |
| 56 | + ].freeze |
| 57 | + |
| 58 | + def initialize(agenda_items) |
| 59 | + @agenda_items = Array(agenda_items) |
| 60 | + end |
| 61 | + |
| 62 | + def call |
| 63 | + return if @agenda_items.empty? |
| 64 | + |
| 65 | + load_rules |
| 66 | + @agenda_items.each { |item| tag_item(item) } |
| 67 | + end |
| 68 | + |
| 69 | + # Seeds default rules for tags that have no rules yet. |
| 70 | + # Safe to call multiple times — only adds rules to tags that have none. |
| 71 | + def self.seed_default_rules! |
| 72 | + DEFAULT_RULES.each do |entry| |
| 73 | + tag = Tag.where("LOWER(name) = ?", entry[:tag].downcase).first_or_create!(name: entry[:tag]) |
| 74 | + next if tag.tag_rules.any? |
| 75 | + |
| 76 | + entry[:rules].each do |rule| |
| 77 | + tag.tag_rules.create!(pattern: rule[:pattern], match_type: rule[:match_type]) |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + private |
| 83 | + |
| 84 | + def load_rules |
| 85 | + @rules = Tag.joins(:tag_rules) |
| 86 | + .includes(:tag_rules) |
| 87 | + .distinct |
| 88 | + .map do |tag| |
| 89 | + regexes = tag.tag_rules.map(&:to_regex) |
| 90 | + { tag: tag, regexes: regexes } |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + def tag_item(item) |
| 95 | + title = item.title.to_s |
| 96 | + |
| 97 | + @rules.each do |rule| |
| 98 | + if rule[:regexes].any? { |pat| pat.match?(title) } |
| 99 | + item.agenda_item_tags.find_or_create_by!(tag: rule[:tag]) |
| 100 | + end |
| 101 | + end |
| 102 | + end |
| 103 | +end |
0 commit comments