drug_extraction_cli/
lib.rs

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
use color_eyre::{
    eyre::{eyre, Context, ContextCompat},
    Result,
};
use indicatif::{ProgressBar, ProgressIterator, ProgressStyle};

use std::{collections::HashSet, fs::File, path::Path, time::Duration};

use serde::{Deserialize, Serialize};

use itertools::Itertools;

/// Create a spinner with default style, takes a message
fn initialize_spinner_style(msg: String) -> ProgressBar {
    let pb = ProgressBar::new_spinner();
    pb.enable_steady_tick(Duration::from_millis(100));
    pb.with_style(
        ProgressStyle::with_template("{spinner:.blue} {msg}")
            .unwrap()
            .tick_strings(&[
                "▹▹▹▹▹",
                "▸▹▹▹▹",
                "▹▸▹▹▹",
                "▹▹▸▹▹",
                "▹▹▹▸▹",
                "▹▹▹▹▸",
                "▪▪▪▪▪",
            ]),
    )
    .with_message(msg)
}

/// Initialize a progress bar with default style, takes a message and length
fn initialize_progress_bar(msg: String, len: u64) -> ProgressBar {
    let pb = ProgressBar::new(len);
    pb.enable_steady_tick(Duration::from_millis(100));
    pb.with_style(
        ProgressStyle::default_bar()
            .template("{spinner:.blue} {msg} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos:>7}/{len:7} ({eta})").unwrap()
            .progress_chars("##-"),
    )
    .with_message(msg)
}

/// Struct to hold search term and metadata
#[derive(Deserialize, Debug, Clone, Default, PartialEq)]
pub struct SearchTerm {
    /// The search term
    pub term: String,
    /// Optional metadata to be included in output
    pub metadata: Option<String>,
}

/// Struct to hold search output
#[derive(Serialize, Debug, Clone, PartialEq)]
pub struct SearchOutput<'a> {
    /// The row id of the matched record, either from specified column or line number
    row_id: &'a str,
    /// The search term that was matched
    search_term: &'a str,
    /// The matched term from the record
    matched_term: &'a str,
    /// The number of edits required to match the search term
    edits: usize,
    /// The similarity score between the search term and the matched term
    similarity_score: f64,
    /// The field that was searched
    search_field: &'a str,
    /// The metadata associated with the search term
    metadata: &'a Option<String>,
}

/// Function to read in search terms from a csv file
/// Performs cleaning of terms, ignoring metadata column
pub fn read_terms_from_file<P: AsRef<Path>>(p: P) -> Result<Vec<SearchTerm>> {
    let mut rdr = csv::Reader::from_path(p).wrap_err("Unable to read search terms file")?;
    let mut records: Vec<SearchTerm> = Vec::new();
    for (i, row) in rdr
        .deserialize()
        .enumerate()
        .progress_with(initialize_spinner_style(
            "Loading Search Terms...".to_string(),
        ))
    {
        let mut record: SearchTerm =
            row.wrap_err(format!("Could not load search term from line: {}", i))?;
        record.term = clean_text(&record.term);
        records.push(record);
    }
    records.sort_by_key(|x| x.term.split_ascii_whitespace().count());
    Ok(records)
}

/// Function to remove non-alphanumeric characters from a string
/// keeps hyphens due to their usage in abbreviations/medical terms.
/// Also uppercase for standardization.
/// Example:
/// ```
/// use drug_extraction_cli::clean_text;
///
/// let s = "This is a test-string with 1234 and some punctuation!@#$%^&*()";
/// let cleaned = clean_text(s);
/// assert_ne!(cleaned, "THIS IS A TEST STRING WITH 1234 AND SOME PUNCTUATION");
/// assert_eq!(cleaned, "THIS IS A TEST-STRING WITH 1234 AND SOME PUNCTUATION");
/// ```
pub fn clean_text(s: &str) -> String {
    s.replace(|c: char| !c.is_ascii_alphanumeric() && c != '-', " ")
        .trim()
        .to_ascii_uppercase()
}

