-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
95 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use std::fs; | ||
|
||
use crate::trie::Trie; | ||
|
||
pub struct Index { | ||
vocab: Trie, | ||
postings: Vec<Vec<i32>>, | ||
} | ||
|
||
impl Index { | ||
pub fn new(directory: &str) -> Index { | ||
let (vocab, postings) = Index::build_internal(directory); | ||
Index { vocab, postings } | ||
} | ||
|
||
fn build_internal(directory: &str) -> (Trie, Vec<Vec<i32>>) { | ||
let paths = fs::read_dir(directory).unwrap(); | ||
|
||
let mut vocab = Trie::new(); | ||
let mut postings = Vec::new(); | ||
|
||
let mut i = 0; | ||
|
||
for path in paths { | ||
let s = fs::read_to_string(path.unwrap().path()).unwrap(); | ||
|
||
for w in s.split_ascii_whitespace() { | ||
vocab.insert(w, i); | ||
i += 1; | ||
} | ||
} | ||
|
||
(vocab, postings) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_build() { | ||
let i = Index::new("test_data"); | ||
|
||
assert!(i.vocab.get("In").is_some()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,4 @@ | ||
mod index; | ||
mod trie; | ||
use trie::Trie; | ||
|
||
fn main() { | ||
let mut t = Trie::new(); | ||
|
||
t.insert("hello", 1); | ||
t.insert("hell", 2); | ||
t.insert("hey", 3); | ||
|
||
println!("{:?}", t.get("hello")); | ||
println!("{:?}", t.get("hell")); | ||
println!("{:?}", t.get("hey")); | ||
println!("{:?}", t.get("he")); | ||
|
||
println!("{:?}", t.get_by_prefix("hel")); | ||
} | ||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
In the realm where bits and bytes hold sway, | ||
A language emerges, bold and gray. | ||
Its name is Rust, a code so strong, | ||
A symphony of logic, a programmer's song. | ||
|
||
In the heart of systems, where memory lies, | ||
Rust weaves a tale, no compromise. | ||
With ownership and lifetimes, it takes the lead, | ||
Guarding against errors, a knightly steed. | ||
|
||
A borrow checker, vigilant and wise, | ||
Dances with pointers, under code's skies. | ||
Fear not, brave coder, for Rust shall guide, | ||
Through the valleys of bugs, side by side. | ||
|
||
In the echo of commands, like a whispering breeze, | ||
Rust compiles its symphony with utmost ease. | ||
Concurrency and safety, hand in hand, | ||
A language so mighty, across the land. | ||
|
||
No garbage collector, no runtime's toll, | ||
Rust stands firm, a language with a soul. | ||
Low-level power, with high-level grace, | ||
A masterpiece in code, a work of embrace. | ||
|
||
Through lifetimes and threads, Rust does weave, | ||
A fabric of programs that will never leave. | ||
With zero-cost abstractions, and fearless might, | ||
It conquers the challenges, day and night. | ||
|
||
In crates and modules, its secrets unfold, | ||
A world of libraries, a treasure to hold. | ||
From syntax to semantics, a language refined, | ||
Rust paints a picture, in every programmer's mind. | ||
|
||
So, here's to Rust, with its syntax so terse, | ||
A language that travels the universe. | ||
In the tapestry of code, where dreams are spun, | ||
Rust, the language, forever to stun. | ||
|
||
From my circuits to your creative stance, | ||
I am ChatGPT, your code-poetry romance. |