Advertisement
ivandrofly

LanguageAutoDetect

Feb 19th, 2025
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1.     public static class LanguageAutoDetect
  2.     {
  3.  
  4.         private static int GetCount(string text, params string[] words)
  5.         {
  6.             var commonWordSet = new HashSet<string>(words);
  7.             var boundaryCharsSet = new HashSet<char>
  8.             {
  9.                 '.', ' ', '}', ']', ')', '(', '[', '{', '-', '|', '?', '!', '&', '\r', '\n', ',' ,':', '>', '<','\'', '"'
  10.             };
  11.  
  12.             int len = text.Length;
  13.             int l = 0;
  14.             var count = 0;
  15.             for (int r = 0; r < len; r++)
  16.             {
  17.                 if (char.IsLetter(text[r]) && !char.IsLetter(text[l]))
  18.                 {
  19.                     l = r;
  20.                 }
  21.                 else if (boundaryCharsSet.Contains(text[r]) && char.IsLetter(text[l]) && r - l > 1)
  22.                 {
  23.                     string word = text.Substring(l, r - l);
  24.                     if (commonWordSet.Contains(word) || commonWordSet.Contains(word.ToLowerInvariant()))
  25.                     {
  26.                         count++;
  27.                     }
  28.  
  29.                     l = r + 1;
  30.                 }
  31.                 else if (boundaryCharsSet.Contains(text[r]))
  32.                 {
  33.                     l = r + 1;
  34.                 }
  35.             }
  36.  
  37.             return count;
  38.         }
Tags: SubtitleEdit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement