Решение на Text Info от Йоанна Николова

Обратно към всички решения

Към профила на Йоанна Николова

Резултати

  • 15 точки от тестове
  • 0 бонус точки
  • 15 точки общо
  • 15 успешни тест(а)
  • 0 неуспешни тест(а)

Код

enum State{
CyrillicL,
LatinL,
EndOfSentence,
Other,
}
pub struct TextInfo{
chars : usize,
words : usize,
latin_letters : usize,
cyrillic_letters : usize,
question_sentences : usize,
exlamation_sentences : usize,
period_sentences : usize,
}
type TI = TextInfo;
impl TextInfo{
pub fn new(text: &str) -> TextInfo {
TI::process_info(text)
}
pub fn char_count(&self) -> usize {
self.chars
}
pub fn alphabetic_count(&self) -> usize {
self.latin_letters + self.cyrillic_letters
}
pub fn latin_letter_count(&self) -> usize {
self.latin_letters
}
pub fn cyrillic_letter_count(&self) -> usize {
self.cyrillic_letters
}
pub fn word_count(&self) -> usize {
self.words
}
pub fn sentence_count(&self) -> usize {
self.question_sentences +
self.exlamation_sentences +
self.period_sentences
}
pub fn emotion(&self) -> String {
if self.question_sentences > self.exlamation_sentences &&
self.question_sentences > self.period_sentences {
return String::from( "🤔");
}
else if self.exlamation_sentences > self.question_sentences &&
self.exlamation_sentences > self.period_sentences {
return String::from("😮");
}
else{
return String::from("😐");
}
}
fn process_info(text: &str) -> TextInfo {
let mut data: TextInfo = TextInfo{
chars: 0, words: 0, latin_letters: 0,
cyrillic_letters: 0,
question_sentences: 0,
exlamation_sentences: 0,
period_sentences:0
};
// следи дали в текущото изречение има букви
let mut has_letter= false;
// следи дали в текущото изречение има не пуктиационни знаци, които не
// са букви
let mut has_non_punct_symb = false;

Отвъд това, че съкращаванията ти са малко неконсистентни (щом "symb", що не "lett"? :)), удобна конвенция е винаги да поддържаш булевите си променливи позитивни. Тоест, on_punctuation вероятно ще ти е по-удобно за работа, отколкото not_on_punctuation. Понеже, във втория случай, редовно ще имаш not_on_punctuation = false, което значи, че не сме не на пунктуация, и not_on_punctuation = true, което значи, че сме не на пунктуация. Двойния негатив може да изтормози човек повече, отколкото е необходимо, особено в 03:57 сутринта :)

for symb in text.chars() {
data.chars += 1;
match TI::type_of_symbol(symb){
State::LatinL
=> { data.latin_letters += 1; has_letter = true; },
State::CyrillicL
=> { data.cyrillic_letters += 1; has_letter = true; },
State::EndOfSentence
=> {
// Ако преди това сме нямали не-пунктуац. символ, то това
// не е край на изречение
if !( has_letter || has_non_punct_symb) { continue; }
if has_letter { data.words +=1; has_letter = false;}
//Проверява какъв е знака и увеличава броя на изреченията
// от този тип
data.save_sentence(symb);
has_non_punct_symb = false;
},
State::Other
=> {
has_non_punct_symb =true;
// ако преди това сме имали последователност от символи
// то тя е прекъсната и трябва да увеличим броя на думите
// в текста
if has_letter{ data.words += 1; has_letter = false;}
},
}
}
// Проверяваме дали има последователност от символи, която не сме
// не сме записали като дума.
// Възможно, когато текста завършва с дума
if has_letter { data.words += 1;}
data
}
//Проверява какъв е знака и увеличава броя на изреченията
// от този тип
fn save_sentence(&mut self, end_of_sentence: char){
match end_of_sentence {
'?'
=> { self.question_sentences += 1; },
'!'
=> { self.exlamation_sentences += 1; },
'.'
=> { self.period_sentences += 1; },
_
=> {}

Тук едва ли ще е по-четимо да сложиш => на нов ред. Можеш и да махнеш къдравите скоби, ако имаш само един statement, така че да получиш:

match end_of_sentence {
    '?' => self.question_sentences += 1,
    '!' => ...,
    '.' => ...,
    _   => {},    

Което мисля, че е по-кратко, и по-четимо.

}
}
fn is_end_of_sentence(s : char) -> bool {
s == '?' || s == '!' || s == '.'
}
fn type_of_symbol(s : char) -> State {
if TI::is_latin_letter(s){
return State::LatinL;
}
else if TI::is_cyrillic_letter(s){
return State::CyrillicL;
}
else if TI::is_end_of_sentence(s) {
return State::EndOfSentence;
}
else{
return State::Other;
}
}
fn is_latin_letter(s: char) -> bool {
(s >= 'A' && s <= 'Z') || (s >= 'a' && s <= 'z')
}
fn is_cyrillic_letter(s : char) -> bool {
(s >= 'А' && s <= 'Я') || (s >= 'а' && s <= 'я')
}
}

