それで、私は Python で書いた文字列トークナイザーを Rust に移植することに取り組んでいますが、ライフタイムと構造体で乗り越えられないように見える問題に遭遇しました。
したがって、プロセスは基本的に次のとおりです。
- ファイルの配列を取得する
- 各ファイルを
Vec<String>
トークンの - ユーザー a
Counter
とUnicase
、それぞれからトークンの個々のインスタンスの数を取得するvec
- そのカウントを他のデータとともに構造体に保存します
- (将来) ファイルごとのデータと一緒に合計データを蓄積するために、一連の構造体に対して何らかの処理を行う
struct Corpus<'a> {
words: Counter<UniCase<&'a String>>,
parts: Vec<CorpusPart<'a>>
}
pub struct CorpusPart<'a> {
percent_of_total: f32,
word_count: usize,
words: Counter<UniCase<&'a String>>
}
fn process_file(entry: &DirEntry) -> CorpusPart {
let mut contents = read_to_string(entry.path())
.expect("Could not load contents.");
let tokens = tokenize(&mut contents);
let counted_words = collect(&tokens);
CorpusPart {
percent_of_total: 0.0,
word_count: tokens.len(),
words: counted_words
}
}
pub fn tokenize(normalized: &mut String) -> Vec<String> {
// snip ...
}
pub fn collect(results: &Vec<String>) -> Counter<UniCase<&'_ String>> {
results.iter()
.map(|w| UniCase::new(w))
.collect::<Counter<_>>()
}
ただし、返そうとCorpusPart
すると、ローカル変数を参照しようとしていると不平を言いますtokens
。これにどのように対処できますか/対処する必要がありますか? 生涯アノテーションを追加しようとしましたが、わかりませんでした...
基本的に、 はもう必要ありませんが、カウンター用に含まれていた のVec<String>
一部が必要です。String
どんな助けでも大歓迎です、ありがとう!