pub fn word_segmentation(&self, input: &str, max_edit_distance: usize, max_segmentation_word_len_opt: Option<usize>) -> (String, String, usize, f64) {
        let max_segmentation_word_len = max_segmentation_word_len_opt.unwrap_or(self.max_dictionary_word_length);
        let input_gc = GraphemeClusters::new(input);
        let input_len = input_gc.len();
        let capacity = max_segmentation_word_len.min(input_len);
        let mut compositions: Vec<(String, String, usize, f64)> = Vec::with_capacity(capacity);

        let mut circular_index = -1;
        // outer loop (column): all possible part start positions
        for j in 0..input_len {
            // inner loop (row): all possible part lengths (from start position): part can't be bigger than longest word in dictionary (other than long unknown word)
            let max = max_segmentation_word_len.min(input_len - j);
            for i in 1..max + 1 {
                // get top spelling correction/ed for part
                let input_range = input_gc.get_slice_range(j..i);
                let mut part = unsafe { input.get_unchecked(input_range).to_string() };
                let mut separator_len = 0;
                let mut top_edit_distance = 0;

                // remove space for levensthein calculation
                if " \n\r\t".contains(unsafe { input.get_unchecked(0..1) }) {
                    part = unsafe { input.get_unchecked(j + 1..i).to_string() };
                } else {
                    // add ed+1: space did not exist, had to be inserted
                    separator_len = 1;
                }

                // remove space from part1, add number of removed spaces to topEd
                top_edit_distance += GraphemeClusters::new(&part).len();
                // remove space
                part = part.replace(" ", "");
                // add number of removed spaces to ed
                let part_len = GraphemeClusters::new(&part).len();
                top_edit_distance -= part_len;

                let results = self.lookup(&part, Verbosity::Top, max_edit_distance, false, true);
                let (top_result, top_probability_log) = if !results.is_empty() {
                    let result = &results[0];
                    top_edit_distance += result.distance;
                    // Naive Bayes Rule
                    // we assume the word probabilities of two words to be independent
                    // therefore the resulting probability of the word combination is the product of the two word probabilities

                    // instead of computing the product of probabilities we are computing the sum of the logarithm of probabilities
                    // because the probabilities of words are about 10^-10, the product of many such small numbers could exceed (underflow) the floating number range and become zero
                    // log(ab)=log(a)+log(b)
                    (&result.term, (result.count as f64 / N).log10())
                } else {
                    // default, if word not found
                    // otherwise long input text would win as long unknown word (with ed=edmax+1 ), although there there should many spaces inserted
                    (&part, (10.0 / (N * 10.0f64.powf(part_len as f64))).log10())
                };

                let destination_index = ((i as i32 + circular_index) % capacity as i32) as usize;

                //set values in first loop
                if j == 0 {
                    compositions[destination_index] = (part.to_string(), top_result.to_string(), top_edit_distance, top_probability_log);
                }

                if circular_index == -1 {
                    continue;
                }
                // Cleaner conditionals this way
                let (_, _, d_distance_sum, d_probability_log_sum) = &compositions[destination_index];
                let (c_segmented_string, c_corrected_string, c_distance_sum, c_probability_log_sum) = &compositions[circular_index as usize];

                if i == max_segmentation_word_len ||
                    //replace values if better probabilityLogSum, if same edit distance OR one space difference
                    ((c_distance_sum + top_edit_distance == *d_distance_sum || c_distance_sum + separator_len + top_edit_distance == *d_distance_sum) &&
                        d_probability_log_sum < c_probability_log_sum) ||
                    c_distance_sum + separator_len + top_edit_distance < *d_distance_sum {
                    compositions[destination_index] = (
                        c_segmented_string.to_owned() + " " + &part,
                        c_corrected_string.to_owned() + " " + top_result,
                        *c_distance_sum + separator_len + top_edit_distance,
                        *c_probability_log_sum + top_probability_log
                    );
                }
            }
            circular_index += 1;
            if circular_index as usize == capacity {
                circular_index = 0;
            }
        }

        compositions.remove(circular_index as usize)
    }
