Update to utilize arpabet.rs

This commit is contained in:
Brandon Thomas committed 2018-06-11 02:24:39 -04:00
1 parent 5f9d50c154
commit b68009408c
10 files changed
+132 -120

No files matched your search

-91
View File
@@ -1,91 +0,0 @@
// Copyright (c) 2016 Brandon Thomas <bt@brand.io, echelon@gmail.com>
use super::dictionary::Dictionary;
use error::SynthError;
use regex::Regex;
use std::collections::HashMap;
use std::collections::HashSet;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
pub type Word = String;
pub type Phoneme = String;
pub type Polyphone = Vec<Phoneme>;
pub struct Arpabet {
/// A map of lowercase words to polyphone breakdown (phones are uppercase).
/// eg. 'jungle' -> [JH, AH1, NG, G, AH0, L]
dictionary: HashMap<Word, Polyphone>
}
impl Arpabet {
/// Read CMU's Arpabet Dictionary.
/// See http://www.speech.cs.cmu.edu/cgi-bin/cmudict
pub fn load_from_file(filename: &str) -> Result<Arpabet, SynthError> {
let f = try!(File::open(filename));
let mut reader = BufReader::new(f);
let mut map = HashMap::new();
let mut buffer = String::new();
// Format resembles the following,
// ABBREVIATE AH0 B R IY1 V IY0 EY2 T
let re = Regex::new(r"^([\w-']+)\s+(.*)\n$").unwrap();
while try!(reader.read_line(&mut buffer)) > 0 {
match re.captures(&buffer) {
None => {},
Some(caps) => {
let word_match = caps.at(1);
let phonemes_match = caps.at(2);
if word_match.is_some() && phonemes_match.is_some() {
let word = word_match.unwrap().to_lowercase();
let split = phonemes_match.unwrap().split(" ");
let v1 = split.collect::<Vec<&str>>();
let v2 = v1.iter().map(|s| s.to_string()).collect::<Vec<String>>();
map.insert(word, v2);
}
},
}
buffer.clear();
}
if map.is_empty() {
Err(SynthError::EmptyFile)
} else {
Ok(Arpabet { dictionary: map })
}
}
/// Get a polyphone from the dictionary.
pub fn get_polyphone(&self, word: &str) -> Option<Polyphone> {
self.dictionary.get(word).and_then(|p| {
Some(p.iter().map(|s| s.to_string()).collect::<Vec<String>>())
})
}
/// Combine two dictionaries into a new one.
pub fn combine(&self, other: &Arpabet) -> Arpabet {
let mut combined = self.dictionary.clone();
for (k, v) in other.dictionary.iter() {
combined.insert(k.clone(), v.clone());
}
Arpabet {
dictionary: combined
}
}
/// Extract and return the internal word dictionary (a list of all
/// the words contained in the arpabet).
pub fn to_dictionary(&self) -> Dictionary {
let mut words = HashSet::new();
for word in self.dictionary.keys() {
words.insert(word.to_string());
}
Dictionary::new(words)
}
}
+13 -1
View File
@@ -1,8 +1,9 @@
// Copyright (c) 2016 Brandon Thomas <bt@brand.io, echelon@gmail.com>
use arpabet::Arpabet;
use speaker::Speaker;
use std::collections::HashSet;
use std::collections::HashMap;
use std::collections::HashSet;
pub type Word = String;
@@ -27,6 +28,17 @@ impl Dictionary {
Dictionary { words: HashSet::new() }
}
/// Extract and return the internal word dictionary (a list of all
/// the words contained in the arpabet).
pub fn from_arpabet(arpabet: &Arpabet) -> Dictionary {
let mut words = HashSet::new();
for word in arpabet.keys() {
words.insert(word.to_string());
}
Dictionary::new(words)
}
/// Returns true if the dictionary contains the word.
pub fn contains(&self, word: &str) -> bool {
self.words.contains(word)
-1
View File
@@ -1,7 +1,6 @@
// Copyright (c) 2016 Brandon Thomas <bt@brand.io, echelon@gmail.com>
pub mod abbr;
pub mod arpabet;
pub mod dates;
pub mod dictionary;
pub mod numbers;
+11 -7
View File
@@ -3,6 +3,7 @@
#[macro_use] extern crate lazy_static;
#[macro_use] extern crate log;
extern crate arpabet;
extern crate chrono;
extern crate clap;
extern crate crypto;
@@ -27,6 +28,7 @@ pub mod old_dictionary;
pub mod speaker;
pub mod synthesis;
use arpabet::Arpabet;
use clap::{App, Arg, ArgMatches};
use config::Config;
use handlers::audio_synth_handler::AudioSynthHandler;
@@ -35,7 +37,7 @@ use handlers::html_handler::HtmlHandler;
use handlers::vocab_list_handler::VocabListHandler;
use iron::prelude::*;
use lang::abbr::AbbreviationsMap;
use lang::arpabet::Arpabet;
use lang::dictionary::Dictionary;
use lang::dictionary::UniversalDictionary;
use lang::parser::Parser;
use lang::tokenizer::Tokenizer;
@@ -145,23 +147,25 @@ fn get_hostname() {
fn create_parser_and_synthesizer(config: &Config) -> (Parser, Synthesizer) {
info!("Reading Arpabet Dictionary...");
let arpabet_dictionary = Arpabet::load_from_file(
&config.phoneme_dictionary_file.clone().unwrap()).unwrap();
let cmudict = Arpabet::load_cmudict();
info!("Reading Extra Dictionary...");
let extra_dictionary = Arpabet::load_from_file(
&config.extra_dictionary_file.clone().unwrap()).unwrap();
&config.extra_dictionary_file.clone().unwrap())
.expect("Error loading extra dictionary");
info!("Reading Square Dictionary...");
let square_dictionary = Arpabet::load_from_file(
&config.square_dictionary_file.clone().unwrap()).unwrap();
&config.square_dictionary_file.clone().unwrap())
.expect("Error reading square dictionary");
let arpabet = arpabet_dictionary
let arpabet = cmudict
.combine(&extra_dictionary)
.combine(&square_dictionary);
let mut dictionary = UniversalDictionary::new();
dictionary.set_arpabet_dictionary(arpabet.to_dictionary());
let arpa_dict = Dictionary::from_arpabet(&arpabet);
dictionary.set_arpabet_dictionary(arpa_dict);
let dictionary = Arc::new(dictionary);
+1 -1
View File
@@ -1,8 +1,8 @@
// Copyright (c) 2016 Brandon Thomas <bt@brand.io, echelon@gmail.com>
use arpabet::Arpabet;
use hound::WavSpec;
use hound::WavWriter;
use lang::arpabet::Arpabet;
use speaker::Speaker;
use std::io::BufWriter;
use std::io::Cursor;
+1 -1
View File
@@ -1,6 +1,6 @@
// Copyright (c) 2016 Brandon Thomas <bt@brand.io, echelon@gmail.com>
use lang::arpabet::Arpabet;
use arpabet::Arpabet;
use lang::syllables::Polysyllabic;
use lang::syllables::Polysyllable;
use lang::syllables::arpabet_to_syllables;