-
Notifications
You must be signed in to change notification settings - Fork 39
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
1 changed file
with
39 additions
and
33 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
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")) | ||
} | ||
} |