/// Struct to hold information about the dataset
#[derive(Debug)]
pub struct DataSet {
    /// csv reader for the dataset
    pub reader: csv::Reader<File>,
    /// rows in the dataset from first scan
    pub rows: usize,
    /// indices of the columns to search in the dataset
    pub clean_search_columns: Vec<ColumnInfo>,
    /// index of the column to use as an id
    pub clean_id_column: Option<ColumnInfo>,
    /// csv writer for the output file
    pub writer: csv::Writer<File>,
}

#[derive(Debug, Clone, Default, PartialEq)]
pub struct ColumnInfo {
    pub name: String,
    pub index: usize,
}

/// Function to get the column index for a given column name
/// Returns an error if the column name is not found
/// Typically ran using the header from the csv reader and is called
/// inside the [collect_column_info] function to do this for each target column.
/// Example:
/// ```
/// use drug_extraction_cli::get_column_info;
///
/// let header = vec!["ID", "NAME", "DESCRIPTION"];
/// let column = "NAME";
/// let column_info = get_column_info(&header, &column);
/// assert!(column_info.is_ok());
/// let column_info = column_info.unwrap();
/// assert_eq!(column_info.name, "NAME");
/// assert_eq!(column_info.index, 1);
/// ```
pub fn get_column_info<S: AsRef<str> + PartialEq>(header: &[S], column: &S) -> Result<ColumnInfo> {
    let pos = header.iter().position(|h| h == column);
    match pos {
        Some(i) => Ok(ColumnInfo {
            name: column.as_ref().to_string(),
            index: i,
        }),
        None => Err(eyre!("Unable to find column {}", column.as_ref())),
    }
}

/// Function to collect column info for each column to search
/// Typically ran using the header from the csv reader and is called
/// inside the [initialize_dataset] function to do this for each target column.
/// Example:
/// ```
/// use drug_extraction_cli::collect_column_info;
///
/// let header = vec!["ID", "NAME", "DESCRIPTION"];
/// let columns = vec!["NAME", "DESCRIPTION"];
/// let column_info = collect_column_info(&header, &columns);
/// assert!(column_info.is_ok());
/// let column_info = column_info.unwrap();
/// assert_eq!(column_info.len(), 2);
/// assert_eq!(column_info[0].name, "NAME");
/// assert_eq!(column_info[0].index, 1);
/// assert_eq!(column_info[1].name, "DESCRIPTION");
/// assert_eq!(column_info[1].index, 2);
/// ```
pub fn collect_column_info<S: AsRef<str> + PartialEq>(
    header: &[S],
    column_names: &[S],
) -> Result<Vec<ColumnInfo>> {
    column_names
        .iter()
        .map(|column| get_column_info(header, column))
        .collect()
}

/// Function to initialize the dataset
pub fn initialize_dataset<P: AsRef<Path>>(
    data_file: P,
    search_columns: &[String],
    id_column: Option<String>,
) -> Result<DataSet> {
    let mut rdr = csv::Reader::from_path(&data_file).wrap_err("Unable to initialize csv reader")?;
    let header = rdr
        .headers()
        .wrap_err("Unable to parse csv headers")?
        .iter()
        .map(clean_text)
        .collect_vec();
    // clean search cols and id col
    let clean_search_cols = search_columns.iter().map(|c| clean_text(c)).collect_vec();
    let clean_id_col = id_column.map(|c| clean_text(&c));
    let column_info = collect_column_info(&header, &clean_search_cols)
        .wrap_err("Unable to collect column indices")?;
    let ds = match clean_id_col {
        Some(c) => DataSet {
            reader: csv::Reader::from_path(&data_file)
                .wrap_err("Unable to initialize csv reader")?,
            rows: rdr.records().count(),
            clean_search_columns: column_info,
            clean_id_column: Some(get_column_info(&header, &c)?),
            writer: csv::Writer::from_path("output.csv")?,
        },
        None => DataSet {
            reader: csv::Reader::from_path(&data_file)
                .wrap_err("Unable to initialize csv reader")?,
            rows: rdr.records().count(),
            clean_search_columns: column_info,
            clean_id_column: None,
            writer: csv::Writer::from_path("output.csv")?,
        },
    };
    Ok(ds)
}

