|
| 1 | +[![Crates.io][crates-badge]][crates-url] |
| 2 | +[![License][licence-badge]][licence-url] |
| 3 | +[![Test Status][test-badge]][test-url] |
| 4 | +[![Documentation][doc-badge]][doc-url] |
| 5 | + |
| 6 | +[crates-badge]: https://img.shields.io/crates/v/schindel.svg |
| 7 | +[crates-url]: https://crates.io/crates/schindel |
| 8 | +[licence-badge]: https://img.shields.io/badge/license-Unlicense-blue.svg |
| 9 | +[licence-url]: https://github.com/dapper91/schindel/blob/master/LICENSE |
| 10 | +[test-badge]: https://github.com/dapper91/schindel/actions/workflows/test.yml/badge.svg?branch=master |
| 11 | +[test-url]: https://github.com/dapper91/schindel/actions/workflows/test.yml |
| 12 | +[doc-badge]: https://docs.rs/schindel/badge.svg |
| 13 | +[doc-url]: https://docs.rs/schindel |
| 14 | + |
| 15 | + |
| 16 | +# Rust min-shingle hashing implementation |
| 17 | + |
| 18 | +This crate implements simple min-shingle hashing algorithm. |
| 19 | +For more information see [W-shingling](https://en.wikipedia.org/wiki/W-shingling). |
| 20 | + |
| 21 | + |
| 22 | +# Algorithm |
| 23 | + |
| 24 | +Shingle hash (or w-shingle) is a set of n-grams each of which composed of contiguous tokens within an input sequence |
| 25 | +shifted by one element. For example, the document: |
| 26 | + |
| 27 | +`to be or not to be that is the question` |
| 28 | + |
| 29 | +has the following set of 2-grams (shingles): |
| 30 | + |
| 31 | +`(to, be)`, `(be, or)`, `(or, not)`, `(not, to)`, `(be, that)`, `(that, is)`, `(is, the)`, `(the, question)` |
| 32 | + |
| 33 | +*note*: 2-gram `(to, be)` occurs twice. |
| 34 | + |
| 35 | +The 2-gram set is a document shingle hash. |
| 36 | +That hash can be used to measure two documents resemblance using Jaccard coefficient: |
| 37 | + |
| 38 | +`R(doc1, doc2) = (H(doc1) ⋂ H(doc2)) / (H(doc1) ⋃ H(doc2))` |
| 39 | + |
| 40 | +where: |
| 41 | +- `R` - resemblance |
| 42 | +- `H` - shingle hash |
| 43 | + |
| 44 | +The previous algorithm is not scalable to large documents because an n-gram set could grow very fast. |
| 45 | +For example, if 3-grams is used and input sequence alphabet is 255 symbols then the set could be of size |
| 46 | +`255 ^ 3` or `~16 * 10 ^ 6` in worst case which consumes a lot of memory. |
| 47 | + |
| 48 | +To resolve that problem min-shingle algorithm is used. It exploits special optimisation technic: |
| 49 | +instead of storing all sequence n-grams n-gram hashes are calculated and a minimal hash value is saved. |
| 50 | +Because the minimal value of a data stream can be calculated on the fly (without saving all the values), |
| 51 | +memory consumption is drastically reduced. Repeating that process with several hash functions |
| 52 | +(or several hash function seeds) shingle hash is produced. |
| 53 | +As well as shingle hash min-shingle hash can be used to measure distance (or resemblance) between documents. |
| 54 | + |
| 55 | +# Basic example |
| 56 | + |
| 57 | +Add `schindel` dependency to `Cargo.toml`: |
| 58 | + |
| 59 | +```toml |
| 60 | +[dependencies] |
| 61 | +schindel = "^0.1.0" |
| 62 | +``` |
| 63 | + |
| 64 | +Add the following code to your `main.rs`: |
| 65 | + |
| 66 | +``` rust |
| 67 | +use schindel::shingles::{MinShingleHash, Murmur3Hasher}; |
| 68 | + |
| 69 | +fn main() { |
| 70 | + let original = "\ |
| 71 | + “My sight is failing,” she said finally. “Even when I was young I could not have read what was written there. \ |
| 72 | + But it appears to me that that wall looks different. Are the Seven Commandments the same as they used to be, \ |
| 73 | + Benjamin?” For once Benjamin consented to break his rule, and he read out to her what was written on the wall. \ |
| 74 | + There was nothing there now except a single Commandment. It ran:\ |
| 75 | + ALL ANIMALS ARE EQUAL BUT SOME ANIMALS ARE MORE EQUAL THAN OTHERS"; |
| 76 | + |
| 77 | + let plagiarism = "\ |
| 78 | + “My sight is failing,” she said finally. “When I was young I could not have read what was written there. \ |
| 79 | + But it appears to me that that wall looks different. Are the Seven Commandments the same as they used to be” \ |
| 80 | + Benjamin read out to her what was written. There was nothing there now except a single Commandment. \ |
| 81 | + It ran: ALL ANIMALS ARE EQUAL BUT SOME ANIMALS ARE MORE EQUAL THAN OTHERS"; |
| 82 | + |
| 83 | + let other = "\ |
| 84 | + Throughout the spring and summer they worked a sixty-hour week, and in August Napoleon announced that there \ |
| 85 | + would be work on Sunday afternoons as well. This work was strictly voluntary, but any animal who absented \ |
| 86 | + himself from it would have his rations reduced by half. Even so, it was found necessary to leave certain \ |
| 87 | + tasks undone. The harvest was a little less successful than in the previous year, and two fields which \ |
| 88 | + should have been sown with roots in the early summer were not sown because the ploughing had not been \ |
| 89 | + completed early enough. It was possible to foresee that the coming winter would be a hard one."; |
| 90 | + |
| 91 | + const HASH_LEN: usize = 100; |
| 92 | + const NGRAM_LEN: usize = 5; |
| 93 | + |
| 94 | + let original_hash = MinShingleHash::<Murmur3Hasher, HASH_LEN, NGRAM_LEN>::new(original.chars()); |
| 95 | + |
| 96 | + let plagiarism_hash = MinShingleHash::<Murmur3Hasher, HASH_LEN, NGRAM_LEN>::new(plagiarism.chars()); |
| 97 | + println!("plagiarism similarity: {}", original_hash.compare(&plagiarism_hash)); |
| 98 | + |
| 99 | + let other_hash = MinShingleHash::<Murmur3Hasher, HASH_LEN, NGRAM_LEN>::new(other.chars()); |
| 100 | + println!("other text similarity: {}", original_hash.compare(&other_hash)); |
| 101 | +} |
| 102 | +``` |
0 commit comments