pub fn parse_many_synonyms(
        &self,
        relations: &[String],
    ) -> Result<HashMap<String, Box<[String]>>> {
        let guard = self.synonyms.load();
        let current_mapping = guard.as_ref();
        let mut new_mapping: HashMap<String, HashSet<String>> =
            HashMap::with_capacity(current_mapping.len());
        for (key, relations) in current_mapping {
            new_mapping.insert(
                key.to_string(),
                HashSet::from_iter(relations.iter().map(String::to_string)),
            );
        }

        for relation in relations {
            let (left, right) = relation.split_once(':').ok_or_else(|| {
                anyhow!(
                    "invalid synonym relation defined. synonyms must follow the \
                    `word(s):related_item(s)` format, Got {:?}",
                    relation,
                )
            })?;

            let left = left.to_lowercase();
            let right = right.to_lowercase();

            let relation_stream: Vec<&str> = right.split(',').collect();

            if relation_stream.is_empty() {
                return Err(anyhow!("at least one relation must be defined, got none"));
            }

            for word in left.split(',') {
                new_mapping
                    .entry(word.trim().to_string())
                    .and_modify(|v| {
                        // There is probably a faster way of doing this. However,
                        // This is by far the simplest strategy and
                        for relation in relation_stream.iter() {
                            // Skip ourself
                            if *relation == word {
                                continue;
                            }

                            v.insert(relation.to_string());
                        }
                    })
                    .or_insert_with(|| {
                        let mut new_set = HashSet::new();

                        // There is probably a faster way of doing this. However,
                        // This is by far the simplest strategy and
                        for relation in relation_stream.iter() {
                            // Skip ourself
                            if *relation == word {
                                continue;
                            }

                            new_set.insert(relation.to_string());
                        }

                        new_set
                    });
            }
        }

        Ok(HashMap::from_iter(new_mapping.into_iter().map(|(k, v)| {
            let v = Vec::from_iter(v.into_iter()).into_boxed_slice();

            (k, v)
        })))
    }
/// Parses a given synonym relation string and modifies the inner dictionary to reflect
/// the changes.
///
/// Parses foo,bar,baz:foo,bar,baz into forming a synonym for each item minus itself.
Parses a given synonym relation string and modifies the inner dictionary to reflect
the changes.

Parses foo,bar,baz:foo,bar,baz into forming a synonym for each item minus itself.
Parses a given synonym relation string and modifies the inner dictionary to reflect the changes . Parses foo bar baz : foo bar baz into forming a synonym for each item minus itself .
pub fn parse_many_synonyms(
        &self,
        relations: &[String],
    ) -> Result<HashMap<String, Box<[String]>>> {
        let guard = self.synonyms.load();
        let current_mapping = guard.as_ref();
        let mut new_mapping: HashMap<String, HashSet<String>> =
            HashMap::with_capacity(current_mapping.len());
        for (key, relations) in current_mapping {
            new_mapping.insert(
                key.to_string(),
                HashSet::from_iter(relations.iter().map(String::to_string)),
            );
        }
        for relation in relations {
            let (left, right) = relation.split_once(':').ok_or_else(|| {
                anyhow!(
                    "invalid synonym relation defined. synonyms must follow the \
                    `word(s):related_item(s)` format, Got {:?}",
                    relation,
                )
            })?;
            let left = left.to_lowercase();
            let right = right.to_lowercase();
            let relation_stream: Vec<&str> = right.split(',').collect();
            if relation_stream.is_empty() {
                return Err(anyhow!("at least one relation must be defined, got none"));
            }
            for word in left.split(',') {
                new_mapping
                    .entry(word.trim().to_string())
                    .and_modify(|v| {
                        // There is probably a faster way of doing this. However,
                        // This is by far the simplest strategy and
                        for relation in relation_stream.iter() {
                            // Skip ourself
                            if *relation == word {
                                continue;
                            }
                            v.insert(relation.to_string());
                        }
                    })
                    .or_insert_with(|| {
                        let mut new_set = HashSet::new();
                        // There is probably a faster way of doing this. However,
                        // This is by far the simplest strategy and
                        for relation in relation_stream.iter() {
                            // Skip ourself
                            if *relation == word {
                                continue;
                            }
                            new_set.insert(relation.to_string());
                        }
                        new_set
                    });
            }
        }
        Ok(HashMap::from_iter(new_mapping.into_iter().map(|(k, v)| {
            let v = Vec::from_iter(v.into_iter()).into_boxed_slice();
            (k, v)
        })))
    }
pub fn parse_many_synonyms ( & self , relations : & [ String ] , ) -> Result < HashMap < String , Box < [ String ] > > > { let guard = self . synonyms . load ( ) ; let current_mapping = guard . as_ref ( ) ; let mut new_mapping : HashMap < String , HashSet < String > > = HashMap :: with_capacity ( current_mapping . len ( ) ) ; for ( key , relations ) in current_mapping { new_mapping . insert ( key . to_string ( ) , HashSet :: from_iter ( relations . iter ( ) . map ( String :: to_string ) ) , ) ; } for relation in relations { let ( left , right ) = relation . split_once ( ':' ) . ok_or_else ( | | { anyhow ! ( " \
 " relation ) } ) ? ; let left = left . to_lowercase ( ) ; let right = right . to_lowercase ( ) ; let relation_stream : Vec < & str > = right . split ( ',' ) . collect ( ) ; if relation_stream . is_empty ( ) { return Err ( anyhow ! ( " " ) ) ; } for word in left . split ( ',' ) { new_mapping . entry ( word . trim ( ) . to_string ( ) ) . and_modify ( | v | { // There is probably a faster way of doing this. However, // This is by far the simplest strategy and for relation in relation_stream . iter ( ) { // Skip ourself if * relation == word { continue ; } v . insert ( relation . to_string ( ) ) ; } } ) . or_insert_with ( | | { let mut new_set = HashSet :: new ( ) ; // There is probably a faster way of doing this. However, // This is by far the simplest strategy and for relation in relation_stream . iter ( ) { // Skip ourself if * relation == word { continue ; } new_set . insert ( relation . to_string ( ) ) ; } new_set } ) ; } } Ok ( HashMap :: from_iter ( new_mapping . into_iter ( ) . map ( | ( k , v ) | { let v = Vec :: from_iter ( v . into_iter ( ) ) . into_boxed_slice ( ) ; ( k , v ) } ) ) ) }
Parses a given synonym relation string and modifies the inner dictionary to reflect
the changes.
Parses a given synonym relation string and modifies the inner dictionary to reflect the changes .
&self
relations
&[String]
&self
relations
&[String]