|
use super::{AnalyticsDirectory, AnalyticsMode, BatchProgramAnalyzerPlugin, BatchProgramAnalyzerContext}; |
|
use crate::common::create_csv_file; |
|
use crate::common::RecordBigram; |
|
use crate::common::RecordTrigram; |
|
use crate::common::RecordSkipgram; |
|
use crate::common::RecordUnigram; |
|
use loda_rust_core; |
|
use loda_rust_core::parser::ParameterType; |
|
use loda_rust_core::parser::{InstructionId, ParsedProgram}; |
|
use std::path::PathBuf; |
|
use std::error::Error; |
|
use std::collections::HashMap; |
|
|
|
type HistogramBigramKey = (String,String); |
|
type HistogramTrigramKey = (String,String,String); |
|
type HistogramSkipgramKey = (String,String); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct AnalyzeLineNgram { |
|
analytics_directory: AnalyticsDirectory, |
|
limit_lower: i64, |
|
limit_upper: i64, |
|
histogram_unigram: HashMap<String,u32>, |
|
histogram_bigram: HashMap<HistogramBigramKey,u32>, |
|
histogram_trigram: HashMap<HistogramTrigramKey,u32>, |
|
histogram_skipgram: HashMap<HistogramSkipgramKey,u32>, |
|
ignore_count: usize, |
|
} |
|
|
|
impl AnalyzeLineNgram { |
|
pub fn new(analytics_directory: AnalyticsDirectory, mode: AnalyticsMode) -> Self { |
|
match mode { |
|
AnalyticsMode::OEIS => { |
|
|
|
|
|
|
|
let limit_lower: i64 = -100; |
|
let limit_upper: i64 = 100; |
|
return Self::create(analytics_directory, limit_lower, limit_upper); |
|
}, |
|
AnalyticsMode::ARC => { |
|
|
|
let limit_lower: i64 = -100; |
|
|
|
|
|
|
|
|
|
let limit_upper: i64 = 200; |
|
return Self::create(analytics_directory, limit_lower, limit_upper); |
|
}, |
|
} |
|
} |
|
|
|
fn create(analytics_directory: AnalyticsDirectory, limit_lower: i64, limit_upper: i64) -> Self { |
|
Self { |
|
analytics_directory, |
|
limit_lower, |
|
limit_upper, |
|
histogram_unigram: HashMap::new(), |
|
histogram_bigram: HashMap::new(), |
|
histogram_trigram: HashMap::new(), |
|
histogram_skipgram: HashMap::new(), |
|
ignore_count: 0, |
|
} |
|
} |
|
|
|
fn extract_words(&mut self, parsed_program: &ParsedProgram) -> Vec<String> { |
|
let mut words: Vec<String> = vec!(); |
|
words.push("START".to_string()); |
|
for instruction in &parsed_program.instruction_vec { |
|
|
|
|
|
|
|
|
|
let should_reject_extreme_source_constant: bool = match instruction.instruction_id { |
|
InstructionId::EvalSequence | |
|
InstructionId::UnofficialFunction { .. } => false, |
|
_ => true |
|
}; |
|
if should_reject_extreme_source_constant { |
|
if instruction.parameter_vec.len() == 2 { |
|
if let Some(parameter) = instruction.parameter_vec.last() { |
|
if parameter.parameter_type == ParameterType::Constant { |
|
if parameter.parameter_value < self.limit_lower { |
|
debug!("Encountered a magic value that is lower than {}. Ignoring program. Instruction: {}", self.limit_lower, instruction); |
|
self.ignore_count += 1; |
|
return vec!(); |
|
} |
|
if parameter.parameter_value > self.limit_upper { |
|
debug!("Encountered a magic value that is higher than {}. Ignoring program. Instruction: {}", self.limit_upper, instruction); |
|
self.ignore_count += 1; |
|
return vec!(); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
let word: String = format!("{}", instruction); |
|
words.push(word); |
|
} |
|
words.push("STOP".to_string()); |
|
words |
|
} |
|
|
|
fn populate_unigram(&mut self, words: &Vec<String>) { |
|
let keys: Vec<String> = words.clone(); |
|
for key in keys { |
|
let counter = self.histogram_unigram.entry(key).or_insert(0); |
|
*counter += 1; |
|
} |
|
} |
|
|
|
fn populate_bigram(&mut self, words: &Vec<String>) { |
|
let mut keys = Vec::<HistogramBigramKey>::new(); |
|
let mut prev_word = String::new(); |
|
for (index, word1) in words.iter().enumerate() { |
|
let word0: String = prev_word; |
|
prev_word = word1.clone(); |
|
if index == 0 { |
|
continue; |
|
} |
|
let key: HistogramBigramKey = (word0, word1.clone()); |
|
keys.push(key); |
|
} |
|
for key in keys { |
|
let counter = self.histogram_bigram.entry(key).or_insert(0); |
|
*counter += 1; |
|
} |
|
} |
|
|
|
fn populate_trigram(&mut self, words: &Vec<String>) { |
|
let mut keys = Vec::<HistogramTrigramKey>::new(); |
|
let mut prev_prev_word = String::new(); |
|
let mut prev_word = String::new(); |
|
for (index, word2) in words.iter().enumerate() { |
|
let word0: String = prev_prev_word; |
|
let word1: String = prev_word.clone(); |
|
prev_prev_word = prev_word; |
|
prev_word = word2.clone(); |
|
if index < 2 { |
|
continue; |
|
} |
|
let key: HistogramTrigramKey = (word0, word1, word2.clone()); |
|
keys.push(key); |
|
} |
|
for key in keys { |
|
let counter = self.histogram_trigram.entry(key).or_insert(0); |
|
*counter += 1; |
|
} |
|
} |
|
|
|
fn populate_skipgram(&mut self, words: &Vec<String>) { |
|
let mut keys = Vec::<HistogramSkipgramKey>::new(); |
|
let mut prev_prev_word = String::new(); |
|
let mut prev_word = String::new(); |
|
for (index, word2) in words.iter().enumerate() { |
|
let word0: String = prev_prev_word; |
|
prev_prev_word = prev_word; |
|
prev_word = word2.clone(); |
|
if index < 2 { |
|
continue; |
|
} |
|
let key: HistogramSkipgramKey = (word0, word2.clone()); |
|
keys.push(key); |
|
} |
|
for key in keys { |
|
let counter = self.histogram_skipgram.entry(key).or_insert(0); |
|
*counter += 1; |
|
} |
|
} |
|
|
|
fn save_unigram(&self) -> Result<(), Box<dyn Error>> { |
|
|
|
let mut records = Vec::<RecordUnigram>::new(); |
|
for (histogram_key, histogram_count) in &self.histogram_unigram { |
|
let record = RecordUnigram { |
|
count: *histogram_count, |
|
word: histogram_key.clone(), |
|
}; |
|
records.push(record); |
|
} |
|
|
|
|
|
|
|
records.sort_unstable_by_key(|item| (item.count, item.word.clone())); |
|
records.reverse(); |
|
|
|
|
|
let output_path: PathBuf = self.analytics_directory.histogram_line_unigram_file(); |
|
create_csv_file(&records, &output_path) |
|
} |
|
|
|
fn save_bigram(&self) -> Result<(), Box<dyn Error>> { |
|
|
|
let mut records = Vec::<RecordBigram>::new(); |
|
for (histogram_key, histogram_count) in &self.histogram_bigram { |
|
let record = RecordBigram { |
|
count: *histogram_count, |
|
word0: histogram_key.0.clone(), |
|
word1: histogram_key.1.clone() |
|
}; |
|
records.push(record); |
|
} |
|
|
|
|
|
|
|
records.sort_unstable_by_key(|item| (item.count, item.word0.clone(), item.word1.clone())); |
|
records.reverse(); |
|
|
|
|
|
let output_path: PathBuf = self.analytics_directory.histogram_line_bigram_file(); |
|
create_csv_file(&records, &output_path) |
|
} |
|
|
|
fn save_trigram(&self) -> Result<(), Box<dyn Error>> { |
|
|
|
let mut records = Vec::<RecordTrigram>::new(); |
|
for (histogram_key, histogram_count) in &self.histogram_trigram { |
|
let record = RecordTrigram { |
|
count: *histogram_count, |
|
word0: histogram_key.0.clone(), |
|
word1: histogram_key.1.clone(), |
|
word2: histogram_key.2.clone() |
|
}; |
|
records.push(record); |
|
} |
|
|
|
|
|
|
|
records.sort_unstable_by_key(|item| (item.count, item.word0.clone(), item.word1.clone(), item.word2.clone())); |
|
records.reverse(); |
|
|
|
|
|
let output_path: PathBuf = self.analytics_directory.histogram_line_trigram_file(); |
|
create_csv_file(&records, &output_path) |
|
} |
|
|
|
fn save_skipgram(&self) -> Result<(), Box<dyn Error>> { |
|
|
|
let mut records = Vec::<RecordSkipgram>::new(); |
|
for (histogram_key, histogram_count) in &self.histogram_skipgram { |
|
let record = RecordSkipgram { |
|
count: *histogram_count, |
|
word0: histogram_key.0.clone(), |
|
word2: histogram_key.1.clone() |
|
}; |
|
records.push(record); |
|
} |
|
|
|
|
|
|
|
records.sort_unstable_by_key(|item| (item.count, item.word0.clone(), item.word2.clone())); |
|
records.reverse(); |
|
|
|
|
|
let output_path: PathBuf = self.analytics_directory.histogram_line_skipgram_file(); |
|
create_csv_file(&records, &output_path) |
|
} |
|
} |
|
|
|
impl BatchProgramAnalyzerPlugin for AnalyzeLineNgram { |
|
fn plugin_name(&self) -> &'static str { |
|
"AnalyzeLineNgram" |
|
} |
|
|
|
fn analyze(&mut self, context: &BatchProgramAnalyzerContext) -> Result<(), Box<dyn Error>> { |
|
let words: Vec<String> = self.extract_words(&context.parsed_program); |
|
self.populate_unigram(&words); |
|
self.populate_bigram(&words); |
|
self.populate_trigram(&words); |
|
self.populate_skipgram(&words); |
|
Ok(()) |
|
} |
|
|
|
fn save(&self) -> Result<(), Box<dyn Error>> { |
|
self.save_unigram()?; |
|
self.save_bigram()?; |
|
self.save_trigram()?; |
|
self.save_skipgram()?; |
|
Ok(()) |
|
} |
|
|
|
fn human_readable_summary(&self) -> String { |
|
let items: Vec<String> = vec![ |
|
format!("unigram: {:?}", self.histogram_unigram.len()), |
|
format!("bigram: {:?}", self.histogram_bigram.len()), |
|
format!("trigram: {:?}", self.histogram_trigram.len()), |
|
format!("skipgram: {:?}", self.histogram_skipgram.len()), |
|
format!("ignore count: {:?}", self.ignore_count), |
|
]; |
|
items.join(", ") |
|
} |
|
} |
|
|