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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// MIT License
//
// Copyright (c) [year] [fullname]
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#![warn(
    missing_docs,
    trivial_casts,
    trivial_numeric_casts,
    unused_import_braces,
    unused_qualifications
)]
#![deny(clippy::all, clippy::pedantic)]
#![allow(clippy::module_name_repetitions)]

//! **Disclaimer:** This is very much work in progress, and in the very early stage at that.
//! Absolutely **no backwards-compatilbility** should be assumed.
//!
//! Cranky is a library and tool for evaluating query effectiveness for information retrieval.
//!
//! It is an attempt to replace tools like [`trec_eval`](https://github.com/usnistgov/trec_eval),
//! [`gdeval`](https://trec.nist.gov/data/web/12/gdeval.pl),
//! and [`ndeval`](https://trec.nist.gov/data/web/12/ndeval.c).
//! The goal is to be fast but also provide a codebase that is easier to maintain, as well as
//! provide interface to use in Rust and (eventually) C/C++ libraries.
//! From there, we can create bindings to basically any other language.

use failure::{format_err, Error, ResultExt};
use itertools::EitherOrBoth::{Both, Left, Right};
use itertools::Itertools;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::fmt;
use std::io::{prelude::*, BufReader};
use std::rc::Rc;
use std::str::FromStr;

/// Represents a query ID in TREC format.
///
/// The IDs are strings, and therefore can be costly to copy.
/// However, there are typically many records with the same query ID.
/// We use this fact by storing a reference counted pointer instead.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Qid(pub Rc<String>);

/// Represents a query iteration.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Iter(pub Rc<String>);

/// Represents a query run.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Run(pub Rc<String>);

/// Document TREC ID.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Docid(pub String);

/// Floating point result score.
#[derive(Debug, PartialEq, PartialOrd)]
pub struct Score(pub f32);

/// Document rank.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Rank(pub u32);

/// Document relevance used as gold standard.
#[derive(Debug, PartialEq, Eq)]
pub enum Relevance {
    /// Judged document with a certain relevance score.
    Judged(u8),
    /// Document present in the reference file but left unjudged.
    Unjudged,
    /// Document not present.
    Missing,
}

impl fmt::Display for Relevance {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use Relevance::*;
        match self {
            &Judged(rel) => write!(f, "{}", rel),
            Unjudged => write!(f, "-1"),
            Missing => write!(f, "-2"),
        }
    }
}

impl Default for Relevance {
    fn default() -> Self {
        Self::Missing
    }
}

impl From<Rc<String>> for Qid {
    fn from(qid: Rc<String>) -> Self {
        Self(qid)
    }
}

impl From<Rc<String>> for Iter {
    fn from(id: Rc<String>) -> Self {
        Self(id)
    }
}

impl From<Rc<String>> for Run {
    fn from(id: Rc<String>) -> Self {
        Self(id)
    }
}

impl FromStr for Score {
    type Err = Error;

    fn from_str(score: &str) -> Result<Self, Self::Err> {
        let score: f32 = score.parse()?;
        Ok(Self(score))
    }
}

impl FromStr for Rank {
    type Err = Error;

    fn from_str(rank: &str) -> Result<Self, Self::Err> {
        let rank: u32 = rank.parse()?;
        Ok(Self(rank))
    }
}

impl FromStr for Relevance {
    type Err = Error;

    fn from_str(rank: &str) -> Result<Self, Self::Err> {
        let judgement: i32 = rank.parse()?;
        match judgement {
            -1 => Ok(Self::Unjudged),
            rel => Ok(Self::Judged(
                u8::try_from(rel).context("Error parsing judgement")?,
            )),
        }
    }
}