Лог от изпълнението

Compiling solution v0.1.0 (file:///tmp/d20171026-5817-1wcnati/solution)
    Finished dev [unoptimized + debuginfo] target(s) in 3.5 secs
     Running target/debug/deps/solution-f5dd4e94aa395cae

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-c3b431457e2a7a27

running 15 tests
test solution_test::test_alpha_count ... ok
test solution_test::test_alpha_count_2 ... ok
test solution_test::test_char_count ... ok
test solution_test::test_cyrillic_letter_count ... ok
test solution_test::test_emotions ... ok
test solution_test::test_emotions_repeated_punctuation ... ok
test solution_test::test_empty_string ... ok
test solution_test::test_latin_letter_count ... ok
test solution_test::test_sentence_count ... ok
test solution_test::test_sentence_count_2 ... ok
test solution_test::test_triple_dots_count ... ok
test solution_test::test_unicode_char_count ... ok
test solution_test::test_word_count ... ok
test solution_test::test_word_count_2 ... ok
test solution_test::test_word_count_3 ... ok

test result: ok. 15 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

История (4 версии и 3 коментара)

Йоанна качи първо решение на 25.10.2017 03:45 (преди почти 8 години)

Йоанна качи решение на 25.10.2017 03:50 (преди почти 8 години)

-/// Contain symbol's states
-/// State is used like a C enumerator.
-/// It's used in some functions work with chars to create a state machine.
-/// State enumerator is not necessary but makes code more readable
enum State{
CyrillicL,
LatinL,
EndOfSentence,
Other,
}
-/// Info contain a labeled integer from 0 to 7.
-/// Size has value 0, CntWords - 1, .. , CntElements - 8.
-/// The first six element has a name like a TextInfo's field. The last one - CntElements, containt count of field.
-/// Can use enumerator to indexing a vector that contain a value of TextInfo's field.
enum Info {
Size,
CntWords,
LatinLetter,
CyrillicLetter,
QuestionSentence,
ExlamationSentence,
PeriodSentence,
CntElements,
}
-/// TextInfo is a static struct. It's cointain some important information about a text.
-///
-/// #Warning:
-/// You can't change text of already exist variable of type TextInfo
pub struct TextInfo {
size : usize,
cnt_words : usize,
latin_letter : usize,
cyrillic_letter : usize,
question_sentence : usize,
exlamation_sentence : usize,
period_sentence : usize,
}
type TI = TextInfo;
impl TextInfo{
- /// Create TextInfo from text.
- /// Given TextInfo contain all information about the text but doesn't save a text.
- /// Information about text: count of used chars, count of words in text, count of latin and cyrillic letter,
- /// count of question, exlamation and period sentence. We don't need more information about text to solve homework_1.
- ///
- /// Time complexity of method : linear by chars of text
- ///
- pub fn new(text: &str) -> TextInfo {
- let data : Vec<usize> = TI::process_info(text);
- TextInfo{
- size : data[Info::Size as usize],
- cnt_words: data[Info::CntWords as usize],
- latin_letter: data[Info::LatinLetter as usize],
- cyrillic_letter: data[Info::CyrillicLetter as usize],
- question_sentence: data[Info::QuestionSentence as usize],
- exlamation_sentence: data[Info::ExlamationSentence as usize],
- period_sentence: data[Info::PeriodSentence as usize],
- }
- }
-
- /// Return count of symbols in process text
- ///
- /// Time complexity : constant
- ///
- /// # Warning:
- /// Count of symbol can be different from count of bytes!
- pub fn char_count(&self) -> usize {
- self.size
- }
-
- /// Give a count of alphabetic - latinic and cyrllic letters.
- ///
- /// Time complexity : constant
- pub fn alphabetic_count(&self) -> usize {
- self.latin_letter + self.cyrillic_letter
- }
-
- /// Give a count of latinic letters in the text.
- ///
- /// Time complexity : constant
- pub fn latin_letter_count(&self) -> usize {
- self.latin_letter
- }
-
- /// Give a count of cyrillic letters in the text.
- ///
- /// Time complexity : constant
- pub fn cyrillic_letter_count(&self) -> usize {
- self.cyrillic_letter
- }
-
- /// Give a count of words in text.
- /// Word is a sequence of letter(cyrillic/latin) separated with another symbol.
- ///
- /// Time complexity : constant
- pub fn word_count(&self) -> usize {
- self.cnt_words
- }
-
- /// Give a count of sentence in text.
- /// Sentence is a sequence of non-punctuation symbols and the last element of this sequence is "?" , "!" or "..".
- ///
- /// Time complexity : constant
- pub fn sentence_count(&self) -> usize {
- self.question_sentence + self.exlamation_sentence + self.period_sentence
- }
-
- /// Give a emoji of text.
- ///
- /// Time complexity : constant
- pub fn emotion(&self) -> String {
- if self.question_sentence > self.exlamation_sentence &&
- self.question_sentence > self.period_sentence {
- return String::from( "🤔");
+
+pub fn new(text: &str) -> TextInfo {
+ let data : Vec<usize> = TI::process_info(text);
+ TextInfo{
+ size : data[Info::Size as usize],
+ cnt_words: data[Info::CntWords as usize],
+ latin_letter: data[Info::LatinLetter as usize],
+ cyrillic_letter: data[Info::CyrillicLetter as usize],
+ question_sentence: data[Info::QuestionSentence as usize],
+ exlamation_sentence: data[Info::ExlamationSentence as usize],
+ period_sentence: data[Info::PeriodSentence as usize],
+ }
+}
+
+pub fn char_count(&self) -> usize {
+ self.size
+}
+
+pub fn alphabetic_count(&self) -> usize {
+ self.latin_letter + self.cyrillic_letter
+}
+
+pub fn latin_letter_count(&self) -> usize {
+ self.latin_letter
+}
+
+
+pub fn cyrillic_letter_count(&self) -> usize {
+ self.cyrillic_letter
+}
+
+pub fn word_count(&self) -> usize {
+ self.cnt_words
+}
+
+pub fn sentence_count(&self) -> usize {
+ self.question_sentence + self.exlamation_sentence + self.period_sentence
+}
+
+pub fn emotion(&self) -> String {
+ if self.question_sentence > self.exlamation_sentence &&
+ self.question_sentence > self.period_sentence {
+ return String::from( "🤔");
+ }
+ else if self.exlamation_sentence > self.question_sentence &&
+ self.exlamation_sentence > self.period_sentence {
+ return String::from("😮");
+ }
+ else{
+ return String::from("😐");
+ }
+}
+
+/// Process each symbol of text in this way learn a value of TextInfo's fieds.
+/// Work like state machine - Get the type of symbol(State) and process information in a specific way.
+///
+/// Time complexity : linear by chars of text
+fn process_info(text: &str) -> Vec<usize> {
+ // Each element of vector contain a value of some TextInfo's fied
+ let mut vec = vec![0 ; Info::CntElements as usize];
+ let mut has_letter= false;
+ let mut has_non_punct_symb = false;
+
+ for symb in text.chars() {
+ vec[Info::Size as usize] += 1; //For each symbol increase with one count of symbol.
+ match TI::type_of_symbol(symb){
+ State::LatinL => { //increase with one count of latin letter
+ vec[Info::LatinLetter as usize] += 1;
+ //found letter in text
+ has_letter = true; },
+ State::CyrillicL => { //increase with one count of latin letter
+ vec[Info::CyrillicLetter as usize] += 1;
+ //found letter in text
+ has_letter = true; },
+ State::EndOfSentence => { if has_letter || has_non_punct_symb {
+ TI::add_word_if(&mut vec, &mut has_letter);
+ vec[TI::type_of_sentence(symb) as usize] += 1;
+ // because start new sentance and it doesn't have any non-punctuation symbols for this moment
+ has_non_punct_symb = false;
+ } },
+ State::Other => { has_non_punct_symb =true; // found non-punctuation symbol in current sentance
+ TI::add_word_if(&mut vec, &mut has_letter);},
}
- else if self.exlamation_sentence > self.question_sentence &&
- self.exlamation_sentence > self.period_sentence {
- return String::from("😮");
+ }
+ TI::add_word_if(&mut vec, &mut has_letter);
+ vec
+}
+
+/// This function will increase element of vector which is associate with TextInfo.cnt_words
+/// if we have some found letter after a last saved word.(has_letter = true)
+/// Time complexity : constant
+fn add_word_if(vec: &mut Vec<usize>, has_letter: &mut bool){
+ if *has_letter {
+ // save(increase)
+ vec[Info::CntWords as usize] += 1;
+ // because doesn't have a unsaved letter
+ *has_letter = false;
+ }
+}
+
+fn type_of_sentence(s: char) -> Info {
+ if s == '?'{
+ return Info::QuestionSentence;
}
- else{
- return String::from("😐");
+ else if s == '!' {
+ return Info::ExlamationSentence;
}
- }
-
- /// Process each symbol of text in this way learn a value of TextInfo's fieds.
- /// Work like state machine - Get the type of symbol(State) and process information in a specific way.
- ///
- /// Time complexity : linear by chars of text
- fn process_info(text: &str) -> Vec<usize> {
- let mut vec = vec![0 ; Info::CntElements as usize]; // Each element of vector contain a value of some TextInfo's fied
- let mut has_letter= false;
- let mut has_non_punct_symb = false;
-
- for symb in text.chars() {
- vec[Info::Size as usize] += 1; //For each symbol increase with one count of symbol.
- match TI::type_of_symbol(symb){
- State::LatinL => { vec[Info::LatinLetter as usize] += 1; //increase with one count of latin letter
- has_letter = true; }, //found letter in text
- State::CyrillicL => { vec[Info::CyrillicLetter as usize] += 1; //increase with one count of latin letter
- has_letter = true; }, //found letter in text
- State::EndOfSentence => { if has_letter || has_non_punct_symb {
- TI::add_word_if(&mut vec, &mut has_letter);
- vec[TI::type_of_sentence(symb) as usize] += 1;
- has_non_punct_symb = false; // because start new sentance and it doesn't have any non-punctuation symbols for this moment
- } },
- State::Other => { has_non_punct_symb =true; // found non-punctuation symbol in current sentance
- TI::add_word_if(&mut vec, &mut has_letter);},
- }
- }
- TI::add_word_if(&mut vec, &mut has_letter);
- vec
- }
-
- /// This function will increase element of vector which is associate with TextInfo.cnt_words
- /// if we have some found letter after a last saved word.(has_letter = true)
- /// Time complexity : constant
- fn add_word_if(vec: &mut Vec<usize>, has_letter: &mut bool){
- if *has_letter {
- vec[Info::CntWords as usize] += 1; // save(increase)
- *has_letter = false; // because doesn't have a unsaved letter
- }
- }
-
- fn type_of_sentence(s: char) -> Info {
- if s == '?'{
- return Info::QuestionSentence;
- }
- else if s == '!' {
- return Info::ExlamationSentence;
- }
- else {
- return Info::PeriodSentence;
- }
- }
-
- fn type_of_symbol(s : char) -> State {
- if TI::is_latin_letter(s){
- return State::LatinL;
- }
- else if TI::is_cyrillic_letter(s){
- return State::CyrillicL;
- }
- else if TI::is_end_of_sentence(s) {
- return State::EndOfSentence;
- }
- else{
- return State::Other;
- }
- }
-
- fn is_latin_letter(s: char) -> bool {
- (s >= 'A' && s <= 'Z') || (s >= 'a' && s <= 'z')
+ else {
+ return Info::PeriodSentence;
+ }
+}
+
+fn type_of_symbol(s : char) -> State {
+ if TI::is_latin_letter(s){
+ return State::LatinL;
}
-
- fn is_cyrillic_letter(s : char) -> bool {
- (s >= 'А' && s <= 'Я') || (s >= 'а' && s <= 'я')
+ else if TI::is_cyrillic_letter(s){
+ return State::CyrillicL;
}
-
- fn is_end_of_sentence(s : char) -> bool {
- s == '?' || s == '!' || s == '.'
- }
+ else if TI::is_end_of_sentence(s) {
+ return State::EndOfSentence;
+ }
+ else{
+ return State::Other;
+ }
+}
+
+fn is_latin_letter(s: char) -> bool {
+ (s >= 'A' && s <= 'Z') || (s >= 'a' && s <= 'z')
+}
+
+fn is_cyrillic_letter(s : char) -> bool {
+ (s >= 'А' && s <= 'Я') || (s >= 'а' && s <= 'я')
+}
+
+fn is_end_of_sentence(s : char) -> bool {
+ s == '?' || s == '!' || s == '.'
+}
}

Йоанна качи решение на 25.10.2017 03:57 (преди почти 8 години)

enum State{
CyrillicL,
LatinL,
EndOfSentence,
Other,
}
enum Info {
Size,
CntWords,
LatinLetter,
CyrillicLetter,
QuestionSentence,
ExlamationSentence,
PeriodSentence,
CntElements,
}
pub struct TextInfo {
size : usize,
cnt_words : usize,
latin_letter : usize,
cyrillic_letter : usize,
question_sentence : usize,
exlamation_sentence : usize,
period_sentence : usize,
}
type TI = TextInfo;
impl TextInfo{
pub fn new(text: &str) -> TextInfo {
let data : Vec<usize> = TI::process_info(text);
TextInfo{
size : data[Info::Size as usize],
cnt_words: data[Info::CntWords as usize],
latin_letter: data[Info::LatinLetter as usize],
cyrillic_letter: data[Info::CyrillicLetter as usize],
question_sentence: data[Info::QuestionSentence as usize],
exlamation_sentence: data[Info::ExlamationSentence as usize],
period_sentence: data[Info::PeriodSentence as usize],
}
}
pub fn char_count(&self) -> usize {
self.size
}
pub fn alphabetic_count(&self) -> usize {
self.latin_letter + self.cyrillic_letter
}
pub fn latin_letter_count(&self) -> usize {
self.latin_letter
}
pub fn cyrillic_letter_count(&self) -> usize {
self.cyrillic_letter
}
pub fn word_count(&self) -> usize {
self.cnt_words
}
pub fn sentence_count(&self) -> usize {
self.question_sentence + self.exlamation_sentence + self.period_sentence
}
pub fn emotion(&self) -> String {
if self.question_sentence > self.exlamation_sentence &&
self.question_sentence > self.period_sentence {
return String::from( "🤔");
}
else if self.exlamation_sentence > self.question_sentence &&
self.exlamation_sentence > self.period_sentence {
return String::from("😮");
}
else{
return String::from("😐");
}
}
/// Process each symbol of text in this way learn a value of TextInfo's fieds.
/// Work like state machine - Get the type of symbol(State) and process information in a specific way.
///
/// Time complexity : linear by chars of text
fn process_info(text: &str) -> Vec<usize> {
// Each element of vector contain a value of some TextInfo's fied
let mut vec = vec![0 ; Info::CntElements as usize];
let mut has_letter= false;
let mut has_non_punct_symb = false;
for symb in text.chars() {
- vec[Info::Size as usize] += 1; //For each symbol increase with one count of symbol.
+ //For each symbol increase with one count of symbol.
+ vec[Info::Size as usize] += 1;
match TI::type_of_symbol(symb){
State::LatinL => { //increase with one count of latin letter
vec[Info::LatinLetter as usize] += 1;
//found letter in text
has_letter = true; },
State::CyrillicL => { //increase with one count of latin letter
vec[Info::CyrillicLetter as usize] += 1;
//found letter in text
has_letter = true; },
- State::EndOfSentence => { if has_letter || has_non_punct_symb {
- TI::add_word_if(&mut vec, &mut has_letter);
- vec[TI::type_of_sentence(symb) as usize] += 1;
- // because start new sentance and it doesn't have any non-punctuation symbols for this moment
- has_non_punct_symb = false;
- } },
- State::Other => { has_non_punct_symb =true; // found non-punctuation symbol in current sentance
+ State::EndOfSentence => {
+ if has_letter || has_non_punct_symb {
+ TI::add_word_if(&mut vec, &mut has_letter);
+ vec[TI::type_of_sentence(symb) as usize] += 1;
+ // because start new sentance and it doesn't have any non-punctuation symbols for this moment
+ has_non_punct_symb = false;
+ } },
+ State::Other => { // found non-punctuation symbol in current sentance
+ has_non_punct_symb =true;
TI::add_word_if(&mut vec, &mut has_letter);},
}
}
TI::add_word_if(&mut vec, &mut has_letter);
vec
}
/// This function will increase element of vector which is associate with TextInfo.cnt_words
/// if we have some found letter after a last saved word.(has_letter = true)
/// Time complexity : constant
fn add_word_if(vec: &mut Vec<usize>, has_letter: &mut bool){
if *has_letter {
// save(increase)
vec[Info::CntWords as usize] += 1;
// because doesn't have a unsaved letter
*has_letter = false;
}
}
fn type_of_sentence(s: char) -> Info {
if s == '?'{
return Info::QuestionSentence;
}
else if s == '!' {
return Info::ExlamationSentence;
}
else {
return Info::PeriodSentence;
}
}
fn type_of_symbol(s : char) -> State {
if TI::is_latin_letter(s){
return State::LatinL;
}
else if TI::is_cyrillic_letter(s){
return State::CyrillicL;
}
else if TI::is_end_of_sentence(s) {
return State::EndOfSentence;
}
else{
return State::Other;
}
}
fn is_latin_letter(s: char) -> bool {
(s >= 'A' && s <= 'Z') || (s >= 'a' && s <= 'z')
}
fn is_cyrillic_letter(s : char) -> bool {
(s >= 'А' && s <= 'Я') || (s >= 'а' && s <= 'я')
}
fn is_end_of_sentence(s : char) -> bool {
s == '?' || s == '!' || s == '.'
}
}

Йоанна качи решение на 25.10.2017 20:53 (преди почти 8 години)

enum State{
- CyrillicL,
- LatinL,
- EndOfSentence,
- Other,
+ CyrillicL,
+ LatinL,
+ EndOfSentence,
+ Other,
}
-enum Info {
- Size,
- CntWords,
- LatinLetter,
- CyrillicLetter,
- QuestionSentence,
- ExlamationSentence,
- PeriodSentence,
- CntElements,
+pub struct TextInfo{
+ chars : usize,
+ words : usize,
+ latin_letters : usize,
+ cyrillic_letters : usize,
+ question_sentences : usize,
+ exlamation_sentences : usize,
+ period_sentences : usize,
}
-pub struct TextInfo {
- size : usize,
- cnt_words : usize,
- latin_letter : usize,
- cyrillic_letter : usize,
- question_sentence : usize,
- exlamation_sentence : usize,
- period_sentence : usize,
-}
-
type TI = TextInfo;
impl TextInfo{
+ pub fn new(text: &str) -> TextInfo {
+ TI::process_info(text)
+ }
-pub fn new(text: &str) -> TextInfo {
- let data : Vec<usize> = TI::process_info(text);
- TextInfo{
- size : data[Info::Size as usize],
- cnt_words: data[Info::CntWords as usize],
- latin_letter: data[Info::LatinLetter as usize],
- cyrillic_letter: data[Info::CyrillicLetter as usize],
- question_sentence: data[Info::QuestionSentence as usize],
- exlamation_sentence: data[Info::ExlamationSentence as usize],
- period_sentence: data[Info::PeriodSentence as usize],
- }
-}
+ pub fn char_count(&self) -> usize {
+ self.chars
+ }
-pub fn char_count(&self) -> usize {
- self.size
-}
+ pub fn alphabetic_count(&self) -> usize {
+ self.latin_letters + self.cyrillic_letters
+ }
-pub fn alphabetic_count(&self) -> usize {
- self.latin_letter + self.cyrillic_letter
-}
+ pub fn latin_letter_count(&self) -> usize {
+ self.latin_letters
+ }
-pub fn latin_letter_count(&self) -> usize {
- self.latin_letter
-}
+ pub fn cyrillic_letter_count(&self) -> usize {
+ self.cyrillic_letters
+ }
+ pub fn word_count(&self) -> usize {
+ self.words
+ }
-pub fn cyrillic_letter_count(&self) -> usize {
- self.cyrillic_letter
-}
+ pub fn sentence_count(&self) -> usize {
+ self.question_sentences +
+ self.exlamation_sentences +
+ self.period_sentences
+ }
-pub fn word_count(&self) -> usize {
- self.cnt_words
-}
+ pub fn emotion(&self) -> String {
+ if self.question_sentences > self.exlamation_sentences &&
+ self.question_sentences > self.period_sentences {
+ return String::from( "🤔");
+ }
+ else if self.exlamation_sentences > self.question_sentences &&
+ self.exlamation_sentences > self.period_sentences {
+ return String::from("😮");
+ }
+ else{
+ return String::from("😐");
+ }
+ }
-pub fn sentence_count(&self) -> usize {
- self.question_sentence + self.exlamation_sentence + self.period_sentence
-}
+ fn process_info(text: &str) -> TextInfo {
+ let mut data: TextInfo = TextInfo{
+ chars: 0, words: 0, latin_letters: 0,
+ cyrillic_letters: 0,
+ question_sentences: 0,
+ exlamation_sentences: 0,
+ period_sentences:0
+ };
+ // следи дали в текущото изречение има букви
+ let mut has_letter= false;
+ // следи дали в текущото изречение има не пуктиационни знаци, които не
+ // са букви
+ let mut has_non_punct_symb = false;

Отвъд това, че съкращаванията ти са малко неконсистентни (щом "symb", що не "lett"? :)), удобна конвенция е винаги да поддържаш булевите си променливи позитивни. Тоест, on_punctuation вероятно ще ти е по-удобно за работа, отколкото not_on_punctuation. Понеже, във втория случай, редовно ще имаш not_on_punctuation = false, което значи, че не сме не на пунктуация, и not_on_punctuation = true, което значи, че сме не на пунктуация. Двойния негатив може да изтормози човек повече, отколкото е необходимо, особено в 03:57 сутринта :)

+
+ for symb in text.chars() {
+ data.chars += 1;
+ match TI::type_of_symbol(symb){
+ State::LatinL
+ => { data.latin_letters += 1; has_letter = true; },
+
+ State::CyrillicL
+ => { data.cyrillic_letters += 1; has_letter = true; },
+
+ State::EndOfSentence
+ => {
+ // Ако преди това сме нямали не-пунктуац. символ, то това
+ // не е край на изречение
+ if !( has_letter || has_non_punct_symb) { continue; }
+ if has_letter { data.words +=1; has_letter = false;}
+ //Проверява какъв е знака и увеличава броя на изреченията
+ // от този тип
+ data.save_sentence(symb);
+ has_non_punct_symb = false;
+ },
+ State::Other
+ => {
+ has_non_punct_symb =true;
+ // ако преди това сме имали последователност от символи
+ // то тя е прекъсната и трябва да увеличим броя на думите
+ // в текста
+ if has_letter{ data.words += 1; has_letter = false;}
+ },
+
+ }
+ }
+ // Проверяваме дали има последователност от символи, която не сме
+ // не сме записали като дума.
+ // Възможно, когато текста завършва с дума
+ if has_letter { data.words += 1;}
+
+ data
+ }
+
+ //Проверява какъв е знака и увеличава броя на изреченията
+ // от този тип
+ fn save_sentence(&mut self, end_of_sentence: char){
+ match end_of_sentence {
+ '?'
+ => { self.question_sentences += 1; },
+ '!'
+ => { self.exlamation_sentences += 1; },
+ '.'
+ => { self.period_sentences += 1; },
+ _
+ => {}

Тук едва ли ще е по-четимо да сложиш => на нов ред. Можеш и да махнеш къдравите скоби, ако имаш само един statement, така че да получиш:

match end_of_sentence {
    '?' => self.question_sentences += 1,
    '!' => ...,
    '.' => ...,
    _   => {},    

Което мисля, че е по-кратко, и по-четимо.

+ }
+ }
+
+ fn is_end_of_sentence(s : char) -> bool {
+ s == '?' || s == '!' || s == '.'
+ }
+
+ fn type_of_symbol(s : char) -> State {
+ if TI::is_latin_letter(s){
+ return State::LatinL;
+ }
+ else if TI::is_cyrillic_letter(s){
+ return State::CyrillicL;
+ }
+ else if TI::is_end_of_sentence(s) {
+ return State::EndOfSentence;
+ }
+ else{
+ return State::Other;
+ }
+ }
-pub fn emotion(&self) -> String {
- if self.question_sentence > self.exlamation_sentence &&
- self.question_sentence > self.period_sentence {
- return String::from( "🤔");
- }
- else if self.exlamation_sentence > self.question_sentence &&
- self.exlamation_sentence > self.period_sentence {
- return String::from("😮");
- }
- else{
- return String::from("😐");
- }
-}
+ fn is_latin_letter(s: char) -> bool {
+ (s >= 'A' && s <= 'Z') || (s >= 'a' && s <= 'z')
+ }
-/// Process each symbol of text in this way learn a value of TextInfo's fieds.
-/// Work like state machine - Get the type of symbol(State) and process information in a specific way.
-///
-/// Time complexity : linear by chars of text
-fn process_info(text: &str) -> Vec<usize> {
- // Each element of vector contain a value of some TextInfo's fied
- let mut vec = vec![0 ; Info::CntElements as usize];
- let mut has_letter= false;
- let mut has_non_punct_symb = false;
-
- for symb in text.chars() {
- //For each symbol increase with one count of symbol.
- vec[Info::Size as usize] += 1;
- match TI::type_of_symbol(symb){
- State::LatinL => { //increase with one count of latin letter
- vec[Info::LatinLetter as usize] += 1;
- //found letter in text
- has_letter = true; },
- State::CyrillicL => { //increase with one count of latin letter
- vec[Info::CyrillicLetter as usize] += 1;
- //found letter in text
- has_letter = true; },
- State::EndOfSentence => {
- if has_letter || has_non_punct_symb {
- TI::add_word_if(&mut vec, &mut has_letter);
- vec[TI::type_of_sentence(symb) as usize] += 1;
- // because start new sentance and it doesn't have any non-punctuation symbols for this moment
- has_non_punct_symb = false;
- } },
- State::Other => { // found non-punctuation symbol in current sentance
- has_non_punct_symb =true;
- TI::add_word_if(&mut vec, &mut has_letter);},
- }
- }
- TI::add_word_if(&mut vec, &mut has_letter);
- vec
-}
-
-/// This function will increase element of vector which is associate with TextInfo.cnt_words
-/// if we have some found letter after a last saved word.(has_letter = true)
-/// Time complexity : constant
-fn add_word_if(vec: &mut Vec<usize>, has_letter: &mut bool){
- if *has_letter {
- // save(increase)
- vec[Info::CntWords as usize] += 1;
- // because doesn't have a unsaved letter
- *has_letter = false;
- }
-}
-
-fn type_of_sentence(s: char) -> Info {
- if s == '?'{
- return Info::QuestionSentence;
- }
- else if s == '!' {
- return Info::ExlamationSentence;
- }
- else {
- return Info::PeriodSentence;
- }
-}
-
-fn type_of_symbol(s : char) -> State {
- if TI::is_latin_letter(s){
- return State::LatinL;
- }
- else if TI::is_cyrillic_letter(s){
- return State::CyrillicL;
- }
- else if TI::is_end_of_sentence(s) {
- return State::EndOfSentence;
- }
- else{
- return State::Other;
- }
-}
-
-fn is_latin_letter(s: char) -> bool {
- (s >= 'A' && s <= 'Z') || (s >= 'a' && s <= 'z')
-}
-
-fn is_cyrillic_letter(s : char) -> bool {
- (s >= 'А' && s <= 'Я') || (s >= 'а' && s <= 'я')
-}
-
-fn is_end_of_sentence(s : char) -> bool {
- s == '?' || s == '!' || s == '.'
-}
+ fn is_cyrillic_letter(s : char) -> bool {
+ (s >= 'А' && s <= 'Я') || (s >= 'а' && s <= 'я')
+ }
}

Индентациите ти са доста неконсистентни. Подозирам, че е въпрос на мешане на табове и интервали, така че може да огледаш настройките на редактора си и да си избереш някакъв setting. Изглежда като малко нещо, но не подценявай колко по-лесно се работи с подреден код, който реагира консистентно, когато натиснеш backspace клавиш :).

Може да пробваш да си инсталираш и rustfmt програмата и да видиш какво форматиране ще ти предложи. Не е задължително да следваш suggestion-ите 1:1, но си заслужава да видиш "нормалния" стил на Rust код, който ще срещаш често в чужди библиотеки или примери.