Skip to content

Commit

Permalink
refactor: clean test code
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 6, 2021
1 parent cfde697 commit d3b0673
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions quake_analysis/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,48 @@
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
fn main() {}

use jieba_rs::Jieba;
use walkdir::{DirEntry, WalkDir};
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;

fn is_markdown(entry: &DirEntry) -> bool {
entry
.file_name()
.to_str()
.map(|s| s.ends_with(".md"))
.unwrap_or(false)
}
use jieba_rs::Jieba;
use walkdir::{DirEntry, WalkDir};

fn main() {
count_by_path(PathBuf::from("examples").join("blog"));
}
fn is_markdown(entry: &DirEntry) -> bool {
entry
.file_name()
.to_str()
.map(|s| s.ends_with(".md"))
.unwrap_or(false)
}

fn count_by_path(path: PathBuf) {
let mut count: HashMap<String, usize> = HashMap::new();
let jieba = Jieba::new();
for entry in WalkDir::new(path).into_iter().filter_map(|e| e.ok()) {
if is_markdown(&entry) {
let content = fs::read_to_string(entry.path()).unwrap();
let x = content.as_str();
let results = jieba.cut(x, true);
let mut one_count: HashMap<String, usize> = HashMap::new();
for result in results {
*one_count.entry(String::from(result)).or_insert(0) += 1;
*count.entry(String::from(result)).or_insert(0) += 1;
}
fn count_by_path(path: PathBuf) {
let mut count: HashMap<String, usize> = HashMap::new();
let jieba = Jieba::new();
for entry in WalkDir::new(path).into_iter().filter_map(|e| e.ok()) {
if is_markdown(&entry) {
let content = fs::read_to_string(entry.path()).unwrap();
let x = content.as_str();
let results = jieba.cut(x, true);
let mut one_count: HashMap<String, usize> = HashMap::new();
for result in results {
*one_count.entry(String::from(result)).or_insert(0) += 1;
*count.entry(String::from(result)).or_insert(0) += 1;
}

let mut count_vec: Vec<_> = one_count.iter().collect();
count_vec.sort_by(|a, b| a.1.cmp(b.1));
println!("{:?}", count_vec);
let mut count_vec: Vec<_> = one_count.iter().collect();
count_vec.sort_by(|a, b| a.1.cmp(b.1));
println!("{:?}", count_vec);
}
}

// let mut count_vec: Vec<_> = count.iter().collect();
// count_vec.sort_by(|a, b| a.1.cmp(b.1));
}

// let mut count_vec: Vec<_> = count.iter().collect();
// count_vec.sort_by(|a, b| a.1.cmp(b.1));
#[test]
fn sample() {
count_by_path(PathBuf::from("examples").join("blog"))
}
}

0 comments on commit d3b0673

Please sign in to comment.