CrucibleAdversary.Perturbations.Character (CrucibleAdversary v0.4.0)

View Source

Character-level perturbation attacks for adversarial text generation.

Implements:

  • Character swapping (typos)
  • Character deletion
  • Character insertion
  • Homoglyph substitution
  • Keyboard-based typo injection

Examples

iex> alias CrucibleAdversary.Perturbations.Character
iex> {:ok, result} = Character.swap("hello", rate: 0.2, seed: 42)
iex> result.attack_type
:character_swap

Summary

Functions

Deletes random characters from text.

Substitutes characters with visually similar Unicode characters (homoglyphs).

Inserts random characters into text.

Injects realistic typos based on keyboard layout.

Randomly swaps adjacent characters to simulate typos.

Functions

delete(text, opts \\ [])

@spec delete(
  String.t(),
  keyword()
) :: {:ok, CrucibleAdversary.AttackResult.t()} | {:error, term()}

Deletes random characters from text.

Options

  • :rate - Float, percentage of characters to delete (default: 0.1)
  • :preserve_spaces - Boolean, whether to preserve space characters (default: true)
  • :seed - Integer, random seed for reproducibility (default: nil)

Returns

  • {:ok, %AttackResult{}} - Success with attack result
  • {:error, reason} - Error with reason

Examples

iex> alias CrucibleAdversary.Perturbations.Character
iex> {:ok, result} = Character.delete("hello world", rate: 0.2, seed: 42)
iex> result.attack_type
:character_delete

homoglyph(text, opts \\ [])

@spec homoglyph(
  String.t(),
  keyword()
) :: {:ok, CrucibleAdversary.AttackResult.t()} | {:error, term()}

Substitutes characters with visually similar Unicode characters (homoglyphs).

Options

  • :rate - Float, percentage of characters to substitute (default: 0.1)
  • :charset - Atom, character set to use (:cyrillic, :greek, :all) (default: :all)
  • :seed - Integer, random seed for reproducibility (default: nil)

Returns

  • {:ok, %AttackResult{}} - Success with attack result
  • {:error, reason} - Error with reason

Examples

iex> alias CrucibleAdversary.Perturbations.Character
iex> {:ok, result} = Character.homoglyph("administrator", charset: :cyrillic, seed: 42)
iex> result.attack_type
:homoglyph

insert(text, opts \\ [])

@spec insert(
  String.t(),
  keyword()
) :: {:ok, CrucibleAdversary.AttackResult.t()} | {:error, term()}

Inserts random characters into text.

Options

  • :rate - Float, percentage of insertion positions (default: 0.1)
  • :char_pool - List of characters to insert from (default: a-z)
  • :seed - Integer, random seed for reproducibility (default: nil)

Returns

  • {:ok, %AttackResult{}} - Success with attack result
  • {:error, reason} - Error with reason

Examples

iex> alias CrucibleAdversary.Perturbations.Character
iex> {:ok, result} = Character.insert("test", rate: 0.2, seed: 42)
iex> result.attack_type
:character_insert

keyboard_typo(text, opts \\ [])

@spec keyboard_typo(
  String.t(),
  keyword()
) :: {:ok, CrucibleAdversary.AttackResult.t()} | {:error, term()}

Injects realistic typos based on keyboard layout.

Options

  • :rate - Float, percentage of typo injection (default: 0.1)
  • :layout - Atom, keyboard layout (:qwerty, :dvorak) (default: :qwerty)
  • :typo_types - List of atoms, types to include [:substitution, :insertion, :deletion, :transposition] (default: [:substitution])
  • :seed - Integer, random seed for reproducibility (default: nil)

Returns

  • {:ok, %AttackResult{}} - Success with attack result
  • {:error, reason} - Error with reason

Examples

iex> alias CrucibleAdversary.Perturbations.Character
iex> {:ok, result} = Character.keyboard_typo("hello", layout: :qwerty, seed: 42)
iex> result.attack_type
:keyboard_typo

swap(text, opts \\ [])

@spec swap(
  String.t(),
  keyword()
) :: {:ok, CrucibleAdversary.AttackResult.t()} | {:error, term()}

Randomly swaps adjacent characters to simulate typos.

Options

  • :rate - Float between 0.0 and 1.0, percentage of characters to swap (default: 0.1)
  • :seed - Integer, random seed for reproducibility (default: nil)

Returns

  • {:ok, %AttackResult{}} - Success with attack result
  • {:error, reason} - Error with reason

Examples

iex> alias CrucibleAdversary.Perturbations.Character
iex> {:ok, result} = Character.swap("hello world", rate: 0.2, seed: 42)
iex> result.original
"hello world"
iex> result.attack_type
:character_swap

iex> Character.swap("test", rate: 1.5)
{:error, :invalid_rate}