-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: created langauge detection demo page
- Loading branch information
1 parent
9713f5d
commit 178bfe0
Showing
7 changed files
with
71 additions
and
5 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
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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use crate::{language_detection::is_likely_english, Dictionary}; | ||
|
||
use super::{Parser, Token, TokenStringExt}; | ||
|
||
/// A parser that wraps another, using heuristics to quickly redact paragraphs of a document that aren't | ||
/// intended to be English text. | ||
pub struct IsolateEnglish<D: Dictionary> { | ||
inner: Box<dyn Parser>, | ||
dict: D, | ||
} | ||
|
||
impl<D: Dictionary> IsolateEnglish<D> { | ||
pub fn new(inner: Box<dyn Parser>, dictionary: D) -> Self { | ||
Self { | ||
inner, | ||
dict: dictionary, | ||
} | ||
} | ||
} | ||
|
||
impl<D: Dictionary> Parser for IsolateEnglish<D> { | ||
fn parse(&mut self, source: &[char]) -> Vec<Token> { | ||
let tokens = self.inner.parse(source); | ||
|
||
let mut english_tokens: Vec<Token> = Vec::with_capacity(tokens.len()); | ||
|
||
for sentence in tokens.iter_sentences() { | ||
if sentence.len() > 5 && is_likely_english(sentence, source, &self.dict) { | ||
english_tokens.extend(sentence); | ||
} | ||
} | ||
|
||
english_tokens | ||
} | ||
} |
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
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
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
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