Skip to content

Commit 1159247

Browse files
authored
Merge pull request #17 from tomschelsen/dry
Refactor: Avoid repeated vocabulary processing
2 parents 537f2e9 + cc45a4e commit 1159247

2 files changed

Lines changed: 34 additions & 47 deletions

File tree

src/main.rs

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,63 +23,21 @@ fn main() {
2323
// Mock input - test conversational format
2424
let string = String::from("User: How do mountains form?");
2525

26-
// Extract all unique words from training data to create vocabulary
27-
let mut vocab_set = std::collections::HashSet::new();
28-
29-
// Add end of sequence token
30-
vocab_set.insert("</s>".to_string());
31-
3226
let dataset = Dataset::new(
3327
String::from("data/pretraining_data.json"),
3428
String::from("data/chat_training_data.json"),
3529
DatasetType::JSON,
3630
); // Placeholder, not used in this example
3731

32+
// Extract all unique words from training data to create vocabulary
33+
let mut vocab_set = std::collections::HashSet::new();
34+
3835
// Process all training examples for vocabulary
3936
// First process pre-training data
40-
for text in &dataset.pretraining_data {
41-
for word in text.split_whitespace() {
42-
// Handle punctuation by splitting it from words
43-
let mut current = String::new();
44-
for c in word.chars() {
45-
if c.is_ascii_punctuation() {
46-
if !current.is_empty() {
47-
vocab_set.insert(current.clone());
48-
current.clear();
49-
}
50-
vocab_set.insert(c.to_string());
51-
} else {
52-
current.push(c);
53-
}
54-
}
55-
if !current.is_empty() {
56-
vocab_set.insert(current);
57-
}
58-
}
59-
}
37+
Vocab::process_text_for_vocab(&dataset.pretraining_data, &mut vocab_set);
6038

6139
// Then process chat training data
62-
for row in &dataset.chat_training_data {
63-
// Add words from outputs
64-
for word in row.split_whitespace() {
65-
// Handle punctuation by splitting it from words
66-
let mut current = String::new();
67-
for c in word.chars() {
68-
if c.is_ascii_punctuation() {
69-
if !current.is_empty() {
70-
vocab_set.insert(current.clone()); // Clone to avoid moving
71-
current.clear(); // Use clear() instead of String::new()
72-
}
73-
vocab_set.insert(c.to_string());
74-
} else {
75-
current.push(c);
76-
}
77-
}
78-
if !current.is_empty() {
79-
vocab_set.insert(current);
80-
}
81-
}
82-
}
40+
Vocab::process_text_for_vocab(&dataset.chat_training_data, &mut vocab_set);
8341

8442
let mut vocab_words: Vec<String> = vocab_set.into_iter().collect();
8543
vocab_words.sort(); // Sort for deterministic ordering

src/vocab.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use bincode::Encode;
22
use std::collections::HashMap;
3+
use std::collections::HashSet;
34

45
#[derive(Clone, Encode)]
56
pub struct Vocab {
@@ -46,6 +47,34 @@ impl Vocab {
4647
pub fn default_words() -> Vec<&'static str> {
4748
vec!["hello", "world", "this", "is", "rust", "</s>"]
4849
}
50+
51+
/// Process text data to extract vocabulary words and add them to the vocabulary set
52+
pub fn process_text_for_vocab(texts: &[String], vocab_set: &mut HashSet<String>) {
53+
// Add end of sequence token
54+
vocab_set.insert("</s>".to_string());
55+
56+
// Process all training examples for vocabulary
57+
for text in texts {
58+
for word in text.split_whitespace() {
59+
// Handle punctuation by splitting it from words
60+
let mut current = String::new();
61+
for c in word.chars() {
62+
if c.is_ascii_punctuation() {
63+
if !current.is_empty() {
64+
vocab_set.insert(current.clone());
65+
current.clear();
66+
}
67+
vocab_set.insert(c.to_string());
68+
} else {
69+
current.push(c);
70+
}
71+
}
72+
if !current.is_empty() {
73+
vocab_set.insert(current);
74+
}
75+
}
76+
}
77+
}
4978
}
5079

5180
impl Into<String> for Vocab {

0 commit comments

Comments
 (0)