similarity-trait

Similarity trait Rust crate: compare input values, such as two or more items, then return an output value, such as a mesure of similarity, or correlation, or overlap.

https://github.com/sixarm/similarity-trait-rust-crate

Science Score: 54.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
    Links to: ncbi.nlm.nih.gov
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (7.9%) to scientific vocabulary
Last synced: 6 months ago · JSON representation ·

Repository

Similarity trait Rust crate: compare input values, such as two or more items, then return an output value, such as a mesure of similarity, or correlation, or overlap.

Basic Info
  • Host: GitHub
  • Owner: SixArm
  • License: other
  • Language: Rust
  • Default Branch: main
  • Size: 86.9 KB
Statistics
  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • Open Issues: 1
  • Releases: 0
Created 8 months ago · Last pushed 8 months ago
Metadata Files
Readme License Code of conduct Citation Codeowners

README.md

Similarity trait Rust crate

documentationsourcellms.txtcrateemail

The Similarity trait defines one function with one input and one output, so you can compare any kinds of input values and return any kind of output value.

rust pub trait Similarity<InputType, OutputType> { fn similarity(input: InputType) -> OutputType; }

This trait is purposefully very generic so you can use it as you wish.

We use this trait in our programs to create multiple kinds of similarity functionality, such as for trying various similarity algorithms that we want to use with the same input type and same output type.

For examples, please see the directory [examples].

Similarity of a pair

One way to use this trait is to calculate the similarity of a pair of values, such as two numbers, or two strings, or two images.

This is sometimes known as pairwise similarity or pair matching.

Example: given two numbers, then return the percent change.

```rust use similarity_trait::Similarity; struct MyStruct;

impl Similarity<(i32, i32), f64> for MyStruct { /// Similarity of numbers via percent change. fn similarity(input: (i32, i32)) -> f64 { (100.0 * (input.1 - input.0) as f64) / i32::abs(input.0) as f64 } }

let percentchange = MyStruct::similarity((100, 120)); asserteq!(percent_change, 20.0); ```

Similarity of a collection

One way to use this trait is to calculate the similarity of a collection of values, such as an array of numbers, or vector of strings, or set of images.

This is sometimes called intra-group similarity or statistical correlation.

Example: given numbers, then return the population standard deviation.

```rust use similarity_trait::Similarity; struct MyStruct;

impl Similarity<&Vec, Option> for MyStruct { /// Similarity of numbers via population standard deviation fn similarity(numbers: &Vec) -> Option { if numbers.is_empty() { return None } let mean = numbers.iter().sum::() / numbers.len() as f64; let variance = numbers.iter().map(|x| (x - mean).powi(2)).sum::() / numbers.len() as f64; Some(variance.sqrt()) } }

let numbers = vec![2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0]; let populationstandarddeviation = MyStruct::similarity(&numbers).expect("similarity"); assert!(populationstandarddeviation > 1.999 && populationstandarddeviation < 2.001); ```

For examples, please see the directory [examples].

Similarity of a pair or a collection

You may want to choose whether you prefer to calculate the similarity of a pair (such as two strings) or a collection (such as a vector of strings).

Example: given a pair of strings, then return the Hamming distance.

```rust use similarity_trait::Similarity; struct MyStruct;

impl Similarity<(&str, &str), usize> for MyStruct { /// Similarity of a pair of strings via Hamming distance. fn similarity(pair: (&str, &str)) -> usize { pair.0.chars().zip(pair.1.chars()).filter(|(c1, c2)| c1 != c2).count() } }

let pair = ("information", "informatics"); let hammingdistance = MyStruct::similarity(pair); asserteq!(hamming_distance, 2); ```

Example: given a collection of strings, then return the maximum Hamming distance.

```rust use similarity_trait::Similarity; struct MyStruct;

impl Similarity, usize> for MyStruct { /// Similarity of a collection of strings via maximum Hamming distance. fn similarity(collection: Vec<&str>) -> usize { let mut max = 0; for i in 0..collection.len() { for j in (i + 1)..collection.len() { max = std::cmp::max(max, collection[i].chars().zip(collection[j].chars()).filter(|(c1, c2)| c1 != c2).count()) } } max } }

let collection = vec!["information", "informatics", "affirmation"]; let maximumhammingdistance = MyStruct::similarity(collection); asserteq!(maximumhamming_distance, 5); ```

How to learn more

Wikipedia links:

Similarity research papers about patient matching:

Owner

  • Name: SixArm
  • Login: SixArm
  • Kind: organization
  • Email: sixarm@sixarm.com
  • Location: San Francisco

SixArm Software

Citation (CITATION.cff)

cff-version: 1.2.0
title: Assertables: Rust crate of assert macros for testing
message: >-
  If you use this work and you want to cite it,
  then you can use the metadata from this file.
type: software
authors:
  - given-names: Joel Parker
    family-names: Henderson
    email: joel@joelparkerhenderson.com
    affiliation: joelparkerhenderson.com
    orcid: 'https://orcid.org/0009-0000-4681-282X'
identifiers:
  - type: url
    value: 'https://github.com/SixArm/assertables-rust-crate/'
    description: Assertables: Rust crate of assert macros for testing
repository-code: 'https://github.com/SixArm/assertables-rust-crate/'
abstract: >-
  Assertables: Rust crate of assert macros for testing
license: See license file

GitHub Events

Total
  • Issues event: 1
  • Watch event: 1
  • Push event: 2
  • Create event: 2
Last Year
  • Issues event: 1
  • Watch event: 1
  • Push event: 2
  • Create event: 2

Issues and Pull Requests

Last synced: 6 months ago

Packages

  • Total packages: 1
  • Total downloads:
    • cargo 325 total
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 1
  • Total maintainers: 1
crates.io: similarity-trait

Similarity trait Rust crate: compare input values, such as two or more items, then return an output value, such as a mesure of similarity, or correlation, or overlap.

  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 325 Total
Rankings
Dependent repos count: 21.1%
Dependent packages count: 28.0%
Average: 48.0%
Downloads: 94.7%
Maintainers (1)
Last synced: 6 months ago

Dependencies

Cargo.lock cargo
Cargo.toml cargo