annotate mod_report_affiliations/traits.lib.lua @ 6513:5fb466693e85

mod_storage_xmlarchive: Ensure list index files are removed using os.remove() on the list files leaves the new index files behind since util.datamanager does not know they are removed. Thanks Link Mauve
author Kim Alvefur <zash@zash.se>
date Tue, 07 Apr 2026 21:10:54 +0200
parents e905ef16efb7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6030
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local known_traits = {};
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local function trait_added(event)
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local trait = event.item;
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local name = trait.name;
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 if known_traits[name] then return; end
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 known_traits[name] = trait.probabilities;
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 end
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local function trait_removed(event)
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local trait = event.item;
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 known_traits[trait.name] = nil;
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 end
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 module:handle_items("account-trait", trait_added, trait_removed);
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local function bayes_probability(prior, prob_given_true, prob_given_false)
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local numerator = prob_given_true * prior;
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local denominator = numerator + prob_given_false * (1 - prior);
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 return numerator / denominator;
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local function prob_is_bad(traits, prior)
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 prior = prior or 0.50;
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 for trait, state in pairs(traits) do
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local probabilities = known_traits[trait];
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 if probabilities then
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 if state then
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 prior = bayes_probability(
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 prior,
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 probabilities.prob_bad_true,
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 probabilities.prob_bad_false
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 );
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 else
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 prior = bayes_probability(
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 prior,
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 1 - probabilities.prob_bad_true,
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 1 - probabilities.prob_bad_false
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 );
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 return prior;
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local function get_probability_bad(username, prior)
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 local user_traits = {};
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 module:fire_event("get-account-traits", { username = username, host = module.host, traits = user_traits });
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local result = prob_is_bad(user_traits, prior);
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 return result;
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 return {
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 get_probability_bad = get_probability_bad;
e905ef16efb7 mod_report_affiliations: New module for XEP-0489: Reporting Account Affiliations
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 };