Last active
December 27, 2020 10:04
-
-
Save mormegil-cz/6634304 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void CountAccents(string input) | |
{ | |
if (input.Length != 0 && !Char.IsWhiteSpace(input[input.Length - 1])) input = input + " "; | |
int wordsTotal = 0; | |
int wordsOnlyLetters = 0; | |
int wordsAccented = 0; | |
int wordStart = 0; | |
bool hasAccent = false; | |
bool hasNonLetter = false; | |
for (int i = 0; i < input.Length; ++i) | |
{ | |
var c = input[i]; | |
if (Char.IsWhiteSpace(c) || Char.IsSeparator(c) || Char.IsPunctuation(c)) | |
{ | |
int wordLength = i - wordStart; | |
if (wordLength > 0) | |
{ | |
++wordsTotal; | |
if (!hasNonLetter && wordLength > 0) ++wordsOnlyLetters; | |
if (!hasNonLetter && wordLength > 0 && hasAccent) ++wordsAccented; | |
} | |
wordStart = i + 1; | |
hasNonLetter = hasAccent = false; | |
} | |
else if (!Char.IsLetter(c)) | |
{ | |
hasNonLetter = true; | |
} | |
else if (c > '\u0080') | |
{ | |
hasAccent = true; | |
} | |
} | |
Console.WriteLine("{0} words total, {1} real words, {2} ({3:N1} %) with accents", wordsTotal, wordsOnlyLetters, wordsAccented, 100.0 * wordsAccented / wordsOnlyLetters); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment