Пример #1
0
        private async void TextBox_data_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var fileName = MyLib.Open();

            if (fileName == null)
            {
                return;
            }
            var encodingSelected = MessageBox.Show("\"Yes\" to use UTF-8\r\n\"No\" to use UTF-16 (Unicode)", "", MessageBoxButton.YesNo);

            if (encodingSelected == MessageBoxResult.None)
            {
                return;
            }
            Log.Assert(encodingSelected == MessageBoxResult.Yes || encodingSelected == MessageBoxResult.No);
            var  encoding = encodingSelected == MessageBoxResult.Yes ? Encoding.UTF8 : Encoding.Unicode;
            int  dataPreprocessMethodId = radioPanel_newData.SelectedIndex;
            bool debugMode = (bool)checkBox_debugMode.IsChecked;
            await stackPanel_tasksQueue.EnqueueTaskAsync($"Read Data {fileName} {encoding} {debugMode} {dataPreprocessMethodId}", new Func <Task>(async() =>
            {
                mainData = await Task.Run(() => TextProcess.ReadTextStream(new FileStream(fileName, FileMode.Open, FileAccess.Read), encoding, debugMode));
                await Task.Run(() => TextProcess.Process(ref mainData, dataPreprocessMethodId));
                Log.Write(" OK");
            }));
        }
Пример #2
0
        private async void Button_new_Click(object sender, RoutedEventArgs e)
        {
            var fileName = MyLib.Open();

            if (fileName == null)
            {
                return;
            }
            var encodingSelected = MessageBox.Show("\"Yes\" to use UTF-8\r\n\"No\" to use UTF-16 (Unicode)", "", MessageBoxButton.YesNo);

            if (encodingSelected == MessageBoxResult.None)
            {
                return;
            }
            Log.Assert(encodingSelected == MessageBoxResult.Yes || encodingSelected == MessageBoxResult.No);
            var  encoding      = encodingSelected == MessageBoxResult.Yes ? Encoding.UTF8 : Encoding.Unicode;
            int  maxWordLength = int.Parse(inputField_data["maxWordLength"].Text);
            bool debugMode     = (bool)checkBox_debugMode.IsChecked;
            int  processMethod = radioPanel_newData.SelectedIndex;
            await stackPanel_tasksQueue.EnqueueTaskAsync($"BuildDataAsync({fileName},{encoding},{processMethod},{maxWordLength},{debugMode})", new Func <Task>(async() => await Task.Run(() => BuildData(
                                                                                                                                                                                             new FileStream(fileName, FileMode.Open, FileAccess.Read),
                                                                                                                                                                                             encoding,
                                                                                                                                                                                             processMethod,
                                                                                                                                                                                             maxWordLength,
                                                                                                                                                                                             debugMode))));
        }
Пример #3
0
        public void AddField(string title, string content)
        {
            MyLib.Assert(!textBoxes.ContainsKey(title));
            var textBox = new TextBox {
                Text = content
            };

            textBoxes.Add(title, textBox);
            this.Children.Add(new Grid
            {
                ColumnDefinitions =
                {
                    new ColumnDefinition {
                        Width = new GridLength(1, GridUnitType.Star)
                    },
                    new ColumnDefinition {
                        Width = new GridLength(1, GridUnitType.Star)
                    }
                },
                Children =
                {
                    new Label {
                        Content = title
                    }.Set(0, 0),
                    textBox.Set(0, 1)
                }
            });
        }
            public static void Process(ref string data, int methodId)
            {
                var sb = new StringBuilder();

                switch (methodId)
                {
                case 0: return;

                case 1:    //non-chinese => space
                {
                    int  percentage = -1;
                    long progress = 0, total_progress = data.Length;
                    char last_char = '\0';
                    foreach (char c in data)
                    {
                        if (++progress * 100 / total_progress > percentage)
                        {
                            Log.WriteLine($"Preprocessing {++percentage}%: non-chinese => space");
                        }
                        if (!MyLib.IsChinese(c))
                        {
                            if (last_char != ' ')
                            {
                                sb.Append(last_char = ' ');
                            }
                        }
                        else
                        {
                            sb.Append(last_char = c);
                        }
                    }
                    data = sb.ToString();
                    return;
                }

                case 2:    //non-chinese => removed
                {
                    int  percentage = -1;
                    long progress = 0, total_progress = data.Length;
                    char last_char = '\0';
                    foreach (char c in data)
                    {
                        if (++progress * 100 / total_progress > percentage)
                        {
                            Log.WriteLine($"Preprocessing {++percentage}%: non-chinese => space");
                        }
                        if (MyLib.IsChinese(c))
                        {
                            sb.Append(last_char = c);
                        }
                    }
                    data = sb.ToString();
                    return;
                }

                default: throw new Exception($"Unknown methodId: {methodId}");
                }
            }
