2020-08-17 15:15:37 +02:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::{str, io};
|
|
|
|
|
2020-08-23 10:52:47 +02:00
|
|
|
use anyhow::Context;
|
2020-08-17 15:15:37 +02:00
|
|
|
use heed::EnvOpenOptions;
|
|
|
|
use milli::Index;
|
|
|
|
use structopt::StructOpt;
|
|
|
|
|
2020-08-23 10:52:47 +02:00
|
|
|
use Command::*;
|
|
|
|
|
2020-08-17 15:15:37 +02:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[global_allocator]
|
|
|
|
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
|
|
|
|
|
2020-10-02 16:46:05 +02:00
|
|
|
const MAIN_DB_NAME: &str = "main";
|
|
|
|
const WORD_DOCIDS_DB_NAME: &str = "word-docids";
|
|
|
|
const DOCID_WORD_POSITIONS_DB_NAME: &str = "docid-word-positions";
|
|
|
|
const WORD_PAIR_PROXIMITY_DOCIDS_DB_NAME: &str = "word-pair-proximity-docids";
|
|
|
|
const DOCUMENTS_DB_NAME: &str = "documents";
|
|
|
|
|
|
|
|
const DATABASE_NAMES: &[&str] = &[
|
|
|
|
MAIN_DB_NAME,
|
|
|
|
WORD_DOCIDS_DB_NAME,
|
|
|
|
DOCID_WORD_POSITIONS_DB_NAME,
|
|
|
|
WORD_PAIR_PROXIMITY_DOCIDS_DB_NAME,
|
|
|
|
DOCUMENTS_DB_NAME,
|
|
|
|
];
|
|
|
|
|
2020-08-17 15:15:37 +02:00
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
#[structopt(name = "milli-info", about = "A stats crawler for milli.")]
|
|
|
|
struct Opt {
|
|
|
|
/// The database path where the database is located.
|
|
|
|
/// It is created if it doesn't already exist.
|
|
|
|
#[structopt(long = "db", parse(from_os_str))]
|
|
|
|
database: PathBuf,
|
|
|
|
|
|
|
|
/// The maximum size the database can take on disk. It is recommended to specify
|
|
|
|
/// the whole disk space (value must be a multiple of a page size).
|
|
|
|
#[structopt(long = "db-size", default_value = "107374182400")] // 100 GB
|
|
|
|
database_size: usize,
|
|
|
|
|
|
|
|
/// Verbose mode (-v, -vv, -vvv, etc.)
|
|
|
|
#[structopt(short, long, parse(from_occurrences))]
|
|
|
|
verbose: usize,
|
|
|
|
|
|
|
|
#[structopt(subcommand)]
|
|
|
|
command: Command,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
enum Command {
|
|
|
|
/// Outputs a CSV of the most frequent words of this index.
|
|
|
|
///
|
|
|
|
/// `word` are displayed and ordered by frequency.
|
|
|
|
/// `document_frequency` defines the number of documents which contains the word.
|
|
|
|
MostCommonWords {
|
|
|
|
/// The maximum number of frequencies to return.
|
|
|
|
#[structopt(default_value = "10")]
|
|
|
|
limit: usize,
|
2020-08-21 14:24:05 +02:00
|
|
|
},
|
|
|
|
|
2020-08-21 14:42:55 +02:00
|
|
|
/// Outputs a CSV with the biggest entries of the database.
|
2020-09-06 17:14:20 +02:00
|
|
|
BiggestValues {
|
2020-08-21 14:42:55 +02:00
|
|
|
/// The maximum number of sizes to return.
|
|
|
|
#[structopt(default_value = "10")]
|
|
|
|
limit: usize,
|
|
|
|
},
|
2020-08-23 10:52:47 +02:00
|
|
|
|
2020-09-07 22:36:35 +02:00
|
|
|
/// Outputs a CSV with the documents ids where the given words appears.
|
|
|
|
WordsDocids {
|
|
|
|
/// Display the whole documents ids in details.
|
|
|
|
#[structopt(long)]
|
|
|
|
full_display: bool,
|
|
|
|
|
|
|
|
/// The words to display the documents ids of.
|
|
|
|
words: Vec<String>,
|
|
|
|
},
|
|
|
|
|
2020-09-07 14:56:48 +02:00
|
|
|
/// Outputs the total size of all the docid-word-positions keys and values.
|
|
|
|
TotalDocidWordPositionsSize,
|
|
|
|
|
|
|
|
/// Outputs the average number of *different* words by document.
|
|
|
|
AverageNumberOfWordsByDoc,
|
|
|
|
|
2020-09-07 15:26:42 +02:00
|
|
|
/// Outputs the average number of positions for each document words.
|
2020-09-29 18:11:44 +02:00
|
|
|
AverageNumberOfPositionsByWord,
|
|
|
|
|
2020-09-29 18:32:48 +02:00
|
|
|
/// Outputs the average number of documents for each words pair.
|
|
|
|
AverageNumberOfDocumentByWordPairProximity,
|
2020-09-07 15:26:42 +02:00
|
|
|
|
2020-09-30 17:41:54 +02:00
|
|
|
/// Outputs some statistics about the words pairs proximities
|
|
|
|
/// (median, quartiles, percentiles, min, max).
|
|
|
|
WordPairProximityStats,
|
|
|
|
|
2020-10-02 16:46:05 +02:00
|
|
|
/// Outputs the size in bytes of the specified database.
|
|
|
|
SizeOfDatabase {
|
|
|
|
#[structopt(possible_values = DATABASE_NAMES)]
|
|
|
|
database: String,
|
|
|
|
},
|
|
|
|
|
2020-09-22 13:52:24 +02:00
|
|
|
/// Outputs a CSV with the proximities for the two specidied words and
|
|
|
|
/// the documents ids where these relations appears.
|
|
|
|
///
|
2020-09-22 14:49:22 +02:00
|
|
|
/// `word1`, `word2` defines the word pair specified *in this specific order*.
|
2020-09-22 13:52:24 +02:00
|
|
|
/// `proximity` defines the proximity between the two specified words.
|
|
|
|
/// `documents_ids` defines the documents ids where the relation appears.
|
|
|
|
WordPairProximitiesDocids {
|
|
|
|
/// Display the whole documents ids in details.
|
|
|
|
#[structopt(long)]
|
|
|
|
full_display: bool,
|
|
|
|
|
|
|
|
/// First word of the word pair.
|
|
|
|
word1: String,
|
|
|
|
|
|
|
|
/// Second word of the word pair.
|
|
|
|
word2: String,
|
|
|
|
},
|
|
|
|
|
2020-09-06 17:14:20 +02:00
|
|
|
/// Outputs the words FST to disk.
|
|
|
|
///
|
|
|
|
/// One can use the FST binary helper to dissect and analyze it,
|
|
|
|
/// you can install it using `cargo install fst-bin`.
|
|
|
|
ExportWordsFst {
|
|
|
|
/// The path where the FST will be written.
|
|
|
|
#[structopt(short, long, default_value = "words.fst")]
|
|
|
|
output: PathBuf,
|
2020-08-29 18:12:31 +02:00
|
|
|
},
|
2020-08-17 15:15:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> anyhow::Result<()> {
|
|
|
|
let opt = Opt::from_args();
|
|
|
|
|
|
|
|
stderrlog::new()
|
|
|
|
.verbosity(opt.verbose)
|
|
|
|
.show_level(false)
|
|
|
|
.timestamp(stderrlog::Timestamp::Off)
|
|
|
|
.init()?;
|
|
|
|
|
|
|
|
let env = EnvOpenOptions::new()
|
|
|
|
.map_size(opt.database_size)
|
|
|
|
.max_dbs(10)
|
|
|
|
.open(&opt.database)?;
|
|
|
|
|
|
|
|
// Open the LMDB database.
|
2020-08-28 15:38:05 +02:00
|
|
|
let index = Index::new(&env)?;
|
2020-08-17 15:15:37 +02:00
|
|
|
let rtxn = env.read_txn()?;
|
|
|
|
|
|
|
|
match opt.command {
|
2020-08-23 10:52:47 +02:00
|
|
|
MostCommonWords { limit } => most_common_words(&index, &rtxn, limit),
|
2020-09-06 17:14:20 +02:00
|
|
|
BiggestValues { limit } => biggest_value_sizes(&index, &rtxn, limit),
|
2020-09-07 22:36:35 +02:00
|
|
|
WordsDocids { full_display, words } => words_docids(&index, &rtxn, !full_display, words),
|
2020-09-07 14:56:48 +02:00
|
|
|
TotalDocidWordPositionsSize => total_docid_word_positions_size(&index, &rtxn),
|
|
|
|
AverageNumberOfWordsByDoc => average_number_of_words_by_doc(&index, &rtxn),
|
2020-09-29 18:11:44 +02:00
|
|
|
AverageNumberOfPositionsByWord => {
|
|
|
|
average_number_of_positions_by_word(&index, &rtxn)
|
|
|
|
},
|
2020-10-02 16:46:05 +02:00
|
|
|
SizeOfDatabase { database } => size_of_database(&index, &rtxn, &database),
|
2020-09-29 18:32:48 +02:00
|
|
|
AverageNumberOfDocumentByWordPairProximity => {
|
|
|
|
average_number_of_document_by_word_pair_proximity(&index, &rtxn)
|
2020-09-30 17:41:54 +02:00
|
|
|
},
|
|
|
|
WordPairProximityStats => word_pair_proximity_stats(&index, &rtxn),
|
2020-09-22 13:52:24 +02:00
|
|
|
WordPairProximitiesDocids { full_display, word1, word2 } => {
|
|
|
|
word_pair_proximities_docids(&index, &rtxn, !full_display, word1, word2)
|
|
|
|
},
|
2020-09-06 17:14:20 +02:00
|
|
|
ExportWordsFst { output } => export_words_fst(&index, &rtxn, output),
|
2020-08-17 15:15:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn most_common_words(index: &Index, rtxn: &heed::RoTxn, limit: usize) -> anyhow::Result<()> {
|
|
|
|
use std::collections::BinaryHeap;
|
|
|
|
use std::cmp::Reverse;
|
|
|
|
|
|
|
|
let mut heap = BinaryHeap::with_capacity(limit + 1);
|
2020-09-06 17:14:20 +02:00
|
|
|
for result in index.word_docids.iter(rtxn)? {
|
2020-08-17 15:15:37 +02:00
|
|
|
if limit == 0 { break }
|
2020-09-06 17:14:20 +02:00
|
|
|
let (word, docids) = result?;
|
|
|
|
heap.push((Reverse(docids.len()), word));
|
2020-08-17 15:15:37 +02:00
|
|
|
if heap.len() > limit { heap.pop(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
let stdout = io::stdout();
|
|
|
|
let mut wtr = csv::Writer::from_writer(stdout.lock());
|
2020-09-06 17:14:20 +02:00
|
|
|
wtr.write_record(&["word", "document_frequency"])?;
|
2020-08-17 15:15:37 +02:00
|
|
|
|
2020-09-06 17:14:20 +02:00
|
|
|
for (Reverse(document_frequency), word) in heap.into_sorted_vec() {
|
|
|
|
wtr.write_record(&[word, &document_frequency.to_string()])?;
|
2020-08-21 14:24:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(wtr.flush()?)
|
|
|
|
}
|
2020-08-21 14:42:55 +02:00
|
|
|
|
|
|
|
fn biggest_value_sizes(index: &Index, rtxn: &heed::RoTxn, limit: usize) -> anyhow::Result<()> {
|
|
|
|
use std::cmp::Reverse;
|
|
|
|
use std::collections::BinaryHeap;
|
|
|
|
use heed::types::{Str, ByteSlice};
|
2020-09-06 17:14:20 +02:00
|
|
|
use milli::heed_codec::BEU32StrCodec;
|
2020-08-21 14:42:55 +02:00
|
|
|
|
2020-08-26 14:36:22 +02:00
|
|
|
let main_name = "main";
|
2020-09-06 17:14:20 +02:00
|
|
|
let word_docids_name = "word_docids";
|
|
|
|
let docid_word_positions_name = "docid_word_positions";
|
2020-08-21 14:42:55 +02:00
|
|
|
|
|
|
|
let mut heap = BinaryHeap::with_capacity(limit + 1);
|
|
|
|
|
|
|
|
if limit > 0 {
|
2020-08-26 14:36:22 +02:00
|
|
|
if let Some(fst) = index.fst(rtxn)? {
|
|
|
|
heap.push(Reverse((fst.as_fst().as_bytes().len(), format!("words-fst"), main_name)));
|
|
|
|
if heap.len() > limit { heap.pop(); }
|
|
|
|
}
|
|
|
|
|
2020-08-29 18:12:31 +02:00
|
|
|
if let Some(documents) = index.main.get::<_, Str, ByteSlice>(rtxn, "documents")? {
|
2020-08-28 15:38:05 +02:00
|
|
|
heap.push(Reverse((documents.len(), format!("documents"), main_name)));
|
|
|
|
if heap.len() > limit { heap.pop(); }
|
|
|
|
}
|
|
|
|
|
2020-08-29 18:12:31 +02:00
|
|
|
if let Some(documents_ids) = index.main.get::<_, Str, ByteSlice>(rtxn, "documents-ids")? {
|
|
|
|
heap.push(Reverse((documents_ids.len(), format!("documents-ids"), main_name)));
|
|
|
|
if heap.len() > limit { heap.pop(); }
|
|
|
|
}
|
|
|
|
|
2020-09-06 17:14:20 +02:00
|
|
|
for result in index.word_docids.as_polymorph().iter::<_, Str, ByteSlice>(rtxn)? {
|
2020-08-21 14:42:55 +02:00
|
|
|
let (word, value) = result?;
|
2020-09-06 17:14:20 +02:00
|
|
|
heap.push(Reverse((value.len(), word.to_string(), word_docids_name)));
|
2020-08-21 14:42:55 +02:00
|
|
|
if heap.len() > limit { heap.pop(); }
|
|
|
|
}
|
|
|
|
|
2020-09-06 17:14:20 +02:00
|
|
|
for result in index.docid_word_positions.as_polymorph().iter::<_, BEU32StrCodec, ByteSlice>(rtxn)? {
|
|
|
|
let ((docid, word), value) = result?;
|
|
|
|
let key = format!("{} {}", docid, word);
|
|
|
|
heap.push(Reverse((value.len(), key, docid_word_positions_name)));
|
2020-08-21 14:42:55 +02:00
|
|
|
if heap.len() > limit { heap.pop(); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let stdout = io::stdout();
|
|
|
|
let mut wtr = csv::Writer::from_writer(stdout.lock());
|
2020-08-26 14:36:22 +02:00
|
|
|
wtr.write_record(&["database_name", "key_name", "size"])?;
|
2020-08-21 14:42:55 +02:00
|
|
|
|
|
|
|
for Reverse((size, key_name, database_name)) in heap.into_sorted_vec() {
|
|
|
|
wtr.write_record(&[database_name.to_string(), key_name, size.to_string()])?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(wtr.flush()?)
|
2020-08-23 10:52:47 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 22:36:35 +02:00
|
|
|
fn words_docids(index: &Index, rtxn: &heed::RoTxn, debug: bool, words: Vec<String>) -> anyhow::Result<()> {
|
|
|
|
let stdout = io::stdout();
|
|
|
|
let mut wtr = csv::Writer::from_writer(stdout.lock());
|
|
|
|
wtr.write_record(&["word", "documents_ids"])?;
|
|
|
|
|
|
|
|
for word in words {
|
|
|
|
if let Some(docids) = index.word_docids.get(rtxn, &word)? {
|
|
|
|
let docids = if debug {
|
|
|
|
format!("{:?}", docids)
|
|
|
|
} else {
|
|
|
|
format!("{:?}", docids.iter().collect::<Vec<_>>())
|
|
|
|
};
|
|
|
|
wtr.write_record(&[word, docids])?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(wtr.flush()?)
|
|
|
|
}
|
|
|
|
|
2020-09-06 17:14:20 +02:00
|
|
|
fn export_words_fst(index: &Index, rtxn: &heed::RoTxn, output: PathBuf) -> anyhow::Result<()> {
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write as _;
|
2020-08-23 10:52:47 +02:00
|
|
|
|
2020-09-06 17:14:20 +02:00
|
|
|
let mut output = File::create(&output)
|
|
|
|
.with_context(|| format!("failed to create {} file", output.display()))?;
|
2020-08-23 10:52:47 +02:00
|
|
|
|
2020-09-06 17:14:20 +02:00
|
|
|
match index.fst(rtxn)? {
|
|
|
|
Some(fst) => output.write_all(fst.as_fst().as_bytes())?,
|
|
|
|
None => {
|
|
|
|
let fst = fst::Set::default();
|
|
|
|
output.write_all(fst.as_fst().as_bytes())?;
|
|
|
|
},
|
2020-08-23 10:52:47 +02:00
|
|
|
}
|
|
|
|
|
2020-09-06 17:14:20 +02:00
|
|
|
Ok(())
|
2020-08-21 14:42:55 +02:00
|
|
|
}
|
2020-09-07 14:56:48 +02:00
|
|
|
|
|
|
|
fn total_docid_word_positions_size(index: &Index, rtxn: &heed::RoTxn) -> anyhow::Result<()> {
|
|
|
|
use heed::types::ByteSlice;
|
|
|
|
|
|
|
|
let mut total_key_size = 0;
|
|
|
|
let mut total_val_size = 0;
|
|
|
|
let mut count = 0;
|
|
|
|
|
|
|
|
let iter = index.docid_word_positions.as_polymorph().iter::<_, ByteSlice, ByteSlice>(rtxn)?;
|
|
|
|
for result in iter {
|
|
|
|
let (key, val) = result?;
|
|
|
|
total_key_size += key.len();
|
|
|
|
total_val_size += val.len();
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("number of keys: {}", count);
|
|
|
|
println!("total key size: {}", total_key_size);
|
|
|
|
println!("total value size: {}", total_val_size);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn average_number_of_words_by_doc(index: &Index, rtxn: &heed::RoTxn) -> anyhow::Result<()> {
|
|
|
|
use heed::types::DecodeIgnore;
|
|
|
|
use milli::{DocumentId, BEU32StrCodec};
|
|
|
|
|
|
|
|
let mut words_counts = Vec::new();
|
|
|
|
let mut count = 0;
|
|
|
|
let mut prev = None as Option<(DocumentId, u32)>;
|
|
|
|
|
|
|
|
let iter = index.docid_word_positions.as_polymorph().iter::<_, BEU32StrCodec, DecodeIgnore>(rtxn)?;
|
|
|
|
for result in iter {
|
|
|
|
let ((docid, _word), ()) = result?;
|
|
|
|
|
|
|
|
match prev.as_mut() {
|
|
|
|
Some((prev_docid, prev_count)) if docid == *prev_docid => {
|
|
|
|
*prev_count += 1;
|
|
|
|
},
|
|
|
|
Some((prev_docid, prev_count)) => {
|
|
|
|
words_counts.push(*prev_count);
|
|
|
|
*prev_docid = docid;
|
|
|
|
*prev_count = 0;
|
|
|
|
count += 1;
|
|
|
|
},
|
|
|
|
None => prev = Some((docid, 1)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some((_, prev_count)) = prev.take() {
|
|
|
|
words_counts.push(prev_count);
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let words_count = words_counts.into_iter().map(|c| c as usize).sum::<usize>() as f64;
|
|
|
|
let count = count as f64;
|
|
|
|
|
|
|
|
println!("average number of different words by document: {}", words_count / count);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-09-07 15:26:42 +02:00
|
|
|
|
2020-09-29 18:11:44 +02:00
|
|
|
fn average_number_of_positions_by_word(index: &Index, rtxn: &heed::RoTxn) -> anyhow::Result<()> {
|
2020-09-07 15:26:42 +02:00
|
|
|
use heed::types::DecodeIgnore;
|
2020-09-07 15:42:20 +02:00
|
|
|
use milli::ByteorderXRoaringBitmapCodec;
|
2020-09-07 15:26:42 +02:00
|
|
|
|
|
|
|
let mut values_length = Vec::new();
|
|
|
|
let mut count = 0;
|
|
|
|
|
2020-09-07 15:42:20 +02:00
|
|
|
let db = index.docid_word_positions.as_polymorph();
|
|
|
|
for result in db.iter::<_, DecodeIgnore, ByteorderXRoaringBitmapCodec>(rtxn)? {
|
2020-09-07 15:26:42 +02:00
|
|
|
let ((), val) = result?;
|
|
|
|
values_length.push(val.len() as u32);
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let values_length_sum = values_length.into_iter().map(|c| c as usize).sum::<usize>() as f64;
|
|
|
|
let count = count as f64;
|
|
|
|
|
|
|
|
println!("average number of positions by word: {}", values_length_sum / count);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-09-22 13:52:24 +02:00
|
|
|
|
2020-10-02 16:46:05 +02:00
|
|
|
fn size_of_database(index: &Index, rtxn: &heed::RoTxn, name: &str) -> anyhow::Result<()> {
|
|
|
|
use heed::types::ByteSlice;
|
|
|
|
|
|
|
|
let database = match name {
|
|
|
|
MAIN_DB_NAME => &index.main,
|
|
|
|
WORD_DOCIDS_DB_NAME => index.word_docids.as_polymorph(),
|
|
|
|
DOCID_WORD_POSITIONS_DB_NAME => index.docid_word_positions.as_polymorph(),
|
|
|
|
WORD_PAIR_PROXIMITY_DOCIDS_DB_NAME => index.word_pair_proximity_docids.as_polymorph(),
|
|
|
|
DOCUMENTS_DB_NAME => index.documents.as_polymorph(),
|
|
|
|
otherwise => anyhow::bail!("unknown database {:?}", otherwise),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut key_size: u64 = 0;
|
|
|
|
let mut val_size: u64 = 0;
|
|
|
|
for result in database.iter::<_, ByteSlice, ByteSlice>(rtxn)? {
|
|
|
|
let (k, v) = result?;
|
|
|
|
key_size += k.len() as u64;
|
|
|
|
val_size += v.len() as u64;
|
|
|
|
}
|
|
|
|
|
|
|
|
eprintln!("The {} database weigh {} bytes in terms of keys and {} bytes in terms of values.",
|
|
|
|
name, key_size, val_size,
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-09-29 18:32:48 +02:00
|
|
|
fn average_number_of_document_by_word_pair_proximity(
|
|
|
|
index: &Index,
|
|
|
|
rtxn: &heed::RoTxn,
|
|
|
|
) -> anyhow::Result<()>
|
|
|
|
{
|
|
|
|
use heed::types::DecodeIgnore;
|
|
|
|
use milli::RoaringBitmapCodec;
|
|
|
|
|
2020-09-30 17:41:54 +02:00
|
|
|
let mut values_length_sum = 0;
|
2020-09-29 18:32:48 +02:00
|
|
|
let mut count = 0;
|
|
|
|
|
|
|
|
let db = index.word_pair_proximity_docids.as_polymorph();
|
|
|
|
for result in db.iter::<_, DecodeIgnore, RoaringBitmapCodec>(rtxn)? {
|
|
|
|
let ((), val) = result?;
|
2020-09-30 17:41:54 +02:00
|
|
|
values_length_sum += val.len() as u64;
|
2020-09-29 18:32:48 +02:00
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
|
2020-09-30 17:41:54 +02:00
|
|
|
let values_length_sum = values_length_sum as f64;
|
2020-09-29 18:32:48 +02:00
|
|
|
let count = count as f64;
|
|
|
|
println!("average number of documents by words pairs proximities: {}", values_length_sum / count);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-09-30 17:41:54 +02:00
|
|
|
fn word_pair_proximity_stats(index: &Index, rtxn: &heed::RoTxn) -> anyhow::Result<()> {
|
|
|
|
use heed::types::DecodeIgnore;
|
|
|
|
use milli::RoaringBitmapCodec;
|
|
|
|
|
|
|
|
let mut values_length = Vec::new();
|
|
|
|
|
|
|
|
let db = index.word_pair_proximity_docids.as_polymorph();
|
|
|
|
for result in db.iter::<_, DecodeIgnore, RoaringBitmapCodec>(rtxn)? {
|
|
|
|
let ((), val) = result?;
|
|
|
|
values_length.push(val.len() as u32);
|
|
|
|
}
|
|
|
|
|
|
|
|
values_length.sort_unstable();
|
|
|
|
|
|
|
|
let median = values_length.get(values_length.len() / 2).unwrap_or(&0);
|
|
|
|
let first_quartile = values_length.get(values_length.len() / 4).unwrap_or(&0);
|
|
|
|
let third_quartile = values_length.get(values_length.len() / 4 * 3).unwrap_or(&0);
|
|
|
|
let ninety_percentile = values_length.get(values_length.len() / 100 * 90).unwrap_or(&0);
|
|
|
|
let ninety_five_percentile = values_length.get(values_length.len() / 100 * 95).unwrap_or(&0);
|
|
|
|
let ninety_nine_percentile = values_length.get(values_length.len() / 100 * 99).unwrap_or(&0);
|
|
|
|
let minimum = values_length.first().unwrap_or(&0);
|
|
|
|
let maximum = values_length.last().unwrap_or(&0);
|
|
|
|
let count = values_length.len();
|
|
|
|
|
|
|
|
println!("words pairs proximities stats");
|
|
|
|
println!("\tnumber of proximity pairs: {}", count);
|
|
|
|
println!("\tfirst quartile: {}", first_quartile);
|
|
|
|
println!("\tmedian: {}", median);
|
|
|
|
println!("\tthird quartile: {}", third_quartile);
|
|
|
|
println!("\t90th percentile: {}", ninety_percentile);
|
|
|
|
println!("\t95th percentile: {}", ninety_five_percentile);
|
|
|
|
println!("\t99th percentile: {}", ninety_nine_percentile);
|
|
|
|
println!("\tminimum: {}", minimum);
|
|
|
|
println!("\tmaximum: {}", maximum);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-09-22 13:52:24 +02:00
|
|
|
fn word_pair_proximities_docids(
|
|
|
|
index: &Index,
|
|
|
|
rtxn: &heed::RoTxn,
|
|
|
|
debug: bool,
|
|
|
|
word1: String,
|
|
|
|
word2: String,
|
|
|
|
) -> anyhow::Result<()>
|
|
|
|
{
|
|
|
|
use heed::types::ByteSlice;
|
|
|
|
use milli::RoaringBitmapCodec;
|
|
|
|
|
|
|
|
let stdout = io::stdout();
|
|
|
|
let mut wtr = csv::Writer::from_writer(stdout.lock());
|
|
|
|
wtr.write_record(&["word1", "word2", "proximity", "documents_ids"])?;
|
|
|
|
|
|
|
|
// Create the prefix key with only the pair of words.
|
2020-09-22 14:49:22 +02:00
|
|
|
let mut prefix = Vec::with_capacity(word1.len() + word2.len() + 1);
|
|
|
|
prefix.extend_from_slice(word1.as_bytes());
|
2020-09-22 13:52:24 +02:00
|
|
|
prefix.push(0);
|
2020-09-22 14:49:22 +02:00
|
|
|
prefix.extend_from_slice(word2.as_bytes());
|
2020-09-22 13:52:24 +02:00
|
|
|
|
|
|
|
let db = index.word_pair_proximity_docids.as_polymorph();
|
|
|
|
let iter = db.prefix_iter::<_, ByteSlice, RoaringBitmapCodec>(rtxn, &prefix)?;
|
|
|
|
for result in iter {
|
|
|
|
let (key, docids) = result?;
|
|
|
|
|
|
|
|
// Skip keys that are longer than the requested one,
|
|
|
|
// a longer key means that the second word is a prefix of the request word.
|
|
|
|
if key.len() != prefix.len() + 1 { continue; }
|
|
|
|
|
|
|
|
let proximity = key.last().unwrap();
|
|
|
|
let docids = if debug {
|
|
|
|
format!("{:?}", docids)
|
|
|
|
} else {
|
|
|
|
format!("{:?}", docids.iter().collect::<Vec<_>>())
|
|
|
|
};
|
2020-09-22 14:49:22 +02:00
|
|
|
wtr.write_record(&[&word1, &word2, &proximity.to_string(), &docids])?;
|
2020-09-22 13:52:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(wtr.flush()?)
|
|
|
|
}
|