]>
git.proxmox.com Git - rustc.git/blob - vendor/similar/src/text/utils.rs
1 use std
::collections
::HashMap
;
4 use super::DiffableStrRef
;
6 // quick and dirty way to get an upper sequence ratio.
7 pub fn upper_seq_ratio
<T
: PartialEq
>(seq1
: &[T
], seq2
: &[T
]) -> f32 {
8 let n
= seq1
.len() + seq2
.len();
12 2.0 * seq1
.len().min(seq2
.len()) as f32 / n
as f32
16 /// Internal utility to calculate an upper bound for a ratio for
17 /// [`get_close_matches`]. This is based on Python's difflib approach
18 /// of considering the two sets to be multisets.
20 /// It counts the number of matches without regard to order, which is an
21 /// obvious upper bound.
22 pub struct QuickSeqRatio
<'a
, T
: DiffableStrRef
+ ?Sized
>(HashMap
<&'a T
, i32>);
24 impl<'a
, T
: DiffableStrRef
+ Hash
+ Eq
+ ?Sized
> QuickSeqRatio
<'a
, T
> {
25 pub fn new(seq
: &[&'a T
]) -> QuickSeqRatio
<'a
, T
> {
26 let mut counts
= HashMap
::new();
28 *counts
.entry(word
).or_insert(0) += 1;
33 pub fn calc(&self, seq
: &[&T
]) -> f32 {
34 let n
= self.0.len() + seq
.len();
39 let mut available
= HashMap
::new();
42 let x
= if let Some(count
) = available
.get(&word
) {
45 self.0.get(&word
).copied().unwrap_or(0)
47 available
.insert(word
, x
- 1);
53 2.0 * matches
as f32 / n
as f32