ret_0

Untitled

Oct 2nd, 2019
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp1
  8. {
  9.     class Program
  10.     {
  11.         static char[] VOWELS = { 'а', 'е', 'и', 'о', 'у', 'ы', 'э', 'ю', 'я' };
  12.         static string[] PERFECTIVE_GERUND_1 = { "в", "вши", "вшись" };
  13.         static string[] PERFECTIVE_GERUND_2 = { "ив", "ивши", "ившись", "ыв", "ывши", "ывшись" };
  14.         static string[] ADJEDCTIVE = { "ее", "ие", "ые", "ое", "ими", "ыми", "ей", "ий", "ый", "ой", "ем", "им", "ым", "ом", "его", "ого", "ему", "ому", "их", "ых", "ую", "юю", "ая", "яя", "ою", "ею" };
  15.         static string[] PARTICIPLE_1 = { "ем", "нн", "вш", "ющ", "щ" };
  16.         static string[] PARTICIPLE_2 = { "ивш", "ывш", "ующ" };
  17.         static string[] REFLEXIVE = { "ся", "сь" };
  18.         static string[] VERB_1 = { "ла", "на", "ете", "йте", "ли", "й", "л", "ем", "н", "ло", "но", "ет", "ют", "ны", "ть", "ешь", "нно" };
  19.         static string[] VERB_2 = { "ила", "ыла", "ена", "ейте", "уйте", "ите", "или", "ыли", "ей", "уй", "ил", "ыл", "им", "ым", "ен", "ило", "ыло", "ено", "ят", "ует", "уют", "ит", "ыт", "ены", "ить", "ыть", "ишь", "ую", "ю" };
  20.         static string[] NOUN = { "а", "ев", "ов", "ие", "ье", "е", "иями", "ями", "ами", "еи", "ии", "и", "ией", "ей", "ой", "ий", "й", "иям", "ям", "ием", "ем", "ам", "ом", "о", "у", "ах", "иях", "ях", "ы", "ь", "ию", "ью", "ю", "ия", "ья", "я" };
  21.         static string[] SUPERLATIVE = { "ейш", "ейше" };
  22.         static string[] DERIVATIONAL = { "ост", "ость" };
  23.         static string RV, R1, R2, word;
  24.  
  25.         static string ValidateString(string a)
  26.         {
  27.             string result = a.ToLower();
  28.             result = result.Replace('ё', 'е');
  29.             foreach (char c in result)
  30.             {
  31.                 if (c < 'а' || c > 'я')
  32.                     return null;
  33.             }
  34.             return result;
  35.         }
  36.  
  37.         static int IndexOfVowel(string a)
  38.         {
  39.             foreach (char c in a)
  40.             {
  41.                 foreach (char t in VOWELS)
  42.                 {
  43.                     if (c == t)
  44.                     {
  45.                         return a.IndexOf(c);
  46.                     }
  47.                 }
  48.             }
  49.             return -1;
  50.         }
  51.  
  52.         static int IndexOfConsonant(string a)
  53.         {
  54.             bool yeah = false;
  55.             foreach (char c in a)
  56.             {
  57.                 yeah = false;
  58.                 foreach (char t in VOWELS)
  59.                 {
  60.                     if (c == t)
  61.                     {
  62.                         yeah = true;
  63.                         break;
  64.                     }
  65.                 }
  66.                 if (!yeah)
  67.                     return a.IndexOf(c);
  68.             }
  69.             return -1;
  70.         }
  71.  
  72.         static string SetWord(string a)
  73.         {
  74.             return a.Replace(RV, "");
  75.         }
  76.  
  77.         static bool IsEnding(string a, string ending)
  78.         {
  79.             int index = a.LastIndexOf(ending);
  80.             if (index > 0 && a.Length - index == ending.Length)
  81.                 return true;
  82.             else
  83.                 return false;
  84.         }
  85.  
  86.         static void UpdateRS(string word)
  87.         {
  88.             RV = FindRV(word);
  89.             R1 = FindR1(word);
  90.             R2 = FindR2(word);
  91.             return;
  92.         }
  93.  
  94.         static string FindRV(string a)
  95.         {
  96.             string result = "";
  97.             int pos = -1;
  98.             bool exit = false;
  99.             foreach (char c in a)
  100.             {
  101.                 foreach (char t in VOWELS)
  102.                 {
  103.                     if (c == t)
  104.                     {
  105.                         pos = a.IndexOf(c);
  106.                         exit = true;
  107.                         break;
  108.                     }
  109.                 }
  110.                 if (exit)
  111.                     break;
  112.             }
  113.             result = a.Substring(++pos);
  114.             return result;
  115.         }
  116.  
  117.         static string FindR1(string a)
  118.         {
  119.             string result = "";
  120.             int v, c;
  121.             v = IndexOfVowel(a);
  122.             c = IndexOfConsonant(a);
  123.             if (v < 0 || c < 0)
  124.                 return result;
  125.             while (a.Length > 0)
  126.             {
  127.                 v = IndexOfVowel(a);
  128.                 c = IndexOfConsonant(a);
  129.                 if (c - v != 1)
  130.                     a = a.Substring(1);
  131.                 else
  132.                 {
  133.                     return a.Substring(++c);
  134.                 }
  135.             }
  136.             return result;
  137.         }
  138.  
  139.         static string FindR2(string a)
  140.         {
  141.             a = FindR1(a);
  142.             string result = "";
  143.             int v, c;
  144.             v = IndexOfVowel(a);
  145.             c = IndexOfConsonant(a);
  146.             if (v < 0 || c < 0)
  147.                 return result;
  148.             while (a.Length > 0)
  149.             {
  150.                 v = IndexOfVowel(a);
  151.                 c = IndexOfConsonant(a);
  152.                 if (c - v != 1)
  153.                     a = a.Substring(1);
  154.                 else
  155.                 {
  156.                     return a.Substring(++c);
  157.                 }
  158.             }
  159.             return result;
  160.         }
  161.  
  162.         static bool DeleteGerund(string a, out string r)
  163.         {
  164.             string result = "";
  165.             bool found = false;
  166.             int index = -1;
  167.             List<string> rs = new List<string>();
  168.             foreach (string gerund in PERFECTIVE_GERUND_1)
  169.             {
  170.                 index = a.LastIndexOf(gerund);
  171.                 if (index > 0 && (a[index - 1] == 'а' || a[index - 1] == 'я'))
  172.                 {
  173.                     if (IsEnding(a, gerund))
  174.                     {
  175.                         found = true;
  176.                         rs.Add(gerund);
  177.                     }
  178.                 }
  179.             }
  180.             foreach (string gerund in PERFECTIVE_GERUND_2)
  181.             {
  182.                 index = a.LastIndexOf(gerund);
  183.                 if (index != -1)
  184.                 {
  185.                     if (IsEnding(a, gerund))
  186.                     {
  187.                         found = true;
  188.                         rs.Add(gerund);
  189.                     }
  190.                 }
  191.             }
  192.             if (found)
  193.             {
  194.                 var sorted = from s in rs orderby s.Length descending select s;
  195.                 foreach (string gerund in sorted)
  196.                 {
  197.                     //result = a.Replace(gerund, "");
  198.                     Console.WriteLine("gerund " + gerund);
  199.                     //break;
  200.                 }
  201.             }
  202.             r = result;
  203.             return found;
  204.         }
  205.  
  206.         static bool DeleteReflexive(string a, out string r)
  207.         {
  208.             string result = "";
  209.             bool found = false;
  210.             foreach (string reflexive in REFLEXIVE)
  211.             {
  212.                 if (a.LastIndexOf(reflexive) > -1)
  213.                 {
  214.                     if (IsEnding(a, reflexive))
  215.                     {
  216.                         result = a.Remove(a.LastIndexOf(reflexive), reflexive.Length);
  217.                         found = true;
  218.                         break;
  219.                     }
  220.                 }
  221.             }
  222.             r = result;
  223.             return found;
  224.         }
  225.  
  226.         static bool DeleteAdjectival(string a, out string r)
  227.         {
  228.             string result = "";
  229.             bool found = false;
  230.             List<string> rs = new List<string>();
  231.             int index = -1;
  232.             foreach (string adjective in ADJEDCTIVE)
  233.             {
  234.                 if (a.LastIndexOf(adjective) > -1)
  235.                 {
  236.                     if (IsEnding(a, adjective))
  237.                     {
  238.                         found = true;
  239.                         rs.Add(adjective);
  240.                     }
  241.                 }
  242.                 foreach (string participle in PARTICIPLE_1)
  243.                 {
  244.                     index = a.LastIndexOf(participle);
  245.                     if (index > 0 && (a[index - 1] == 'а' || a[index - 1] == 'я'))
  246.                     {
  247.                         if (a.LastIndexOf(participle + adjective) > -1)
  248.                         {
  249.                             if (IsEnding(a, participle + adjective))
  250.                             {
  251.                                 found = true;
  252.                                 rs.Add(participle + adjective);
  253.                             }
  254.                         }
  255.                     }
  256.                 }
  257.                 foreach (string participle in PARTICIPLE_2)
  258.                 {
  259.                     if (a.LastIndexOf(participle + adjective) > -1)
  260.                     {
  261.                         if (IsEnding(a, participle + adjective))
  262.                         {
  263.                             found = true;
  264.                             rs.Add(participle + adjective);
  265.                         }
  266.                     }
  267.                 }
  268.             }
  269.             if (found)
  270.             {
  271.                 var sorted = from s in rs orderby s.Length descending select s;
  272.                 foreach (string adjectival in sorted)
  273.                 {
  274.                     result = a.Replace(adjectival, "");
  275.                     //Console.WriteLine("adjectivalsss: " + adjectival);
  276.                     break;
  277.                 }
  278.             }
  279.             r = result;
  280.             return found;
  281.         }
  282.  
  283.         static bool DeleteVerb(string a, out string r)
  284.         {
  285.             string result = "";
  286.             bool found = false;
  287.             int index = -1;
  288.             List<string> rs = new List<string>();
  289.             foreach (string verb in VERB_1)
  290.             {
  291.                 index = a.LastIndexOf(verb);
  292.                 if (index > 0 && (a[index - 1] == 'а' || a[index - 1] == 'я'))
  293.                 {
  294.                     if (IsEnding(a, verb))
  295.                     {
  296.                         found = true;
  297.                         rs.Add(verb);
  298.                     }
  299.                 }
  300.             }
  301.             foreach (string verb in VERB_2)
  302.             {
  303.                 index = a.LastIndexOf(verb);
  304.                 if (index > -1)
  305.                 {
  306.                     if (IsEnding(a, verb))
  307.                     {
  308.                         found = true;
  309.                         rs.Add(verb);
  310.                     }
  311.                 }
  312.             }
  313.             if (found)
  314.             {
  315.                 var sorted = from s in rs orderby s.Length descending select s;
  316.                 foreach (string verb in sorted)
  317.                 {
  318.                     result = a.Replace(verb, "");
  319.                     break;
  320.                 }
  321.             }
  322.             r = result;
  323.             return found;
  324.         }
  325.  
  326.         static bool DeleteNoun(string a, out string r)
  327.         {
  328.             string result = "";
  329.             bool found = false;
  330.             List<string> rs = new List<string>();
  331.             foreach (string noun in NOUN)
  332.             {
  333.                 if (a.LastIndexOf(noun) > -1)
  334.                 {
  335.                     if (IsEnding(a, noun))
  336.                     {
  337.                         result = a.Remove(a.LastIndexOf(noun), noun.Length);
  338.                         found = true;
  339.                         rs.Add(noun);
  340.                     }
  341.                 }
  342.             }
  343.             if (found)
  344.             {
  345.                 var sorted = from s in rs orderby s.Length descending select s;
  346.                 foreach (string noun in sorted)
  347.                 {
  348.                     result = a.Replace(noun, "");
  349.                     break;
  350.                 }
  351.             }
  352.             r = result;
  353.             return found;
  354.         }
  355.  
  356.         static bool DeleteDerivational(string a, out string r)
  357.         {
  358.             string result = "";
  359.             bool found = false;
  360.             List<string> rs = new List<string>();
  361.             foreach (string derivational in DERIVATIONAL)
  362.             {
  363.                 if (a.LastIndexOf(derivational) > -1)
  364.                 {
  365.                     if (IsEnding(a, derivational))
  366.                     {
  367.                         result = a.Remove(a.LastIndexOf(derivational), derivational.Length);
  368.                         found = true;
  369.                         rs.Add(derivational);
  370.                     }
  371.                 }
  372.             }
  373.             if (found)
  374.             {
  375.                 var sorted = from s in rs orderby s.Length descending select s;
  376.                 foreach (string derivational in sorted)
  377.                 {
  378.                     result = a.Replace(derivational, "");
  379.                     break;
  380.                 }
  381.             }
  382.             r = result;
  383.             return found;
  384.         }
  385.  
  386.         static bool DeleteSuperlative(string a, out string r)
  387.         {
  388.             string result = "";
  389.             bool found = false;
  390.             List<string> rs = new List<string>();
  391.             foreach (string superlative in SUPERLATIVE)
  392.             {
  393.                 if (a.LastIndexOf(superlative) > -1)
  394.                 {
  395.                     if (IsEnding(a, superlative))
  396.                     {
  397.                         result = a.Remove(a.LastIndexOf(superlative), superlative.Length);
  398.                         found = true;
  399.                         rs.Add(superlative);
  400.                     }
  401.                 }
  402.             }
  403.             if (found)
  404.             {
  405.                 var sorted = from s in rs orderby s.Length descending select s;
  406.                 foreach (string superlative in sorted)
  407.                 {
  408.                     result = a.Replace(superlative, "");
  409.                     break;
  410.                 }
  411.             }
  412.             r = result;
  413.             return found;
  414.         }
  415.  
  416.         static void Main(string[] args)
  417.         {
  418.             Console.Write("Введите слово: ");
  419.             word = ValidateString(Console.ReadLine());
  420.             if (word == null)
  421.             {
  422.                 Console.WriteLine("Слово было введено с символами кроме кириллицы или не было введено вообще!\n");
  423.                 Console.ReadKey();
  424.                 return;
  425.             }
  426.             UpdateRS(word);
  427.             Console.WriteLine("RV (область слова после первой гласной): " + RV);
  428.             Console.WriteLine("R1 (область слова после первого сочетания “гласная-согласная”): " + R1);
  429.             Console.WriteLine("R2 (область R1 после первого сочетания “гласная-согласная”): " + R2);
  430.             bool was = false;
  431.  
  432.             //word = RV;
  433.             word = SetWord(word);
  434.  
  435.             // step 1
  436.             //Console.WriteLine("gay".IndexOf("ass").ToString());
  437.             Console.Write("\nШаг 1\n");
  438.             string word_1;
  439.             if (DeleteGerund(RV, out word_1))
  440.             {
  441.                 RV = word_1;
  442.                 was = true;
  443.                 Console.WriteLine("Удалён PERFECTIVE_GERUND: " + word + word_1);
  444.             }
  445.             else
  446.             {
  447.                 if (DeleteReflexive(RV, out word_1))
  448.                 {
  449.                     RV = word_1;
  450.                     was = true;
  451.                     Console.WriteLine("Удалён REFLEXIVE: " + word + word_1);
  452.                 }
  453.                 if (DeleteAdjectival(RV, out word_1))
  454.                 {
  455.                     RV = word_1;
  456.                     was = true;
  457.                     Console.WriteLine("Удалён ADJECTIVAL: " + word + word_1);
  458.                 }
  459.                 else if (DeleteVerb(RV, out word_1))
  460.                 {
  461.                     RV = word_1;
  462.                     was = true;
  463.                     Console.WriteLine("Удалён VERB: " + word + word_1);
  464.                 }
  465.                 else if (DeleteNoun(RV, out word_1))
  466.                 {
  467.                     RV = word_1;
  468.                     was = true;
  469.                     Console.WriteLine("Удалён NOUN: " + word + word_1);
  470.                 }
  471.             }
  472.             if (!was)
  473.                 Console.WriteLine("Произошло ничего");
  474.  
  475.             // step 2
  476.             was = false;
  477.             Console.Write("\nШаг 2:\n");
  478.             if (IsEnding(word + RV, "и"))
  479.             {
  480.                 RV = RV.Substring(0, RV.Length - 1);
  481.                 was = true;
  482.                 Console.WriteLine("Слово заканчивалось на \'и\', удалили: " + word + RV);
  483.             }
  484.             if (!was)
  485.                 Console.WriteLine("Произошло ничего");
  486.  
  487.             // step 3
  488.             was = false;
  489.             Console.Write("\nШаг 3:\n");
  490.             UpdateRS(word + RV); // word before RV
  491.             if (DeleteDerivational(R2, out word_1))
  492.             {
  493.                 DeleteDerivational(RV, out word_1);
  494.                 RV = word_1;
  495.                 was = true;
  496.                 Console.WriteLine("Удалён DERIVATIONAL: " + word + RV);
  497.             }
  498.             if(!was)
  499.                 Console.WriteLine("Произошло ничего");
  500.  
  501.             // step 4
  502.             was = false;
  503.             Console.Write("\nШаг 4:\n");
  504.             if (IsEnding(RV, "нн"))
  505.             {
  506.                 RV = RV.Remove(RV.Length - 1);
  507.                 Console.WriteLine("Слово заканчивалось на \'нн\', удалили одну: " + word + RV);
  508.                 was = true;
  509.             }
  510.             else if(DeleteSuperlative(RV, out word_1))
  511.             {
  512.                 RV = word_1;
  513.                 Console.WriteLine("Удалён SUPERLATIVE: " + word + RV);
  514.                 if (IsEnding(RV, "нн"))
  515.                 {
  516.                     RV = RV.Remove(RV.Length - 1);
  517.                     Console.WriteLine("Также слово заканчивалось на \'нн\', удалили одну: " + word + RV);
  518.                 }
  519.                 was = true;
  520.             }
  521.             else if (IsEnding(RV, "ь"))
  522.             {
  523.                 RV = RV.Remove(RV.Length - 1);
  524.                 Console.WriteLine("Слово заканчивалось на \'ь\', удалили: " + word + RV);
  525.                 was = true;
  526.             }
  527.             if (!was)
  528.                 Console.WriteLine("Произошло ничего");
  529.  
  530.             Console.WriteLine("\n---------------------------------------\nИтак, основа слова: " + word + RV);
  531.             Console.ReadKey();
  532.         }
  533.     }
  534. }
Advertisement
Add Comment
Please, Sign In to add comment