Advertisement
MBrendecke

SMS-Analyse

Feb 21st, 2020
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6. namespace SMS_Analyse
  7. {
  8.     public enum Worttyp
  9.     {
  10.         Nomen,
  11.         Verb,
  12.         Adjektiv,        
  13.         Artikel
  14.     }
  15.    
  16.     public static class Wortliste
  17.     {
  18.         public static IEnumerable<string> ReadFromFile(string path) {
  19.             try {
  20.                 return File.ReadAllLines(path);
  21.             } catch (Exception) {
  22.                 throw;
  23.             }
  24.         }
  25.     }
  26.  
  27.     sealed class Matrix
  28.     {
  29.         private static Matrix INSTANCE = null;
  30.         public static Matrix Instance {
  31.             get {
  32.                 if (INSTANCE == null)
  33.                     INSTANCE = new Matrix();
  34.  
  35.                 return INSTANCE;
  36.             }
  37.         }
  38.  
  39.         private readonly Dictionary<Worttyp, Dictionary<Worttyp, bool>> _matrix;
  40.  
  41.         private Matrix()
  42.         {
  43.             _matrix = new Dictionary<Worttyp, Dictionary<Worttyp, bool>>();
  44.             Initialize();
  45.         }
  46.  
  47.         private void Initialize()
  48.         {
  49.             var values = (Worttyp[])Enum.GetValues(typeof(Worttyp));
  50.             foreach (Worttyp outer in values)
  51.             {
  52.                 _matrix.Add(outer, new Dictionary<Worttyp, bool>());
  53.  
  54.                 foreach (Worttyp inner in values)
  55.                 {
  56.                     _matrix[outer].Add(inner, false);
  57.                 }
  58.             }
  59.         }
  60.  
  61.         public bool this[Worttyp von, Worttyp nach] {
  62.             get {
  63.                 return _matrix[von][nach];
  64.             }
  65.             set {
  66.                 _matrix[von][nach] = value;
  67.             }
  68.         }
  69.     }
  70.  
  71.     class Program
  72.     {
  73.         static readonly Dictionary<string, Worttyp> DIC = new Dictionary<string, Worttyp>();
  74.         static void AddRange(IEnumerable<string> wörter, Worttyp typ)
  75.         {
  76.             foreach (string wort in wörter)
  77.             {
  78.                 DIC[wort] = typ;
  79.             }
  80.         }
  81.         static void AddRange(string path, Worttyp typ)
  82.         {
  83.             AddRange(Wortliste.ReadFromFile(path), typ);
  84.         }
  85.  
  86.         static void Main(string[] args)
  87.         {
  88.             var am = Matrix.Instance;
  89.             am[Worttyp.Artikel, Worttyp.Nomen] = true;
  90.             am[Worttyp.Artikel, Worttyp.Adjektiv] = true;
  91.  
  92.             am[Worttyp.Nomen, Worttyp.Verb] = true;            
  93.             am[Worttyp.Nomen, Worttyp.Nomen] = true;
  94.  
  95.             am[Worttyp.Adjektiv, Worttyp.Adjektiv] = true;
  96.             am[Worttyp.Adjektiv, Worttyp.Verb] = true;
  97.  
  98.             am[Worttyp.Verb, Worttyp.Nomen] = true;            
  99.  
  100.             AddRange(@"Listen\Nomen.lst", Worttyp.Nomen);
  101.             AddRange(@"Listen\Adjektive.lst", Worttyp.Adjektiv);
  102.             AddRange(@"Listen\Verben.lst", Worttyp.Verb);
  103.             AddRange(@"Listen\Artikel.lst", Worttyp.Artikel);
  104.  
  105.  
  106.             string satz = "Blau, blau, blau blüht der Enzian";
  107.             //string satz = "Der Blau der Grün Enzian, rote Rosen";
  108.            
  109.             satz = satz.Replace(",", "");
  110.             var satzArr = satz.Split(' ');
  111.  
  112.             var test = new List<Worttyp>();
  113.             satzArr.ToList().ForEach(w => test.Add(DIC[w]));
  114.  
  115.             var ergebnis = new bool[test.Count() - 1];
  116.             for (int i = 0; i < ergebnis.Length; i++)
  117.             {
  118.                 ergebnis[i] = am[test[i], test[i + 1]];
  119.             }
  120.            
  121.             bool found = false;
  122.             for (int i = 0; i < ergebnis.Length; i++) {
  123.                 if (ergebnis[i] &&
  124.                     (DIC[satzArr[i]] == Worttyp.Nomen ||
  125.                      DIC[satzArr[i + 1]] == Worttyp.Nomen)) {
  126.                     found = true;
  127.                     Console.WriteLine("{0} {1}", satzArr[i], satzArr[i + 1]);
  128.                 }
  129.             }
  130.            
  131.             if (!found) {
  132.                 foreach (var w in satzArr) {
  133.                     if (DIC[w] == Worttyp.Nomen) {
  134.                         Console.WriteLine(w);
  135.                     }
  136.                 }
  137.             }
  138.            
  139.            
  140.             Console.ReadKey(true);
  141.         }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement