1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use std::collections::{BTreeMap, HashMap};
use std::io::Read;
use thiserror::Error;
#[cfg(test)]
#[macro_use]
extern crate parameterized;
pub struct MostFrequentLineByLine {
content: String,
}
impl MostFrequentLineByLine {
pub fn try_new<R: Read>(source: &mut R) -> Result<MostFrequentLineByLine, Error> {
let mut buffer = String::new();
source
.read_to_string(&mut buffer)
.map_err(|err| Error::Io(err))?;
Ok(MostFrequentLineByLine {
content: buffer.to_owned(),
})
}
}
#[derive(Debug, Error)]
pub enum Error {
#[error("I/O error ({0})")]
Io(std::io::Error),
#[error("The file doesn't contain any of the expected delimiters.")]
NoDelimiterFound,
}
trait IsOneOf<'v, V: 'v> {
fn is_one_of<I: Iterator<Item = &'v V>>(&self, iter: I) -> bool;
}
impl<'v, V: PartialEq + 'v> IsOneOf<'v, V> for V {
fn is_one_of<I: Iterator<Item = &'v V>>(&self, iter: I) -> bool {
iter.filter(|f| *f == self).next().is_some()
}
}
pub trait GuessSeparator {
fn guess<'a>(&self, separators: impl Iterator<Item = &'a char> + Clone) -> Result<char, Error>;
}
impl GuessSeparator for MostFrequentLineByLine {
fn guess<'a>(&self, separators: impl Iterator<Item = &'a char> + Clone) -> Result<char, Error> {
self.content
.lines()
.flat_map(|line| {
line.chars()
.filter(|item| item.is_one_of(separators.clone()))
.fold(HashMap::<char, usize>::new(), |mut acc, char| {
*acc.entry(char).or_default() += 1;
acc
})
.iter()
.max_by(|lhs, rhs| lhs.1.cmp(&rhs.1))
.map(|(char, _)| *char)
})
.fold(BTreeMap::<char, usize>::new(), |mut acc, char| {
*acc.entry(char).or_default() += 1;
acc
})
.iter()
.max_by(|lhs, rhs| lhs.1.cmp(&rhs.1))
.ok_or_else(|| Error::NoDelimiterFound)
.map(|(char, _)| *char)
}
}
#[cfg(test)]
mod tests {
use crate::{GuessSeparator, MostFrequentLineByLine};
ide!();
#[parameterized(
text = {
"a;a",
"a;a\na;a",
"a;b;c,c",
"a;b;c\na;a,b,c\na;b;c\na;b;c\n",
"a,b,c;d\na,b,c;d\na,b,c;d",
},
expected = {
';',
';',
';',
';',
',',
}
)]
fn positive_well_formed_guesses(text: &str, expected: char) {
let mut source = text.as_bytes();
let guesser = MostFrequentLineByLine::try_new(&mut source).unwrap();
let guess = guesser.guess([';', ','].iter()).unwrap();
assert_eq!(guess, expected);
}
#[parameterized(
text = {
"a,a\na;a",
"a,a\na;a",
"a;a\na,a",
"a;a\na,a",
},
seps = {
&[';', ','],
&[',', ';'],
&[';', ','],
&[',', ';'],
},
expected = {
';',
';',
';',
';',
}
)]
fn equal_counts_have_char_ordering(text: &str, seps: &[char], expected: char) {
let mut source = text.as_bytes();
let guesser = MostFrequentLineByLine::try_new(&mut source).unwrap();
let guess = guesser.guess(seps.iter()).unwrap();
assert_eq!(guess, expected);
}
}