/// <summary>Find suggested spellings for a multi-word input string (supports word splitting/merging).</summary>
/// <param name="input">The string being spell checked.</param>
/// <param name="maxSegmentationWordLength">The maximum word length that should be considered.</param>
/// <param name="maxEditDistance">The maximum edit distance between input and corrected words
/// (0=no correction/segmentation only).</param>
/// <returns>The word segmented string,
/// the word segmented and spelling corrected string,
/// the Edit distance sum between input string and corrected string,
/// the Sum of word occurence probabilities in log scale (a measure of how common and probable the corrected segmentation is).</returns>
Find suggested spellings for a multi-word input string (supports word splitting/merging).
The string being spell checked.
The maximum word length that should be considered.
The maximum edit distance between input and corrected words
(0=no correction/segmentation only).
The word segmented string,
the word segmented and spelling corrected string,
the Edit distance sum between input string and corrected string,
the Sum of word occurence probabilities in log scale (a measure of how common and probable the corrected segmentation is).
Find suggested spellings for a multi - word input string ( supports word splitting / merging ) . The string being spell checked . The maximum word length that should be considered . The maximum edit distance between input and corrected words ( 0 = no correction / segmentation only ) . The word segmented string the word segmented and spelling corrected string the Edit distance sum between input string and corrected string the Sum of word occurence probabilities in log scale ( a measure of how common and probable the corrected segmentation is ) .
pub fn word_segmentation(&self, input: &str, max_edit_distance: usize, max_segmentation_word_len_opt: Option<usize>) -> (String, String, usize, f64) {
        let max_segmentation_word_len = max_segmentation_word_len_opt.unwrap_or(self.max_dictionary_word_length);
        let input_gc = GraphemeClusters::new(input);
        let input_len = input_gc.len();
        let capacity = max_segmentation_word_len.min(input_len);
        let mut compositions: Vec<(String, String, usize, f64)> = Vec::with_capacity(capacity);
        let mut circular_index = -1;
        // outer loop (column): all possible part start positions
        for j in 0..input_len {
            // inner loop (row): all possible part lengths (from start position): part can't be bigger than longest word in dictionary (other than long unknown word)
            let max = max_segmentation_word_len.min(input_len - j);
            for i in 1..max + 1 {
                // get top spelling correction/ed for part
                let input_range = input_gc.get_slice_range(j..i);
                let mut part = unsafe { input.get_unchecked(input_range).to_string() };
                let mut separator_len = 0;
                let mut top_edit_distance = 0;
                // remove space for levensthein calculation
                if " \n\r\t".contains(unsafe { input.get_unchecked(0..1) }) {
                    part = unsafe { input.get_unchecked(j + 1..i).to_string() };
                } else {
                    // add ed+1: space did not exist, had to be inserted
                    separator_len = 1;
                }
                // remove space from part1, add number of removed spaces to topEd
                top_edit_distance += GraphemeClusters::new(&part).len();
                // remove space
                part = part.replace(" ", "");
                // add number of removed spaces to ed
                let part_len = GraphemeClusters::new(&part).len();
                top_edit_distance -= part_len;
                let results = self.lookup(&part, Verbosity::Top, max_edit_distance, false, true);
                let (top_result, top_probability_log) = if !results.is_empty() {
                    let result = &results[0];
                    top_edit_distance += result.distance;
                    // Naive Bayes Rule
                    // we assume the word probabilities of two words to be independent
                    // therefore the resulting probability of the word combination is the product of the two word probabilities
                    // instead of computing the product of probabilities we are computing the sum of the logarithm of probabilities
                    // because the probabilities of words are about 10^-10, the product of many such small numbers could exceed (underflow) the floating number range and become zero
                    // log(ab)=log(a)+log(b)
                    (&result.term, (result.count as f64 / N).log10())
                } else {
                    // default, if word not found
                    // otherwise long input text would win as long unknown word (with ed=edmax+1 ), although there there should many spaces inserted
                    (&part, (10.0 / (N * 10.0f64.powf(part_len as f64))).log10())
                };
                let destination_index = ((i as i32 + circular_index) % capacity as i32) as usize;
                //set values in first loop
                if j == 0 {
                    compositions[destination_index] = (part.to_string(), top_result.to_string(), top_edit_distance, top_probability_log);
                }
                if circular_index == -1 {
                    continue;
                }
                // Cleaner conditionals this way
                let (_, _, d_distance_sum, d_probability_log_sum) = &compositions[destination_index];
                let (c_segmented_string, c_corrected_string, c_distance_sum, c_probability_log_sum) = &compositions[circular_index as usize];
                if i == max_segmentation_word_len ||
                    //replace values if better probabilityLogSum, if same edit distance OR one space difference
                    ((c_distance_sum + top_edit_distance == *d_distance_sum || c_distance_sum + separator_len + top_edit_distance == *d_distance_sum) &&
                        d_probability_log_sum < c_probability_log_sum) ||
                    c_distance_sum + separator_len + top_edit_distance < *d_distance_sum {
                    compositions[destination_index] = (
                        c_segmented_string.to_owned() + " " + &part,
                        c_corrected_string.to_owned() + " " + top_result,
                        *c_distance_sum + separator_len + top_edit_distance,
                        *c_probability_log_sum + top_probability_log
                    );
                }
            }
            circular_index += 1;
            if circular_index as usize == capacity {
                circular_index = 0;
            }
        }
        compositions.remove(circular_index as usize)
    }