/// Result record.
///
/// # Examples
///
/// The separator could be any number of whitespaces.
///
/// ```
/// # use cranky::{Record, ResultRecord};
/// # fn main() -> Result<(), failure::Error> {
/// let record_line = "030  Q0\tZF08-175-870  0 \t 4238   prise1";
/// let record: ResultRecord = Record::parse_record(record_line, None)?;
/// # Ok(())
/// # }
/// ```
///
/// You can use [`StringIdFactory`](struct.StringIdFactory.html) to reuse string-based IDs.
///
/// ```
/// # use cranky::{Record, ResultRecord, StringIdFactory};
/// # fn main() -> Result<(), failure::Error> {
/// let mut id_factory = StringIdFactory::new();
/// let record_line = "030  Q0\tZF08-175-870  0 \t 4238   prise1";
/// let record: ResultRecord = Record::parse_record(record_line, Some(&mut id_factory))?;
/// # Ok(())
/// # }
/// ```
///
/// When formating to a string, all separators are tabs:
///
/// ```
/// # use cranky::{Record, ResultRecord, StringIdFactory};
/// # fn main() -> Result<(), failure::Error> {
/// # let mut id_factory = StringIdFactory::new();
/// # let record_line = "030  Q0\tZF08-175-870  0 \t 4238   prise1";
/// # let record: ResultRecord = Record::parse_record(record_line, Some(&mut id_factory))?;
/// assert_eq!(
///     &format!("{}", record),
///     "030\tQ0\tZF08-175-870\t0\t4238\tprise1"
/// );
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct ResultRecord {
    /// Query TREC ID.
    pub qid: Qid,
    /// Document TREC ID.
    pub docid: Docid,
    /// Rank of the document in query result set.
    /// The lower the number, the higher the document is ranked.
    pub rank: Rank,
    /// Score of the document in query result set.
    /// The higher the number, the higher the document is ranked.
    pub score: Score,
    /// Iteration.
    pub iter: Iter,
    /// Run ID.
    pub run: Option<Run>,
    relevance: Relevance,
}

impl Eq for ResultRecord {}

impl Ord for ResultRecord {
    fn cmp(&self, other: &Self) -> Ordering {
        (&self.iter, &self.qid, &self.docid).cmp(&(&other.iter, &other.qid, &other.docid))
    }
}

impl PartialOrd for ResultRecord {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        (&self.iter, &self.qid, &self.docid).partial_cmp(&(&other.iter, &other.qid, &other.docid))
    }
}

impl PartialEq for ResultRecord {
    fn eq(&self, other: &Self) -> bool {
        (&self.iter, &self.qid, &self.docid) == (&other.iter, &other.qid, &other.docid)
    }
}

impl ResultRecord {
    fn with_relevance(self, relevance: Relevance) -> Self {
        Self { relevance, ..self }
    }
}

/// Judgement record.
#[derive(Debug)]
pub struct JudgementRecord {
    /// Query TREC ID.
    pub qid: Qid,
    /// Document TREC ID.
    pub docid: Docid,
    /// Iteration.
    pub iter: Iter,
    /// Gold standard relevance.
    pub relevance: Relevance,
}

impl Eq for JudgementRecord {}

impl Ord for JudgementRecord {
    fn cmp(&self, other: &Self) -> Ordering {
        (&self.iter, &self.qid, &self.docid).cmp(&(&other.iter, &other.qid, &other.docid))
    }
}

impl PartialOrd for JudgementRecord {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        (&self.iter, &self.qid, &self.docid).partial_cmp(&(&other.iter, &other.qid, &other.docid))
    }
}

impl PartialEq for JudgementRecord {
    fn eq(&self, other: &Self) -> bool {
        (&self.iter, &self.qid, &self.docid) == (&other.iter, &other.qid, &other.docid)
    }
}

/// Result or judgement record.
pub trait Record: Sized + fmt::Display {
    /// Parses record from a line of text.
    /// `record_factory` is optionally used for reusing string-based IDs;
    /// in case of its absense, all strings will be copied to the record.
    fn parse_record(
        record_line: &str,
        record_factory: Option<&mut StringIdFactory>,
    ) -> Result<Self, Error>;
}

fn rcid(id_factory: &mut Option<&mut StringIdFactory>, id: &str) -> Rc<String> {
    id_factory
        .as_mut()
        .map_or_else(|| Rc::new(id.to_string()), |f| f.get(id))
}

impl fmt::Display for ResultRecord {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}\t{}\t{}\t{}\t{}",
            self.qid.0, self.iter.0, self.docid.0, self.rank.0, self.score.0
        )?;
        if let Some(run) = &self.run {
            write!(f, "\t{}", run.0)
        } else {
            write!(f, "")
        }
    }
}

