Skip to content

Commit

Permalink
index init
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfran committed Nov 26, 2023
1 parent 9718be5 commit 8ec0403
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 24 deletions.
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@
name = "search-rs"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
48 changes: 48 additions & 0 deletions src/index.rs
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())
}
}
17 changes: 2 additions & 15 deletions src/main.rs
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() {}
8 changes: 3 additions & 5 deletions src/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Trie {

pub fn get_by_prefix(&self, prefix: &str) -> Vec<i32> {
self.get_internal(prefix)
.map_or_else(|| Vec::new(), |n| self.visit(n))
.map_or_else(Vec::new, |n| self.visit(n))
}

fn get_internal(&self, word: &str) -> Option<&Node> {
Expand Down Expand Up @@ -76,19 +76,17 @@ mod tests {
#[test]
fn test_insert_and_get() {
let mut trie = Trie::new();

trie.insert("hello", 42);

assert_eq!(trie.get("hello"), Some(42));
assert_eq!(trie.get("world"), None);
}

// Add more tests...

#[test]
fn test_get_by_prefix() {
let mut trie = Trie::new();

trie.insert("hello", 42);
trie.insert("help", 99);
trie.insert("world", 123);
Expand Down
42 changes: 42 additions & 0 deletions test_data/doc.txt
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.

0 comments on commit 8ec0403

Please sign in to comment.