/// Primary search function
pub fn search(mut dataset: DataSet, search_terms: Vec<SearchTerm>) -> Result<()> {
    let mut total_records_with_matches = 0;
    let mut total_records = 0;
    let mut matched_terms: HashSet<&str> = HashSet::new();

    let spinner =
        initialize_progress_bar("Searching for matches...".to_string(), dataset.rows as u64);
    for (i, row) in dataset
        .reader
        .records()
        .enumerate()
        .progress_with(spinner.clone())
    {
        let record = row.wrap_err(format!("Unable to read record from line {}", i))?;

        let id = match &dataset.clean_id_column {
            Some(c) => record
                .get(c.index)
                .wrap_err(format!(
                    "Unable to read id column {} from line {}",
                    c.name, i
                ))?
                .to_string(),
            None => i.to_string(),
        };

        let mut found_match = false;
        for column in &dataset.clean_search_columns {
            let text = record.get(column.index).wrap_err(format!(
                "Unable to read column {} from line {}",
                column.name, i
            ))?;
            let cleaned_text = clean_text(text);
            let grams = cleaned_text.split_ascii_whitespace().collect_vec();
            for (term_len, term_list) in &search_terms
                .iter()
                .group_by(|st| st.term.split_ascii_whitespace().count())
            {
                let combos = if term_len == 1 {
                    term_list.cartesian_product(
                        grams
                            .iter()
                            .unique()
                            .map(|word| word.to_string())
                            .collect_vec(),
                    )
                } else {
                    term_list.cartesian_product(
                        grams
                            .windows(term_len)
                            .unique()
                            .map(|words| words.join(" "))
                            .collect_vec(),
                    )
                };
                for (search_term, comparison_term) in combos {
                    let edits = strsim::osa_distance(&search_term.term, &comparison_term);
                    match edits {
                        0 => {
                            dataset
                                .writer
                                .serialize(SearchOutput {
                                    row_id: &id,
                                    search_term: &search_term.term,
                                    matched_term: &comparison_term,
                                    edits,
                                    similarity_score: 1.0,
                                    search_field: &column.name,
                                    metadata: &search_term.metadata,
                                })
                                .wrap_err("Enable to serialize output")?;
                            found_match = true;
                            matched_terms.insert(&search_term.term);
                        }
                        1 => {
                            let sim = strsim::jaro_winkler(&search_term.term, &comparison_term);
                            if sim >= 0.95 {
                                dataset
                                    .writer
                                    .serialize(SearchOutput {
                                        row_id: &id,
                                        search_term: &search_term.term,
                                        matched_term: &comparison_term,
                                        edits,
                                        similarity_score: sim,
                                        search_field: &column.name,
                                        metadata: &search_term.metadata,
                                    })
                                    .wrap_err("Enable to serialize output")?;
                                found_match = true;
                                matched_terms.insert(&search_term.term);
                            }
                        }
                        2 => {
                            let sim = strsim::jaro_winkler(&search_term.term, &comparison_term);
                            if sim >= 0.97 {
                                dataset
                                    .writer
                                    .serialize(SearchOutput {
                                        row_id: &id,
                                        search_term: &search_term.term,
                                        matched_term: &comparison_term,
                                        edits,
                                        similarity_score: sim,
                                        search_field: &column.name,
                                        metadata: &search_term.metadata,
                                    })
                                    .wrap_err("Enable to serialize output")?;
                                found_match = true;
                                matched_terms.insert(&search_term.term);
                            }
                        }
                        _ => continue,
                    }
                }
            }
        }
        if found_match {
            total_records_with_matches += 1;
        }
        total_records += 1;
    }
    dataset.writer.flush().wrap_err("Unable to flush writer")?;
    spinner.finish_with_message("Done!");

    println!(
        "Found matches in {:} of {:} records ({:.2}%)",
        total_records_with_matches,
        total_records,
        (total_records_with_matches as f64 / total_records as f64) * 100.0
    );
    println!(
        "Found {:} of {:} search terms ({:.2}%)",
        matched_terms.len(),
        search_terms.len(),
        (matched_terms.len() as f64 / search_terms.len() as f64) * 100.0
    );

    Ok(())
}