impl Record for ResultRecord {
    fn parse_record(
        record_line: &str,
        mut id_factory: Option<&mut StringIdFactory>,
    ) -> Result<Self, Error> {
        let fields: Vec<&str> = record_line.split_whitespace().collect();
        if fields.len() < 5 {
            return Err(format_err!(
                "Invalid number of colums {}; expected at least 5",
                fields.len()
            ));
        }
        let qid = Qid(rcid(&mut id_factory, fields[0]));
        let iter = Iter(rcid(&mut id_factory, fields[1]));
        let docid = Docid(String::from(fields[2]));
        let rank: Rank = fields[3].parse()?;
        let score: Score = fields[4].parse()?;
        let run = if fields.len() > 5 {
            Some(Run(rcid(&mut id_factory, fields[5])))
        } else {
            None
        };
        Ok(Self {
            qid,
            iter,
            docid,
            rank,
            score,
            run,
            relevance: Relevance::default(),
        })
    }
}

impl fmt::Display for JudgementRecord {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}\t{}\t{}\t{}",
            self.qid.0, self.iter.0, self.docid.0, self.relevance
        )
    }
}

impl Record for JudgementRecord {
    fn parse_record(
        record_line: &str,
        mut id_factory: Option<&mut StringIdFactory>,
    ) -> Result<Self, Error> {
        let fields: Vec<&str> = record_line.split_whitespace().collect();
        if fields.len() != 4 {
            return Err(format_err!(
                "Invalid number of colums {}; expected 4",
                fields.len()
            ));
        }
        let qid = Qid(rcid(&mut id_factory, fields[0]));
        let iter = Iter(rcid(&mut id_factory, fields[1]));
        let docid = Docid(String::from(fields[2]));
        let relevance: Relevance = fields[3].parse()?;
        Ok(Self {
            qid,
            iter,
            docid,
            relevance,
        })
    }
}

/// Abstraction over a set of results from a single file in TREC format.
pub struct ResultSet(pub Vec<ResultRecord>);

/// Read records.
pub fn read_records<R, T>(reader: R) -> Result<Vec<T>, Error>
where
    R: Read,
    T: Record,
{
    let reader = BufReader::new(reader);
    let mut id_factory = StringIdFactory::new();
    let res: Result<Vec<_>, Error> = reader
        .lines()
        .map(|line| T::parse_record(&line?, Some(&mut id_factory)))
        .collect();
    Ok(res?)
}

impl ResultSet {
    /// Reads file to memory.
    pub fn from_reader<R: Read>(reader: R) -> Result<Self, Error> {
        Ok(Self(read_records(reader)?))
    }

    /// Consumes a result set and judgements, and returns a new result set with applied judgements.
    pub fn apply_judgements(mut self, mut judgements: Judgements) -> Self {
        self.0.sort();
        judgements.0.sort();
        let compare = |res: &ResultRecord, rel: &JudgementRecord| {
            (&res.iter, &res.qid, &res.docid).cmp(&(&rel.iter, &rel.qid, &rel.docid))
        };
        Self(
            self.0
                .into_iter()
                .merge_join_by(judgements.0, compare)
                .filter_map(|either| match either {
                    Left(result) => Some(result.with_relevance(Relevance::Missing)),
                    Right(_) => None,
                    Both(result, judgement) => Some(result.with_relevance(judgement.relevance)),
                })
                .collect(),
        )
    }
}

/// Abstraction over a set of relevance judgements.
pub struct Judgements(pub Vec<JudgementRecord>);

impl Judgements {
    /// Reads file to memory.
    pub fn from_reader<R: Read>(reader: R) -> Result<Self, Error> {
        Ok(Self(read_records(reader)?))
    }
}

/// A convenience structure used to produce String IDs.
/// It stores all previously used query IDs. When possible, it reuses a string.
pub struct StringIdFactory {
    ids: HashMap<String, Rc<String>>,
}

impl StringIdFactory {
    /// Constructs a new empty factory.
    pub fn new() -> Self {
        Self {
            ids: HashMap::new(),
        }
    }

    /// Construct a new ID reusing a string if possible.
    pub fn get(&mut self, qid: &str) -> Rc<String> {
        if let Some(qid) = self.ids.get(qid) {
            Rc::clone(qid)
        } else {
            let new_qid = Rc::new(qid.to_string());
            self.ids.insert(qid.to_string(), Rc::clone(&new_qid));
            new_qid
        }
    }
}