pub fn word_segmentation ( & self , input : & str , max_edit_distance : usize , max_segmentation_word_len_opt : Option < usize > ) -> ( String , String , usize , f64 ) { let max_segmentation_word_len = max_segmentation_word_len_opt . unwrap_or ( self . max_dictionary_word_length ) ; let input_gc = GraphemeClusters :: new ( input ) ; let input_len = input_gc . len ( ) ; let capacity = max_segmentation_word_len . min ( input_len ) ; let mut compositions : Vec < ( String , String , usize , f64 ) > = Vec :: with_capacity ( capacity ) ; let mut circular_index = - 1 ; // outer loop (column): all possible part start positions for j in 0 .. input_len { // inner loop (row): all possible part lengths (from start position): part can't be bigger than longest word in dictionary (other than long unknown word) let max = max_segmentation_word_len . min ( input_len - j ) ; for i in 1 .. max + 1 { // get top spelling correction/ed for part let input_range = input_gc . get_slice_range ( j .. i ) ; let mut part = unsafe { input . get_unchecked ( input_range ) . to_string ( ) } ; let mut separator_len = 0 ; let mut top_edit_distance = 0 ; // remove space for levensthein calculation if " \n \r \t " . contains ( unsafe { input . get_unchecked ( 0 .. 1 ) } ) { part = unsafe { input . get_unchecked ( j + 1 .. i ) . to_string ( ) } ; } else { // add ed+1: space did not exist, had to be inserted separator_len = 1 ; } // remove space from part1, add number of removed spaces to topEd top_edit_distance += GraphemeClusters :: new ( & part ) . len ( ) ; // remove space part = part . replace ( " " , " " ) ; // add number of removed spaces to ed let part_len = GraphemeClusters :: new ( & part ) . len ( ) ; top_edit_distance -= part_len ; let results = self . lookup ( & part , Verbosity :: Top , max_edit_distance , false , true ) ; let ( top_result , top_probability_log ) = if ! results . is_empty ( ) { let result = & results [ 0 ] ; top_edit_distance += result . distance ; // Naive Bayes Rule // we assume the word probabilities of two words to be independent // therefore the resulting probability of the word combination is the product of the two word probabilities // instead of computing the product of probabilities we are computing the sum of the logarithm of probabilities // because the probabilities of words are about 10^-10, the product of many such small numbers could exceed (underflow) the floating number range and become zero // log(ab)=log(a)+log(b) ( & result . term , ( result . count as f64 / N ) . log10 ( ) ) } else { // default, if word not found // otherwise long input text would win as long unknown word (with ed=edmax+1 ), although there there should many spaces inserted ( & part , ( 10.0 / ( N * 10.0f64 . powf ( part_len as f64 ) ) ) . log10 ( ) ) } ; let destination_index = ( ( i as i32 + circular_index ) % capacity as i32 ) as usize ; //set values in first loop if j == 0 { compositions [ destination_index ] = ( part . to_string ( ) , top_result . to_string ( ) , top_edit_distance , top_probability_log ) ; } if circular_index == - 1 { continue ; } // Cleaner conditionals this way let ( _ , _ , d_distance_sum , d_probability_log_sum ) = & compositions [ destination_index ] ; let ( c_segmented_string , c_corrected_string , c_distance_sum , c_probability_log_sum ) = & compositions [ circular_index as usize ] ; if i == max_segmentation_word_len || //replace values if better probabilityLogSum, if same edit distance OR one space difference ( ( c_distance_sum + top_edit_distance == * d_distance_sum || c_distance_sum + separator_len + top_edit_distance == * d_distance_sum ) && d_probability_log_sum < c_probability_log_sum ) || c_distance_sum + separator_len + top_edit_distance < * d_distance_sum { compositions [ destination_index ] = ( c_segmented_string . to_owned ( ) + " " + & part , c_corrected_string . to_owned ( ) + " " + top_result , * c_distance_sum + separator_len + top_edit_distance , * c_probability_log_sum + top_probability_log ) ; } } circular_index += 1 ; if circular_index as usize == capacity { circular_index = 0 ; } } compositions . remove ( circular_index as usize ) }
<summary>Find suggested spellings for a multi-word input string (supports word splitting/merging).</summary>
<param name="input">The string being spell checked.</param>
<param name="maxSegmentationWordLength">The maximum word length that should be considered.</param>
<param name="maxEditDistance">The maximum edit distance between input and corrected words
(0=no correction/segmentation only).</param>
<returns>The word segmented string,
the word segmented and spelling corrected string,
the Edit distance sum between input string and corrected string,
the Sum of word occurence probabilities in log scale (a measure of how common and probable the corrected segmentation is).</returns>
<summary > Find suggested spellings for a multi - word input string ( supports word splitting / merging ) . < / summary > <param name = " input " > The string being spell checked . < / param > <param name = " maxSegmentationWordLength " > The maximum word length that should be considered . < / param > <param name = " maxEditDistance " > The maximum edit distance between input and corrected words ( 0 = no correction / segmentation only ) . < / param > <returns > The word segmented string the word segmented and spelling corrected string the Edit distance sum between input string and corrected string the Sum of word occurence probabilities in log scale ( a measure of how common and probable the corrected segmentation is ) . < / returns >
&self
input
&str
max_edit_distance
usize
max_segmentation_word_len_opt
Option<usize>