public static string DeTwist(string text, LangCook language, Action <double> progressHandler)
        {
            StringBuilder sb = new StringBuilder(), current = new StringBuilder();
            double        step     = 1d / text.Length;
            double        progress = 0;

            foreach (char _char in text)
            {
                if (char.IsLetter(_char))
                {
                    current.Append(_char);
                }
                else
                {
                    DeTwistWordAndWriteSolutions(current, sb, language);
                    current.Clear();
                    sb.Append(_char);
                }
                progressHandler(progress += step);
            }
            DeTwistWordAndWriteSolutions(current, sb, language);
            progressHandler(1);

            return(sb.ToString());
        }
Exemple #2
0
 public static LangCook WriteLangcook(string langcookPath, LangCook lang = null)
 {
     Directory.CreateDirectory(Path.GetDirectoryName(langcookPath));
     File.WriteAllLines(
         langcookPath,
         lang.words.SelectMany(
             pair => new[] { $"{pair.Value.Count}#{pair.Key}" }.Union(pair.Value))
         .ToList(),
         Encoding.UTF8);
     return(lang);
 }
        private static List <string> DeTwistWord(string text, LangCook language)
        {
            if (text.Length <= 3) // Worte mit maximal 3 Buchstaben können nicht getwistet werden, und somit auch nicht enttwistet
            {
                return(new List <string> {
                    text
                });
            }
            else
            {
                string textLower = text.ToLower();
                var    remainder = language[LangCook.GetKey(textLower)]
                                   .Select(x => new
                {
                    original = x,
                    body     = new StringBuilder(x.Substring(1, x.Length - 2))
                })
                                   .ToList();

                for (int i = 1; i < textLower.Length - 1; i++)
                {
                    for (int j = 0; j < remainder.Count; j++)
                    {
                        int index = remainder[j].body.ToString().IndexOf(textLower[i]);

                        if (index == -1)
                        {
                            remainder.RemoveAt(j--);
                        }
                        else
                        {
                            remainder[j].body.Remove(index, 1);
                        }
                    }
                }

                return(remainder.Select(x => x.original.CopyCase(text)).ToList());
            }
        }
        private static void DeTwistWordAndWriteSolutions(StringBuilder word, StringBuilder sb, LangCook language)
        {
            if (word.Length == 0)
            {
                return;
            }

            var solutions = DeTwistWord(word.ToString(), language);

            if (solutions.Count == 1) // Eindeutige Lösung gefunden
            {
                sb.Append(solutions[0]);
            }
            else if (solutions.Count > 1) // Mehrere Lösungen gefunden
            {
                sb.Append($"[AMBIGUOUSITY {solutions.Aggregate((x, y) => $"{x}, {y}")}]");
            }
            else // Keine Lösungen gefunden
            {
                sb.Append($"[ERROR {word.ToString()}]");
            }
        }