impl Default for StringIdFactory {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod test {

    use super::*;
    use std::io::Cursor;

    const TREC_FILE: &str = "321 Q0  9171    0   10.9951 R0
321 Q0  4a7c    1   10.9951 R0
321 Q0  a5df    2   10.929  R0
336 Q0  d6ed    0   12.7334 R0
336 Q0  6ddf    1   12.5704 R0
336 Q0  29a7    14  11.4663 R0";

    const QRELS: &str = "321 Q0  9171    0
321 Q0  4a7c    -1
321 Q0  a5df    2
336 Q0  6ddf    1
336 Q0  29a7    0";

    fn rec(
        qid: &str,
        iter: &str,
        docid: &str,
        rank: u32,
        score: f32,
        run: &str,
        rel: Relevance,
    ) -> ResultRecord {
        ResultRecord {
            qid: Qid(Rc::new(qid.to_string())),
            iter: Iter(Rc::new(iter.to_string())),
            docid: Docid(docid.to_string()),
            rank: Rank(rank),
            score: Score(score),
            run: Some(Run(Rc::new(run.to_string()))),
            relevance: rel,
        }
    }

    fn rel(qid: &str, iter: &str, docid: &str, rel: Relevance) -> JudgementRecord {
        JudgementRecord {
            qid: Qid(Rc::new(qid.to_string())),
            iter: Iter(Rc::new(iter.to_string())),
            docid: Docid(docid.to_string()),
            relevance: rel,
        }
    }

    #[test]
    fn test_trec_file() {
        let cursor = Cursor::new(&TREC_FILE);
        let trec_file = ResultSet::from_reader(cursor).expect("Could not parse TREC file");
        assert_eq!(
            trec_file.0,
            vec![
                rec("321", "Q0", "9171", 0, 10.9951, "R0", Relevance::Missing),
                rec("321", "Q0", "4a7c", 1, 10.9951, "R0", Relevance::Missing),
                rec("321", "Q0", "a5df", 2, 10.929, "R0", Relevance::Missing),
                rec("336", "Q0", "d6ed", 0, 12.7334, "R0", Relevance::Missing),
                rec("336", "Q0", "6ddf", 1, 12.5704, "R0", Relevance::Missing),
                rec("336", "Q0", "29a7", 14, 11.4663, "R0", Relevance::Missing),
            ]
        );
    }

    #[test]
    fn test_qrels() {
        let cursor = Cursor::new(&QRELS);
        let trec_file = Judgements::from_reader(cursor).expect("Could not parse TREC file");
        assert_eq!(
            trec_file.0,
            vec![
                rel("321", "Q0", "9171", Relevance::Judged(0)),
                rel("321", "Q0", "4a7c", Relevance::Unjudged),
                rel("321", "Q0", "a5df", Relevance::Judged(2)),
                rel("336", "Q0", "6ddf", Relevance::Judged(1)),
                rel("336", "Q0", "29a7", Relevance::Judged(0)),
            ]
        );
    }

    #[test]
    fn test_apply_judgements() {
        let cursor = Cursor::new(&TREC_FILE);
        let results = ResultSet::from_reader(cursor).expect("Could not parse TREC file");
        let cursor = Cursor::new(&QRELS);
        let judgements = Judgements::from_reader(cursor).expect("Could not parse TREC file");
        assert_eq!(
            results.apply_judgements(judgements).0,
            vec![
                rec("321", "Q0", "4a7c", 1, 10.9951, "R0", Relevance::Unjudged),
                rec("321", "Q0", "9171", 0, 10.9951, "R0", Relevance::Judged(0)),
                rec("321", "Q0", "a5df", 2, 10.929, "R0", Relevance::Judged(2)),
                rec("336", "Q0", "29a7", 14, 11.4663, "R0", Relevance::Judged(0)),
                rec("336", "Q0", "6ddf", 1, 12.5704, "R0", Relevance::Judged(1)),
                rec("336", "Q0", "d6ed", 0, 12.7334, "R0", Relevance::Missing),
            ]
        );
    }
}