Struct rnltk::sentiment::SentimentModel
source · pub struct SentimentModel { /* private fields */ }
Implementations§
source§impl SentimentModel
impl SentimentModel
sourcepub fn new(custom_words: CustomWords) -> Self
pub fn new(custom_words: CustomWords) -> Self
Creates new instance of SentimentModel from custom_words
, a CustomWords
sentiment lexicon.
Examples
use rnltk::sentiment::{SentimentModel, CustomWords};
let custom_word_dict = r#"
{
"abduction": {
"word": "abduction",
"stem": "abduct",
"avg": [2.76, 5.53],
"std": [2.06, 2.43]
}
}"#;
let custom_words_sentiment_hashmap: CustomWords = serde_json::from_str(custom_word_dict).unwrap();
let sentiment = SentimentModel::new(custom_words_sentiment_hashmap);
if sentiment.does_term_exist("abduction") {
println!("abduction exists");
}
Examples found in repository?
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
fn main() {
// lexicon data pulled from https://link.springer.com/article/10.3758/s13428-012-0314-x
let mut reader = csv::Reader::from_path("examples/BRM-emot-submit.csv").unwrap();
let mut custom_words: CustomWords = HashMap::new();
for record in reader.records() {
let record = record.unwrap();
let word = record[1].to_owned();
let stemmed_word = stem::get(&word).unwrap();
let avg = vec![record[2].parse::<f64>().unwrap(), record[5].parse::<f64>().unwrap()];
let std = vec![record[3].parse::<f64>().unwrap(), record[6].parse::<f64>().unwrap()];
let sentiment_dict = SentimentDictValue::new(word, stemmed_word, avg, std);
custom_words.insert(record[1].to_owned(), sentiment_dict);
}
let sentiment = SentimentModel::new(custom_words);
println!("{:?}", sentiment.get_arousal_for_single_term("abduction"));
}
sourcepub fn add_custom_stems(&mut self, custom_stems: CustomStems)
pub fn add_custom_stems(&mut self, custom_stems: CustomStems)
Adds new custom_stems
lexicon of stemmed words.
Examples
use rnltk::sentiment::{SentimentModel, CustomWords, CustomStems};
use rnltk::sample_data;
let custom_words_sentiment_hashmap: CustomWords = sample_data::get_sample_custom_word_dict();
let custom_stem_dict = r#"
{
"abduct": {
"word": "abduction",
"stem": "abduct",
"avg": [2.76, 5.53],
"std": [2.06, 2.43]
}
}"#;
let custom_stems_sentiment_hashmap: CustomStems = serde_json::from_str(custom_stem_dict).unwrap();
let mut sentiment = SentimentModel::new(custom_words_sentiment_hashmap);
sentiment.add_custom_stems(custom_stems_sentiment_hashmap);
if sentiment.does_term_exist("abduct") {
println!("abduct exists");
}
sourcepub fn does_term_exist(&self, term: &str) -> bool
pub fn does_term_exist(&self, term: &str) -> bool
Checks if a term
exists in the sentiment dictionaries.
Examples
use rnltk::sentiment::{SentimentModel, CustomWords};
use rnltk::sample_data;
let custom_words_sentiment_hashmap: CustomWords = sample_data::get_sample_custom_word_dict();
let sentiment = SentimentModel::new(custom_words_sentiment_hashmap);
if sentiment.does_term_exist("abduction") {
println!("abduction exists");
}
sourcepub fn get_raw_arousal(&self, term: &str) -> RawSentiment
pub fn get_raw_arousal(&self, term: &str) -> RawSentiment
Gets the raw arousal values (RawSentiment
) for a given term
word token.
Examples
use rnltk::sentiment::{SentimentModel, CustomWords};
use rnltk::sample_data;
let custom_words_sentiment_hashmap: CustomWords = sample_data::get_sample_custom_word_dict();
let sentiment = SentimentModel::new(custom_words_sentiment_hashmap);
let arousal = sentiment.get_raw_arousal("abduction");
let correct_arousal = vec![5.53, 2.43];
assert_eq!(vec![arousal.average, arousal.standard_deviation], correct_arousal);
sourcepub fn get_raw_valence(&self, term: &str) -> RawSentiment
pub fn get_raw_valence(&self, term: &str) -> RawSentiment
Gets the raw valence values (RawSentiment
) for a given term
word token.
Examples
use rnltk::sentiment::{SentimentModel, CustomWords};
use rnltk::sample_data;
let custom_words_sentiment_hashmap: CustomWords = sample_data::get_sample_custom_word_dict();
let sentiment = SentimentModel::new(custom_words_sentiment_hashmap);
let valence = sentiment.get_raw_valence("abduction");
let correct_valence = vec![2.76, 2.06];
assert_eq!(vec![valence.average, valence.standard_deviation], correct_valence);
sourcepub fn get_arousal_for_single_term(&self, term: &str) -> f64
pub fn get_arousal_for_single_term(&self, term: &str) -> f64
Gets the arousal value for a given term
word token.
Examples
use rnltk::sentiment::{SentimentModel, CustomWords};
use rnltk::sample_data;
let custom_words_sentiment_hashmap: CustomWords = sample_data::get_sample_custom_word_dict();
let sentiment = SentimentModel::new(custom_words_sentiment_hashmap);
let arousal = sentiment.get_arousal_for_single_term("abduction");
let correct_arousal = 5.53;
assert_eq!(arousal, correct_arousal);
Examples found in repository?
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
fn main() {
// lexicon data pulled from https://link.springer.com/article/10.3758/s13428-012-0314-x
let mut reader = csv::Reader::from_path("examples/BRM-emot-submit.csv").unwrap();
let mut custom_words: CustomWords = HashMap::new();
for record in reader.records() {
let record = record.unwrap();
let word = record[1].to_owned();
let stemmed_word = stem::get(&word).unwrap();
let avg = vec![record[2].parse::<f64>().unwrap(), record[5].parse::<f64>().unwrap()];
let std = vec![record[3].parse::<f64>().unwrap(), record[6].parse::<f64>().unwrap()];
let sentiment_dict = SentimentDictValue::new(word, stemmed_word, avg, std);
custom_words.insert(record[1].to_owned(), sentiment_dict);
}
let sentiment = SentimentModel::new(custom_words);
println!("{:?}", sentiment.get_arousal_for_single_term("abduction"));
}
sourcepub fn get_valence_for_single_term(&self, term: &str) -> f64
pub fn get_valence_for_single_term(&self, term: &str) -> f64
Gets the valence value for a given term
word token.
Examples
use rnltk::sentiment::{SentimentModel, CustomWords};
use rnltk::sample_data;
let custom_words_sentiment_hashmap: CustomWords = sample_data::get_sample_custom_word_dict();
let sentiment = SentimentModel::new(custom_words_sentiment_hashmap);
let valence = sentiment.get_valence_for_single_term("abduction");
let correct_valence = 2.76;
assert_eq!(valence, correct_valence);
sourcepub fn get_arousal_for_term_vector(&self, terms: &Vec<&str>) -> f64
pub fn get_arousal_for_term_vector(&self, terms: &Vec<&str>) -> f64
Gets the arousal value for a word token vector of terms
.
Examples
use rnltk::sentiment::{SentimentModel, CustomWords};
let custom_word_dict = r#"
{
"betrayed": {
"word": "betrayed",
"stem": "betrai",
"avg": [2.57, 7.24],
"std": [1.83, 2.06]
},
"bees": {
"word": "bees",
"stem": "bee",
"avg": [3.2, 6.51],
"std": [2.07, 2.14]
}
}"#;
let custom_words_sentiment_hashmap: CustomWords = serde_json::from_str(custom_word_dict).unwrap();
let sentiment = SentimentModel::new(custom_words_sentiment_hashmap);
let arousal = sentiment.get_arousal_for_term_vector(&vec!["I", "betrayed", "the", "bees"]);
let correct_arousal = 6.881952380952381;
assert_eq!(arousal, correct_arousal);
sourcepub fn get_valence_for_term_vector(&self, terms: &Vec<&str>) -> f64
pub fn get_valence_for_term_vector(&self, terms: &Vec<&str>) -> f64
Gets the valence value for a word token vector of terms
.
Examples
use rnltk::sentiment::{SentimentModel, CustomWords};
let custom_word_dict = r#"
{
"betrayed": {
"word": "betrayed",
"stem": "betrai",
"avg": [2.57, 7.24],
"std": [1.83, 2.06]
},
"bees": {
"word": "bees",
"stem": "bee",
"avg": [3.2, 6.51],
"std": [2.07, 2.14]
}
}"#;
let custom_words_sentiment_hashmap: CustomWords = serde_json::from_str(custom_word_dict).unwrap();
let sentiment = SentimentModel::new(custom_words_sentiment_hashmap);
let valence = sentiment.get_valence_for_term_vector(&vec!["I", "betrayed", "the", "bees"]);
let correct_valence = 2.865615384615385;
assert_eq!(valence, correct_valence);
sourcepub fn get_sentiment_for_term(&self, term: &str) -> HashMap<&str, f64>
pub fn get_sentiment_for_term(&self, term: &str) -> HashMap<&str, f64>
Gets the valence, arousal sentiment for a term
word token.
Examples
use std::collections::HashMap;
use rnltk::sentiment::{SentimentModel, CustomWords};
use rnltk::sample_data;
let custom_words_sentiment_hashmap: CustomWords = sample_data::get_sample_custom_word_dict();
let sentiment = SentimentModel::new(custom_words_sentiment_hashmap);
let sentiment_info = sentiment.get_sentiment_for_term("abduction");
let sentiment_map = HashMap::from([("valence", 2.76), ("arousal", 5.53)]);
assert_eq!(sentiment_info, sentiment_map);
sourcepub fn get_sentiment_for_term_vector(
&self,
terms: &Vec<&str>
) -> HashMap<&str, f64>
pub fn get_sentiment_for_term_vector( &self, terms: &Vec<&str> ) -> HashMap<&str, f64>
Gets the valence, arousal sentiment for a word token vector of terms
.
Examples
use std::collections::HashMap;
use rnltk::sentiment::{SentimentModel, CustomWords};
let custom_word_dict = r#"
{
"betrayed": {
"word": "betrayed",
"stem": "betrai",
"avg": [2.57, 7.24],
"std": [1.83, 2.06]
},
"bees": {
"word": "bees",
"stem": "bee",
"avg": [3.2, 6.51],
"std": [2.07, 2.14]
}
}"#;
let custom_words_sentiment_hashmap: CustomWords = serde_json::from_str(custom_word_dict).unwrap();
let sentiment = SentimentModel::new(custom_words_sentiment_hashmap);
let sentiment_info = sentiment.get_sentiment_for_term_vector(&vec!["I", "betrayed", "the", "bees"]);
let sentiment_map = HashMap::from([("valence", 2.865615384615385), ("arousal", 6.881952380952381)]);
assert_eq!(sentiment_info, sentiment_map);
sourcepub fn get_sentiment_description(
&self,
valence: &f64,
arousal: &f64
) -> Cow<'static, str>
pub fn get_sentiment_description( &self, valence: &f64, arousal: &f64 ) -> Cow<'static, str>
Gets the Russel-like description given valence
and arousal
scores.
Examples
use rnltk::sentiment::{SentimentModel, CustomWords};
use rnltk::sample_data;
let custom_words_sentiment_hashmap: CustomWords = sample_data::get_sample_custom_word_dict();
let sentiment = SentimentModel::new(custom_words_sentiment_hashmap);
let sentiment_description = sentiment.get_sentiment_description(&2.76, &5.53);
let description = "upset";
assert_eq!(sentiment_description, description);
sourcepub fn get_term_description(&self, term: &str) -> Cow<'static, str>
pub fn get_term_description(&self, term: &str) -> Cow<'static, str>
Gets the Russel-like description given a term
word token.
Examples
use std::collections::HashMap;
use rnltk::sentiment::{SentimentModel, CustomWords};
use rnltk::sample_data;
let custom_words_sentiment_hashmap: CustomWords = sample_data::get_sample_custom_word_dict();
let sentiment = SentimentModel::new(custom_words_sentiment_hashmap);
let sentiment_description = sentiment.get_term_description("abduction");
let description = "upset";
assert_eq!(sentiment_description, description);
sourcepub fn get_term_vector_description(
&self,
terms: &Vec<&str>
) -> Cow<'static, str>
pub fn get_term_vector_description( &self, terms: &Vec<&str> ) -> Cow<'static, str>
Gets the Russel-like description given a word token vector of terms
.
Examples
use rnltk::sentiment::{SentimentModel, CustomWords};
let custom_word_dict = r#"
{
"betrayed": {
"word": "betrayed",
"stem": "betrai",
"avg": [2.57, 7.24],
"std": [1.83, 2.06]
},
"bees": {
"word": "bees",
"stem": "bee",
"avg": [3.2, 6.51],
"std": [2.07, 2.14]
}
}"#;
let custom_words_sentiment_hashmap: CustomWords = serde_json::from_str(custom_word_dict).unwrap();
let sentiment = SentimentModel::new(custom_words_sentiment_hashmap);
let sentiment_description = sentiment.get_term_vector_description(&vec!["I", "betrayed", "the", "bees"]);
let description = "stressed";
assert_eq!(sentiment_description, description);
sourcepub fn add_term_without_replacement(
&mut self,
term: &'static str,
valence: &f64,
arousal: &f64
) -> Result<(), RnltkError>
pub fn add_term_without_replacement( &mut self, term: &'static str, valence: &f64, arousal: &f64 ) -> Result<(), RnltkError>
Adds a new term
word token with its corresponding valence
and arousal
values to the sentiment lexicons. If the term
does not already exist, it
will be added to the custom sentiment lexicon.
Errors
Returns RnltkError::SentimentTermExists
if the term already exists
Examples
use std::collections::HashMap;
use rnltk::sentiment::{SentimentModel, CustomWords};
use rnltk::error::RnltkError;
use rnltk::sample_data;
let custom_words_sentiment_hashmap: CustomWords = sample_data::get_sample_custom_word_dict();
let mut sentiment = SentimentModel::new(custom_words_sentiment_hashmap);
let sentiment_return_value = sentiment.add_term_without_replacement("squanch", &2.0, &8.5);
match sentiment_return_value {
Ok(_) => {
let sentiment_info = sentiment.get_sentiment_for_term("squanch");
let sentiment_map = HashMap::from([("valence", 2.0), ("arousal", 8.5)]);
assert_eq!(sentiment_info, sentiment_map);
},
Err(error_msg) => assert_eq!(error_msg, RnltkError::SentimentTermExists),
}
sourcepub fn add_term_with_replacement(
&mut self,
term: &'static str,
valence: &f64,
arousal: &f64
) -> Result<(), RnltkError>
pub fn add_term_with_replacement( &mut self, term: &'static str, valence: &f64, arousal: &f64 ) -> Result<(), RnltkError>
Adds a new term
word token and its corresponding valence
and arousal
values to the sentiment lexicons. If this term
already exists, the term
will be updated
with the new valence
and arousal
values. If the term
does not already exist, the term
will be
stemmed and added to the custom sentiment lexicon.
Errors
Returns RnltkError::StemNonAscii
in the event that the term
being stemmed contains non-ASCII characters (like hopè).
Examples
use std::collections::HashMap;
use rnltk::sentiment::{SentimentModel, CustomWords};
use rnltk::error::RnltkError;
use rnltk::sample_data;
let custom_words_sentiment_hashmap: CustomWords = sample_data::get_sample_custom_word_dict();
let mut sentiment = SentimentModel::new(custom_words_sentiment_hashmap);
let sentiment_return_value = sentiment.add_term_with_replacement("abduction", &8.0, &8.5);
match sentiment_return_value {
Ok(_) => {
let sentiment_info = sentiment.get_sentiment_for_term("abduction");
let sentiment_map = HashMap::from([("valence", 8.0), ("arousal", 8.5)]);
assert_eq!(sentiment_info, sentiment_map);
},
Err(error_msg) => assert_eq!(error_msg, RnltkError::StemNonAscii),
}
Auto Trait Implementations§
impl RefUnwindSafe for SentimentModel
impl Send for SentimentModel
impl Sync for SentimentModel
impl Unpin for SentimentModel
impl UnwindSafe for SentimentModel
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.