Exemple #1
0
        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.Handled          = true;
                e.SuppressKeyPress = true;

                textBox2.Text = textBox2.Text.Trim();

                PushString("dc-koromo@hitomi-copy$ " + textBox2.Text);
                string cmd = textBox2.Text.Trim().Split(' ')[0];

                if (cmd == "enum" || cmd == "enumi" || cmd == "enumx")
                {
                    if (textBox2.Text.Trim().Split(' ').Length == 1)
                    {
                        foreach (var f in Application.OpenForms)
                        {
                            PushString(f.GetType().Name);
                        }
                    }
                    else
                    {
                        try
                        {
                            string frm_name = textBox2.Text.Trim().Split(' ')[1];
                            if (cmd == "enum")
                            {
                                enum_recursion(Application.OpenForms[frm_name], textBox2.Text.Trim().Split(' '), 2, BindingFlags.Instance | BindingFlags.Public);
                            }
                            else if (cmd == "enumi")
                            {
                                enum_recursion(Application.OpenForms[frm_name], textBox2.Text.Trim().Split(' '), 2, default_bf);
                            }
                            else if (cmd == "enumx")
                            {
                                enum_recursion(Application.OpenForms[frm_name], textBox2.Text.Trim().Split(' '), 2);
                            }
                        }
                        catch (Exception ex)
                        {
                            PushString(ex.Message);
                        }
                    }
                }
                else if (cmd == "get")
                {
                    string[] split = textBox2.Text.Trim().Split(' ');
                    if (split.Length >= 3)
                    {
                        string frm = split[1];
                        string var = split[2];

                        try
                        {
                            PushString(LogEssential.SerializeObject(get_recursion(Application.OpenForms[frm], split, 2)));
                        }
                        catch (Exception ex)
                        {
                            PushString(ex.Message);
                        }
                    }
                    else if (split.Length >= 2 && split[1] == "hitomi_analysis")
                    {
                        PushString(string.Join("\r\n", HitomiAnalysis.Instance.Rank.Select(p => $"{p.Item1} ({p.Item2})")));
                        PushString($"Artist Counts : {HitomiAnalysis.Instance.Rank.Count}");
                    }
                    else
                    {
                        PushString("using 'get (Form) (Variable1) [Variable2] ...'");
                    }
                }
                else if (cmd == "set")
                {
                    string[] split = textBox2.Text.Trim().Split(' ');
                    if (split.Length >= 4)
                    {
                        string frm = split[1];
                        string var = split[2];
                        string val = split[3];

                        try
                        {
                            set_recurion(Application.OpenForms[frm], split, 2);
                        }
                        catch (Exception ex)
                        {
                            PushString(ex.Message);
                        }
                    }
                    else
                    {
                        PushString("using 'set (Form) (Variable1) [Variable2] ... [Value]'");
                    }
                }
                else if (cmd == "ra")
                {
                    string[] split = textBox2.Text.Trim().Split(' ');
                    if (split.Length > 1)
                    {
                        if (split[1] == "ulist")
                        {
                            PushString("User History");
                            HitomiAnalysisArtist user;
                            user = new HitomiAnalysisArtist(HitomiLog.Instance.GetEnumerator());
                            foreach (var pair in user.GetDictionary())
                            {
                                PushString($"{pair.Key} ({pair.Value})");
                            }
                        }
                        else if (split[1] == "list")
                        {
                            PushString("User Custom History");
                            if (HitomiAnalysis.Instance.CustomAnalysis != null)
                            {
                                HitomiAnalysis.Instance.CustomAnalysis.Sort((a, b) => a.Item2.CompareTo(b.Item2));
                                PushString(string.Join("\r\n", HitomiAnalysis.Instance.CustomAnalysis.Select(x => $"{x.Item1} ({x.Item2})")));
                            }
                        }
                        else if (split[1] == "clear")
                        {
                            HitomiAnalysis.Instance.CustomAnalysis.Clear();
                        }
                        else if (split[1] == "update")
                        {
                            HitomiAnalysis.Instance.Update();
                            (Application.OpenForms[0] as frmMain).UpdateNewStatistics();
                        }
                        else if (split[1] == "on")
                        {
                            HitomiAnalysis.Instance.UserDefined = true;
                        }
                        else if (split[1] == "off")
                        {
                            HitomiAnalysis.Instance.UserDefined = false;
                        }
                        else if (split[1] == "mion")
                        {
                            HitomiAnalysis.Instance.MustInclude = true;
                        }
                        else if (split[1] == "mioff")
                        {
                            HitomiAnalysis.Instance.MustInclude = false;
                        }
                        else if (split[1] == "rank")
                        {
                            PushString(string.Join("\r\n", HitomiAnalysis.Instance.Rank.ToArray().Reverse().Select(p => $"{p.Item1} ({p.Item2})")));
                            PushString($"Artist Counts : {HitomiAnalysis.Instance.Rank.Count}");
                        }
                        else if (split[1] == "add")
                        {
                            if (split.Length >= 3)
                            {
                                string artist = Regex.Replace(split[2], "_", " ");

                                bool found = false;
                                found = HitomiData.Instance.tagdata_collection.artist.Any(x => x.Tag == artist);

                                if (!found)
                                {
                                    PushString($"'{artist}' is not found.");
                                    string similar = "";
                                    int    diff    = int.MaxValue;
                                    HitomiData.Instance.tagdata_collection.artist.ForEach(x => { int diff_t = StringAlgorithms.get_diff(artist, x.Tag); if (diff_t < diff)
                                                                                                 {
                                                                                                     diff = diff_t; similar = x.Tag;
                                                                                                 }
                                                                                          });
                                    PushString($"Are you looking for '{similar}'?");
                                    return;
                                }

                                (Application.OpenForms[0] as frmMain).AddRecommendArtist(artist);
                            }
                            else
                            {
                                PushString("using 'ra add (artist)'");
                            }
                        }
                        else if (split[1] == "+")
                        {
                            if (split.Length >= 4)
                            {
                                try
                                {
                                    string tag = Regex.Replace(split[2], "_", " ");
                                    int    val = Convert.ToInt32(split[3]);

                                    bool found = false;
                                    found = HitomiData.Instance.tagdata_collection.female.Any(x => x.Tag == tag);
                                    if (found == false)
                                    {
                                        found = HitomiData.Instance.tagdata_collection.male.Any(x => x.Tag == tag);
                                    }
                                    if (found == false)
                                    {
                                        found = HitomiData.Instance.tagdata_collection.tag.Any(x => x.Tag == tag);
                                    }

                                    if (!found)
                                    {
                                        PushString($"'{tag}' is not found.");
                                        string similar = "";
                                        int    diff    = int.MaxValue;
                                        HitomiData.Instance.tagdata_collection.female.ForEach(x => { int diff_t = StringAlgorithms.get_diff(tag, x.Tag); if (diff_t < diff)
                                                                                                     {
                                                                                                         diff = diff_t; similar = x.Tag;
                                                                                                     }
                                                                                              });
                                        HitomiData.Instance.tagdata_collection.male.ForEach(x => { int diff_t = StringAlgorithms.get_diff(tag, x.Tag); if (diff_t < diff)
                                                                                                   {
                                                                                                       diff = diff_t; similar = x.Tag;
                                                                                                   }
                                                                                            });
                                        HitomiData.Instance.tagdata_collection.tag.ForEach(x => { int diff_t = StringAlgorithms.get_diff(tag, x.Tag); if (diff_t < diff)
                                                                                                  {
                                                                                                      diff = diff_t; similar = x.Tag;
                                                                                                  }
                                                                                           });
                                        PushString($"Are you looking for '{similar}'?");
                                        return;
                                    }

                                    if (HitomiAnalysis.Instance.CustomAnalysis.Any(x => x.Item1 == tag))
                                    {
                                        for (int i = 0; i < HitomiAnalysis.Instance.CustomAnalysis.Count; i++)
                                        {
                                            if (HitomiAnalysis.Instance.CustomAnalysis[i].Item1 == tag)
                                            {
                                                HitomiAnalysis.Instance.CustomAnalysis[i] = new Tuple <string, int>(tag, val);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        HitomiAnalysis.Instance.CustomAnalysis.Add(new Tuple <string, int>(tag, val));
                                    }
                                }
                                catch (Exception ex)
                                {
                                    PushString(ex.Message);
                                }
                            }
                            else if (split.Length == 3)
                            {
                                string tag = Regex.Replace(split[2], "_", " ");
                                List <Tuple <string, int, int> > diff = new List <Tuple <string, int, int> >();
                                HitomiData.Instance.tagdata_collection.female.ForEach(x => diff.Add(new Tuple <string, int, int>(x.Tag, StringAlgorithms.get_diff(tag, x.Tag), x.Count)));
                                HitomiData.Instance.tagdata_collection.male.ForEach(x => diff.Add(new Tuple <string, int, int>(x.Tag, StringAlgorithms.get_diff(tag, x.Tag), x.Count)));
                                HitomiData.Instance.tagdata_collection.tag.ForEach(x => diff.Add(new Tuple <string, int, int>(x.Tag, StringAlgorithms.get_diff(tag, x.Tag), x.Count)));
                                diff.Sort((a, b) => a.Item2.CompareTo(b.Item2));
                                for (int i = 5; i >= 0; i--)
                                {
                                    PushString($"{diff[i].Item1} [{diff[i].Item3}]");
                                }
                                return;
                            }
                            else
                            {
                                PushString("'+' command need 2 more parameters.");
                            }
                        }
                        else if (split[1] == "+a")
                        {
                            if (split.Length >= 3)
                            {
                                try
                                {
                                    string artist = Regex.Replace(split[2], "_", " ");

                                    bool found = false;
                                    found = HitomiData.Instance.tagdata_collection.artist.Any(x => x.Tag == artist);

                                    if (!found)
                                    {
                                        PushString($"'{artist}' is not found.");
                                        string similar = "";
                                        int    diff    = int.MaxValue;
                                        HitomiData.Instance.tagdata_collection.artist.ForEach(x => { int diff_t = StringAlgorithms.get_diff(artist, x.Tag); if (diff_t < diff)
                                                                                                     {
                                                                                                         diff = diff_t; similar = x.Tag;
                                                                                                     }
                                                                                              });
                                        PushString($"Are you looking for '{similar}'?");
                                        return;
                                    }

                                    foreach (var data in HitomiData.Instance.metadata_collection)
                                    {
                                        if (!HitomiSetting.Instance.GetModel().RecommendLanguageALL)
                                        {
                                            string lang = data.Language;
                                            if (data.Language == null)
                                            {
                                                lang = "N/A";
                                            }
                                            if (HitomiSetting.Instance.GetModel().Language != "ALL" &&
                                                HitomiSetting.Instance.GetModel().Language != lang)
                                            {
                                                continue;
                                            }
                                        }
                                        if (data.Artists != null && data.Tags != null && data.Artists.Contains(artist))
                                        {
                                            foreach (var tag in data.Tags)
                                            {
                                                if (HitomiAnalysis.Instance.CustomAnalysis.Any(x => x.Item1 == tag))
                                                {
                                                    for (int i = 0; i < HitomiAnalysis.Instance.CustomAnalysis.Count; i++)
                                                    {
                                                        if (HitomiAnalysis.Instance.CustomAnalysis[i].Item1 == tag)
                                                        {
                                                            HitomiAnalysis.Instance.CustomAnalysis[i] = new Tuple <string, int>(tag, HitomiAnalysis.Instance.CustomAnalysis[i].Item2 + 1);
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    HitomiAnalysis.Instance.CustomAnalysis.Add(new Tuple <string, int>(tag, 1));
                                                }
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    PushString(ex.Message);
                                }
                            }
                            else
                            {
                                PushString("'+a' command need 1 more parameters.");
                            }
                        }
                        else if (split[1] == "-")
                        {
                            if (split.Length >= 3)
                            {
                                string tag = Regex.Replace(split[2], "_", " ");
                                for (int i = 0; i < HitomiAnalysis.Instance.CustomAnalysis.Count; i++)
                                {
                                    if (HitomiAnalysis.Instance.CustomAnalysis[i].Item1 == tag)
                                    {
                                        HitomiAnalysis.Instance.CustomAnalysis.RemoveAt(i--);
                                    }
                                }
                            }
                        }
                        else
                        {
                            PushString($"'{split[1]}' is not a valid command option.");
                        }
                    }
                    else
                    {
                        PushString("using 'ra (option) [tag] [count] ...'");
                        PushString("  (option): ulist, list, clear, update, on, off, mion, mioff, rank, add, +, +a");
                    }
                }
                else if (cmd == "help")
                {
                    PushString("Realtime Variable Update System");
                    PushString("Copyright (C) 2018. Hitomi Parser Developers");
                    PushString("");
                    PushString("enum [Form] [Variable1] [Variable2] ... : Enumerate form or class members.");
                    PushString("enumi [Form] [Variable1] [Variable2] ... : Enumerate form or class members with private members.");
                    PushString("enumx [Form] [Variable1] [Variable2] ... : Enumerate all class members without static.");
                    PushString("get (Form|hitomi_analysis) (Variable1) [Variable2] ... : Get value.");
                    PushString("set (Form) (Variable1) [Variable2] ... [Value] : Set value.");
                    PushString("fucs : Frequently Used Command Snippet");
                    PushString("ra (option) [var1] [var2] ... : Recommend artists tools.");
                }
                else if (cmd == "fucs")
                {
                    PushString("get frmMain latest_load_count");
                    PushString("   - get Recommend scroll status");
                    PushString("set frmMain latest_load_count 100");
                    PushString("   - set Recommend scroll status to 100");
                }
                else
                {
                    PushString("Command not found. Try 'help' command.");
                }

                cmd_stack.Insert(0, textBox2.Text + " ");
                textBox2.Text = "";
                stack_pointer = 0;
            }
            else if (e.KeyCode == Keys.Up)
            {
                if (textBox2.Text == "")
                {
                    stack_pointer = 0;
                }
                if (stack_pointer >= cmd_stack.Count)
                {
                    stack_pointer = cmd_stack.Count - 1;
                }
                if (stack_pointer >= 0)
                {
                    textBox2.Text = cmd_stack[stack_pointer];
                    Application.DoEvents();
                    textBox2.Focus();
                    textBox2.SelectionStart  = Math.Max(0, textBox2.Text.Length * 10);
                    textBox2.SelectionLength = 0;
                    textBox2.Focus();
                }
                stack_pointer++;
            }
            else if (e.KeyCode == Keys.Down)
            {
                textBox2.SelectionStart  = Math.Max(0, textBox2.Text.Length - 1);
                textBox2.SelectionLength = 0;
            }
        }
Exemple #2
0
        public void Update()
        {
            HitomiAnalysisArtist user;

            user = new HitomiAnalysisArtist(CustomAnalysis);

            ///////////////////////////////

            Dictionary <string, Tuple <double, HitomiAnalysisArtist> > score = new Dictionary <string, Tuple <double, HitomiAnalysisArtist> >();
            bool rms = HitomiSetting.Instance.GetModel().UsingRMSAanlysis;
            bool cos = HitomiSetting.Instance.GetModel().UsingCosineAnalysis;

            if (rms && cos)
            {
                System.Windows.Forms.MessageBox.Show("[작가 추천 설정 오류] rms, cos는 동시에 사용할 수 없습니다.", "Hitomi Copy", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return;
            }

            ///////////////////////////////

            if (rms == true && user.GetDictionary().Count != 0)
            {
                double x_mean = user.GetDictionary().Sum(x => x.Value) / user.GetDictionary().Count;

                double va = user.GetDictionary().Sum(x => (x.Value - x_mean) * (x.Value - x_mean));

                foreach (var data in HitomiAnalysis.Instance.datas)
                {
                    if (data.GetDictionary().Count == 0)
                    {
                        continue;
                    }
                    double y_mean = data.GetDictionary().Sum(x => x.Value) / data.GetDictionary().Count;

                    double           a   = 0.0;
                    HashSet <string> map = new HashSet <string>();

                    // 기본 형태는 rms 기반이나, rms는 예측을 하는 방법이지, 유사도 측정 방법이 아님
                    // 따라서 계산에 사용되지 않는 태그들은 비율에 따라 반전시켜 계산하면
                    // tangent가 요동치므로 신뢰성있는 결과를 얻을 수 있을 것 같음 ㅇㅅㅇ
                    foreach (var pair in user.GetDictionary())
                    {
                        if (data.IsExsit(pair.Key))
                        {
                            a += (data.GetRate(pair.Key) - y_mean) * (pair.Value - x_mean);
                        }
                        else
                        {
                            a += -y_mean * (pair.Value - x_mean) * (pair.Value - x_mean);
                        }
                        map.Add(pair.Key);
                    }

                    foreach (var pair in data.GetDictionary())
                    {
                        if (!map.Contains(pair.Key))
                        {
                            a += -x_mean *x_mean *Math.Abs(data.GetRate(pair.Key) - y_mean);
                        }
                    }

                    if (a / va >= 1.0)
                    {
                        score.Add(data.Aritst, new Tuple <double, HitomiAnalysisArtist>(va / a * 100, data));
                    }
                    else if (a / va > 0.0)
                    {
                        score.Add(data.Aritst, new Tuple <double, HitomiAnalysisArtist>(a / va * 100, data));
                    }
                    else
                    {
                        double v = -a / va;
                        if (v >= 1.0)
                        {
                            score.Add(data.Aritst, new Tuple <double, HitomiAnalysisArtist>(va / a * 100, data));
                        }
                        else
                        {
                            score.Add(data.Aritst, new Tuple <double, HitomiAnalysisArtist>(a / va * 100, data));
                        }
                    }
                }
            }
            else if (cos == true && user.GetDictionary().Count != 0)
            {
                double s_user = Math.Sqrt(user.GetDictionary().Sum(x => x.Value * x.Value));

                foreach (var data in HitomiAnalysis.Instance.datas)
                {
                    double s_data = Math.Sqrt(data.GetDictionary().Sum(x => x.Value * x.Value));
                    double dist   = 0.0;

                    if (s_user * s_data == 0.0)
                    {
                        continue;
                    }

                    foreach (var pair in user.GetDictionary())
                    {
                        if (data.IsExsit(pair.Key))
                        {
                            dist += data.GetRate(pair.Key) * pair.Value;
                        }
                    }

                    score.Add(data.Aritst, new Tuple <double, HitomiAnalysisArtist>(dist / (s_user * s_data) * 100, data));
                }
            }

            ///////////////////////////////

            var list = score.ToList();

            list.Sort((p1, p2) => p2.Value.Item1.CompareTo(p1.Value.Item1));

            if (Rank != null)
            {
                Rank.Clear();
            }
            else
            {
                Rank = new List <Tuple <string, double, string> >();
            }

            foreach (var item in list)
            {
                Rank.Add(new Tuple <string, double, string>(item.Key, item.Value.Item1, item.Value.Item2.GetDetail(user)));
            }
        }