C# :: Aufgabe #124 :: Lösung #1
3 Lösungen
#124
Zeichensatz zu Umlauten
Fortgeschrittener - C#
von J_U_B
- 16.03.2016 um 10:03 Uhr
Es soll ein Programm geschrieben werden, welches Zeichensätze aus einem String - wie zum Beispiel "AE", "OE", "UE" oder "SS" - in die Entsprechenden Umlaute umwandelt - "AE" zu "Ä", "OE" zu "Ö" u.s.w.
Hierbei sollte auf die Korrekte Umsetzung der Wörter geachtet werden, hierfür einige Beispiele:
STOSSDAEMPFER = STOßDÄMPFER
STEUERKETTE = STEUERKETTE
KASSE = KASSE
HUELSE = HÜLSE
u.s.w.
Hierbei sollte auf die Korrekte Umsetzung der Wörter geachtet werden, hierfür einige Beispiele:
STOSSDAEMPFER = STOßDÄMPFER
STEUERKETTE = STEUERKETTE
KASSE = KASSE
HUELSE = HÜLSE
u.s.w.
#1
von daniel59 (4260 Punkte)
- 06.04.2016 um 10:39 Uhr
using System;
using System.Collections.Generic;
using System.Linq;
using Word = Microsoft.Office.Interop.Word;
namespace ConsoleUmlaut
{
class Program
{
static readonly string[] Umlaute = { "ae", "oe", "ue", "ss", "Ae", "Oe", "Ue" };
static Dictionary<string, string> Values = new Dictionary<string, string>() { };
static void Main(string[] args)
{
Values = new Dictionary<string, string>();
Values.Add("ae", "ä");
Values.Add("oe", "ö");
Values.Add("ue", "ü");
Values.Add("ss", "ß");
Values.Add("Ae", "Ä");
Values.Add("Oe", "Ö");
Values.Add("Ue", "Ü");
string text = "Stossdaempfer Steuerpruefung Suessstofffresssucht Uebergroessentraeger";
string[] split = text.Split(' ', '\t', '\r', '\n', ',', ';', '.', '?', '!', '-');
int words = split.Length;
IEnumerable<IEnumerable<string>> allPossibilities = CreatePossibilities(split);
IEnumerable<string> correctWords = GetCorrectWords(allPossibilities);
foreach(string word in correctWords)
{
Console.WriteLine(word);
}
Console.ReadLine();
}
static IEnumerable<List<bool>> CheckSpelling(IEnumerable<IEnumerable<string>> words)
{
Word.Application app = new Word.Application();
foreach (IEnumerable<string> word in words)
{
List<bool> list = new List<bool>();
foreach (string s in word)
{
list.Add(app.CheckSpelling(s));
}
yield return list;
}
app.Quit();
}
static IEnumerable<string> GetCorrectWords(IEnumerable<IEnumerable<string>> allPossibilties)
{
var checkedPossibilties = CheckSpelling(allPossibilties);
int curList = 0;
foreach (var item in checkedPossibilties)
{
int index = item.LastIndexOf(true);
if (index == -1)
{ index = 0; }
string word = allPossibilties.ElementAt(curList).ElementAt(index);
curList++;
yield return word;
}
}
static IEnumerable<IEnumerable<string>> CreatePossibilities(string[] words)
{
foreach (string word in words)
{
yield return CreatePossibilties(word);
}
}
static IEnumerable<string> CreatePossibilties(string word)
{
string[] split = SplitAtUmlaut(word);
if (split == null)
{
yield return word;
}
else
{
int possibility = (int)Math.Pow(2, split.Length);
bool[] getsMapped = new bool[split.Length];
string[] temp = new string[split.Length];
for (int i = 0; i != possibility; i++)
{
split.CopyTo(temp, 0); ;
string s = Convert.ToString(i, 2).PadLeft(split.Length, '0');
var table = GetTable(s).ToList();
for (int b = 0; b != table.Count; b++)
{
if (table[b])
{
string part = split[b];
temp[b] = Replace(part);
}
}
string possible = RebuildString(temp);
yield return possible;
}
}
}
static string Replace(string text)
{
string key = text;
foreach (var item in Values)
{
key = key.Replace(item.Key, item.Value);
}
return key;
}
static string RebuildString(string[] source)
{
string text = null;
foreach (string s in source)
{
text += s;
}
return text;
}
static IEnumerable<bool> GetTable(string s)
{
char[] values = s.ToCharArray();
for (int i = 0; i != values.Length; i++)
{
if (values[i] == '1')
{
yield return true;
}
else if (values[i] == '0')
{ yield return false; }
else
{ throw new ArgumentException("Der string darf nur die Zeichen '0' und '1' beinhalten."); }
}
}
static string[] SplitAtUmlaut(string text)
{
List<int> indexes = FindAllIndexes(text, Umlaute).ToList();
int minus = FindAllIndexes(text, new string[1] { "sss" }).Count();
int count = indexes.Count - minus;
if (count == 0)
{ return null; }
else if (count == 1)
{ return new string[1] { text }; }
else
{
string[] split = new string[count];
int first = indexes.First();
int last = indexes.Last();
int length = 0;
int startIndex = 0;
for (int i = 0; i != count; i++)
{
int next = 0;
if (i == count - 1)
{ length = text.Length - startIndex; }
else if (i == 0 && indexes[i + 1] - indexes[i] == 1)
{ length = indexes[1] + 1; next = length; }
else if (indexes[i + 1] - indexes[i] == 1)
{ length = 2; next = startIndex + 2; }
else if (i == 0 && indexes[i] != 0)
{ length = indexes[1]; next = length; }
else
{ length = indexes[i + 1] - indexes[i]; next = indexes[i + 1]; }
split[i] = text.Substring(startIndex, length);
startIndex = next;
}
return split;
}
}
static IEnumerable<int> FindAllIndexes(string source, params string[] param)
{
int index = 0;
int i = 0;
bool run = true;
while (run)
{
List<int> temp = new List<int>();
foreach (string par in param)
{
temp.Add(source.IndexOf(par, i));
}
if (temp.Max() == -1)
{ break; }
index = (from t in temp where t > -1 select t).Min();
i = index + 1;
yield return index;
}
}
}
}
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1