Пример #5
0
 void InitializeButtons(List <Tuple <string, Action> > bs)
 {
     bs.Add(new Tuple <string, Action>("Convert Encoding", async() =>
     {
         Encoding encodingSource, encodingTarget;
         var encodingList = GetProperties(typeof(Encoding)).Select(v => v as Encoding).ToList();
         if ((encodingSource = await Select(encodingList, $"Source Encoding ({encodingList.Count})")) == null ||
             (encodingTarget = await Select(encodingList, $"Target Encoding ({encodingList.Count})")) == null)
         {
             return;
         }
         await MyLib.OpenSave(async(fs, ss) =>
         {
             try
             {
                 Log.Indent();
                 Log.WriteLine($"Converting: {encodingSource} → {encodingTarget}");
                 using (var reader = new StreamReader(fs, encodingSource))
                 {
                     using (var writer = new StreamWriter(ss, false, encodingTarget))
                     {
                         const int bufLen = 1 << 20;
                         var buf          = new char[bufLen];
                         long progress    = 0, total_progress = reader.BaseStream.Length;
                         DateTime time    = DateTime.Now;
                         for (int n; (n = await reader.ReadAsync(buf, 0, buf.Length)) > 0;)
                         {
                             await writer.WriteAsync(buf, 0, n);
                             progress += n;
                             if ((DateTime.Now - time).TotalSeconds > 0.5)
                             {
                                 Log.WriteLine($"Converting: {(double)progress * 100 / total_progress}% {encodingSource} → {encodingTarget}");
                                 time = DateTime.Now;
                             }
                         }
                     }
                 }
                 Log.WriteLine($"Convert OK: {encodingSource} → {encodingTarget}");
             }
             finally { Log.Unindent(); }
         });
     }));
     bs.Add(new Tuple <string, Action>("Convert Trie to WordList (file to file)", async() =>
     {
         await MyLib.OpenSave((fs, ss) => Log.SubTask(async() =>
         {
             Log.WriteLine($"Reading... file size: {fs.Length}");
             var trie = new Trie();
             await Task.Run(() => trie.Load(new FileStream(fs, FileMode.Open, FileAccess.Read)));
             Log.WriteLine("Exporting...");
             await trie.ExportList(new FileStream(ss, FileMode.Create, FileAccess.Write));
             Log.WriteLine($"Done");
         }));
     }));
 }
Пример #6
0
        private async void Button_exportList_Click(object sender, RoutedEventArgs e)
        {
            var fileName = MyLib.Save();

            if (fileName == null)
            {
                return;
            }
            await stackPanel_tasksQueue.EnqueueTaskAsync($"Export Word List {fileName}", new Func <Task>(async() =>
            {
                Log.WriteLine("Exporting Word List...");
                await trie.ExportList(new FileStream(fileName, FileMode.Create, FileAccess.Write));
                Log.Write(" OK");
            }));
        }
Пример #7
0
        private async void Button_load_Click(object sender, RoutedEventArgs e)
        {
            var fileName = MyLib.Open();

            if (fileName == null)
            {
                return;
            }
            await stackPanel_tasksQueue.EnqueueTaskAsync($"Read Trie {fileName}", new Func <Task>(async() =>
            {
                Log.WriteLine("Loading trie...");
                await Task.Run(() => trie.Load(new FileStream(fileName, FileMode.Open, FileAccess.Read)));
                Log.Write(" OK");
            }));
        }
Пример #8
0
        private async void Button_save_Click(object sender, RoutedEventArgs e)
        {
            var fileName = MyLib.Save();

            if (fileName == null)
            {
                return;
            }
            await stackPanel_tasksQueue.EnqueueTaskAsync($"Save Trie {fileName}", new Func <Task>(async() =>
            {
                Log.WriteLine("Saving trie...");
                await Task.Run(() => trie.Save(new FileStream(fileName, FileMode.Create, FileAccess.Write)));
                Log.Write(" OK");
            }));
        }
Пример #9
0
        private async void Button_wordsPerCount_Click(object sender, RoutedEventArgs e)
        {
            int targetWordLength = int.Parse(Microsoft.VisualBasic.Interaction.InputBox("targetWordLength?", "", "4"));
            var fileName         = MyLib.Save();

            if (fileName == null)
            {
                return;
            }
            await stackPanel_tasksQueue.EnqueueTaskAsync($"Words per Count {fileName} {targetWordLength}", async() => await Task.Run(() =>
            {
                long percent   = -1, progress = 0, total_progress = trie.Size;
                int wordLength = 0;
                SortedDictionary <long, long> ans = new SortedDictionary <long, long>();
                for (int i = 1; i <= 10; i++)
                {
                    ans[i] = 0;
                }
                trie.Traverse(c => wordLength++, () => wordLength--, cnt =>
                {
                    if (++progress * 100 / total_progress > percent)
                    {
                        Log.WriteLine($"Words per Count... {++percent}% {ans[1]} {ans[2]} {ans[3]} {ans[4]} {ans[5]} | {ans[6]} {ans[7]} {ans[8]} {ans[9]} {ans[10]}");
                    }
                    if (wordLength != targetWordLength)
                    {
                        return;
                    }
                    if (!ans.ContainsKey(cnt))
                    {
                        ans[cnt] = 0;
                    }
                    ans[cnt]++;
                });
                Log.WriteLine($"Writing...");
                using (StreamWriter writer = new StreamWriter(fileName, false, Encoding.UTF8))
                {
                    writer.WriteLine("Count,Words");
                    foreach (var p in ans)
                    {
                        writer.WriteLine($"{p.Key},{p.Value}");
                    }
                }
                Log.Write(" OK");
            }));
        }