pub struct SentimentModel { /* private fields */ }

Implementations§

source§

impl SentimentModel

source

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?
examples/lexicon_creation.rs (line 29)
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"));
}
source

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");
}
source

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");
}
source

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);
source

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);
source

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?
examples/lexicon_creation.rs (line 30)
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"));
}
source

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);
source

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);
source

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);
source

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);
source

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);
source

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);
source

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);
source

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);
source

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),
}
source

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§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.