pub fn run_searcher<P: AsRef<Path>>(
    data_file: P,
    search_terms_file: P,
    search_columns: Vec<String>,
    id_column: Option<String>,
) -> Result<()> {
    let search_terms = read_terms_from_file(search_terms_file)?;
    let dataset = initialize_dataset(data_file, &search_columns, id_column)?;
    search(dataset, search_terms)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_clean_text_no_changes() {
        let s = "This is a test string.";
        assert_eq!(clean_text(s), "this is a test string".to_ascii_uppercase());
    }

    #[test]
    fn test_clean_text_numeric() {
        let s = "This is a test string with 1234 numbers.";
        assert_eq!(
            clean_text(s),
            "this is a test string with 1234 numbers".to_ascii_uppercase()
        );
    }

    #[test]
    fn test_clean_text_symbols() {
        let s = "!@#$%^&*()_+-";
        assert_eq!(clean_text(s), "-");
    }

    #[test]
    fn test_clean_empty() {
        let s = "";
        assert_eq!(clean_text(s), "");
    }

    #[test]
    fn test_clean_end_whitespace() {
        let s = "!! This is a test string.   ";
        assert_eq!(clean_text(s), "this is a test string".to_ascii_uppercase());
    }

    #[test]
    fn test_clean_end_whitespace2() {
        let s = "!! This is a test to test- - hyphenated string.   ";
        assert_eq!(
            clean_text(s),
            "this is a test to test- - hyphenated string".to_ascii_uppercase()
        );
    }

    #[test]
    fn test_whitespace_split() {
        let s = "!! This is a test to test- - hyphenated string.   ";
        assert_eq!(
            clean_text(s),
            "this is a test to test- - hyphenated string".to_ascii_uppercase()
        );
        let c = clean_text(s);
        let v = c.split_ascii_whitespace().collect_vec();
        assert_eq!(
            v,
            vec![
                "THIS",
                "IS",
                "A",
                "TEST",
                "TO",
                "TEST-",
                "-",
                "HYPHENATED",
                "STRING"
            ]
        );
    }

    #[test]
    fn test_get_column_info() {
        let header = vec!["a", "b", "c"];
        let col = "a";
        assert_eq!(get_column_info(&header, &col).unwrap().index, 0);
    }

    #[test]
    fn test_get_column_info_errors() {
        let header = vec!["a", "b", "c"];
        let col = "d";
        assert!(get_column_info(&header, &col).is_err());
    }

    #[test]
    fn test_collect_column_info() {
        let header = vec!["a", "b", "c"];
        let cols = vec!["a", "b"];
        let info = collect_column_info(&header, &cols);
        assert!(info.is_ok());
        let info = info.unwrap();
        assert_eq!(info.len(), 2);
        assert_eq!(
            info,
            vec![
                ColumnInfo {
                    name: "a".to_string(),
                    index: 0
                },
                ColumnInfo {
                    name: "b".to_string(),
                    index: 1
                }
            ]
        );
    }

    #[test]
    fn test_collect_column_info_sample() -> Result<()> {
        let header = csv::Reader::from_path("../data/search_terms.csv")?
            .headers()?
            .into_iter()
            .map(clean_text)
            .collect_vec();
        let cols = ["term", "metadata"]
            .iter()
            .map(|c| clean_text(c))
            .collect_vec();
        let info = collect_column_info(&header, &cols)?;
        assert_eq!(info.len(), 2);
        Ok(())
    }

    #[test]
    fn test_enumerated_reader() {
        let mut reader = csv::Reader::from_path("../data/search_terms.csv").unwrap();
        let (i, _) = reader.records().enumerate().next().unwrap();
        assert!(i == 0);
    }
}