<commit_before>/*
 * Copyright (C) 2011 Collabora Ltd.
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       <NAME> <<EMAIL>>
 */

using Gee;

/**
 * Likely-ness of a potential match.
 *
 * Note that the order should be maintained.
 *
 * @since 0.5.0
 */
public enum Folks.MatchResult
{
  /**
   * Zero likelihood of a match.
   *
   * This is used in situations where two individuals should never be linked,
   * such as when one of them has a {@link Individual.trust_level} of
   * {@link TrustLevel.NONE}.
   *
   * @since UNRELEASED
   */
  NONE = -1,

  /**
   * Very low likelihood of a match.
   */
  VERY_LOW = 0,

  /**
   * Low likelihood of a match.
   */
  LOW = 1,

  /**
   * Medium likelihood of a match.
   */
  MEDIUM = 2,

  /**
   * High likelihood of a match.
   */
  HIGH = 3,

  /**
   * Very high likelihood of a match.
   */
  VERY_HIGH = 4,

  /**
   * Minimum likelihood of a match.
   */
  MIN = NONE,

  /**
   * Maximum likelihood of a match.
   */
  MAX = VERY_HIGH
}

/**
 * This class provides functionality to explore a potential match between
 * two individuals.
 *
 * @since 0.5.0
 */
public class Folks.PotentialMatch : Object
{
  MatchResult _result;
  private Folks.Individual _individual_a;
  private Folks.Individual _individual_b;

  /**
   * A set of e-mail addresses known to be aliases of each other, such as
   * various forms of administrator address.
   *
   * @since 0.5.1
   */
  public static Set<string> known_email_aliases =
      new Gee.HashSet<string> (str_hash, str_equal);

  private static double _DIST_THRESHOLD = 0.70;
  private const string _SEPARATORS = "._-+";

  static construct
    {
      PotentialMatch.known_email_aliases.add ("admin");
      PotentialMatch.known_email_aliases.add ("abuse");
      PotentialMatch.known_email_aliases.add ("webmaster");
    }

  /**
   * Whether two individuals are likely to be the same person.
   *
   * @param a an individual to compare
   * @param b another individual to compare
   *
   * @since 0.5.0
   */
  public MatchResult potential_match (Individual a, Individual b)
    {
      this._individual_a = a;
      this._individual_b = b;
      this._result = MatchResult.MIN;

      /* Immediately discount a match if either of the individuals can't be
       * trusted (e.g. due to containing link-local XMPP personas, which can be
       * spoofed). */
      if (a.trust_level == TrustLevel.NONE || b.trust_level == TrustLevel.NONE)
        {
          this._result = MatchResult.NONE;
          return this._result;
        }

      /* If individuals share gender. */
      if (this._individual_a.gender != Gender.UNSPECIFIED &&
          this._individual_b.gender != Gender.UNSPECIFIED &&
          this._individual_a.gender != this._individual_b.gender)
        {
          return this._result;
        }

      /* If individuals share common im-addresses */
      this._inspect_im_addresses ();
      if (this._result == MatchResult.MAX)
        return this._result;

      /* If individuals share common e-mails */
      this._inspect_emails ();
      if (this._result == MatchResult.MAX)
        return this._result;

      /* If individuals share common phone numbers */
      this._inspect_phone_numbers ();
      if (this._result == MatchResult.MAX)
        return this._result;<commit_msg>tests: Fix potential match tests in Tracker backend<commit_after>/*
 * Copyright (C) 2011 Collabora Ltd.
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       <NAME> <<EMAIL>>
 */

using Gee;

/**
 * Likely-ness of a potential match.
 *
 * Note that the order should be maintained.
 *
 * @since 0.5.0
 */
public enum Folks.MatchResult
{
  /**
   * Zero likelihood of a match.
   *
   * This is used in situations where two individuals should never be linked,
   * such as when one of them has a {@link Individual.trust_level} of
   * {@link TrustLevel.NONE}.
   *
   * @since UNRELEASED
   */
  NONE = -1,

  /**
   * Very low likelihood of a match.
   */
  VERY_LOW = 0,

  /**
   * Low likelihood of a match.
   */
  LOW = 1,

  /**
   * Medium likelihood of a match.
   */
  MEDIUM = 2,

  /**
   * High likelihood of a match.
   */
  HIGH = 3,

  /**
   * Very high likelihood of a match.
   */
  VERY_HIGH = 4,

  /**
   * Minimum likelihood of a match.
   */
  MIN = NONE,

  /**
   * Maximum likelihood of a match.
   */
  MAX = VERY_HIGH
}

/**
 * This class provides functionality to explore a potential match between
 * two individuals.
 *
 * @since 0.5.0
 */
public class Folks.PotentialMatch : Object
{
  MatchResult _result;
  private Folks.Individual _individual_a;
  private Folks.Individual _individual_b;

  /**
   * A set of e-mail addresses known to be aliases of each other, such as
   * various forms of administrator address.
   *
   * @since 0.5.1
   */
  public static Set<string> known_email_aliases =
      new Gee.HashSet<string> (str_hash, str_equal);

  private static double _DIST_THRESHOLD = 0.70;
  private const string _SEPARATORS = "._-+";

  static construct
    {
      PotentialMatch.known_email_aliases.add ("admin");
      PotentialMatch.known_email_aliases.add ("abuse");
      PotentialMatch.known_email_aliases.add ("webmaster");
    }

  /**
   * Whether two individuals are likely to be the same person.
   *
   * @param a an individual to compare
   * @param b another individual to compare
   *
   * @since 0.5.0
   */
  public MatchResult potential_match (Individual a, Individual b)
    {
      this._individual_a = a;
      this._individual_b = b;
      this._result = MatchResult.MIN;

      /* Immediately discount a match if either of the individuals can't be
       * trusted (e.g. due to containing link-local XMPP personas, which can be
       * spoofed). */
      if (a.trust_level == TrustLevel.NONE || b.trust_level == TrustLevel.NONE)
        {
          return this._result;
        }

      this._result = MatchResult.VERY_LOW;

      /* If individuals share gender. */
      if (this._individual_a.gender != Gender.UNSPECIFIED &&
          this._individual_b.gender != Gender.UNSPECIFIED &&
          this._individual_a.gender != this._individual_b.gender)
        {
          return this._result;
        }

      /* If individuals share common im-addresses */
      this._inspect_im_addresses ();
      if (this._result == MatchResult.MAX)
        return this._result;

      /* If individuals share common e-mails */
      this._inspect_emails ();
      if (this._result == MatchResult.MAX)
        return this._result;

      /* If individuals share common phone numbers */
      this._inspect_phone_numbers ();
      if (this._result == MatchResult.MAX)
        return this._result;
{'fraction_non_alphanumeric': 0.09882915774896135, 'fraction_numerical': 0.006798438876998615, 'mean_word_length': 2.525965379494008, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7526827', 'n_tokens_mistral': 2479, 'n_tokens_neox': 2327, 'n_words': 812}