private void OnRefresh() { if (!m_Refreshing) { m_ClassifiersFolder = Path.Combine(Application.dataPath, Config.Instance.ClassifierDirectory); if (!Directory.Exists(m_ClassifiersFolder)) { Directory.CreateDirectory(m_ClassifiersFolder); } m_ClassifierData = new List <ClassifierData>(); foreach (var file in Directory.GetFiles(m_ClassifiersFolder, "*.json")) { ClassifierData data = new ClassifierData(); if (data.Load(file)) { m_ClassifierData.Add(data); } } if (!m_NaturalLanguageClassifier.GetClassifiers(OnGetClassifiers)) { Log.Error("Natural Language Classifier Trainer", "Failed to request classifiers, please make sure your NaturalLanguageClassifierV1 service has credentials configured."); } else { m_Refreshing = true; } } }
private void Initialize() { if (!_isInitialized) { _filteredPatterns = Filters != null? Patterns.Where(p => Filters.All(f => f.PassFilter(p))).ToArray() : Patterns.ToArray(); _data = new ClassifierData { ClassFeature = Patterns.First().ClassFeature, TrainingInstances = TrainingInstances, AllPatterns = _filteredPatterns, }; if (SelectionPolicy != null) { SelectionPolicy.Data = _data; } if (VotesAggregator != null) { VotesAggregator.Data = _data; } if (VotesNormalizer != null) { VotesNormalizer.Data = _data; } } _isInitialized = true; }
private void OnRefresh() { if (!m_Refreshing) { if (m_ClassifiersFolder != null && !Directory.Exists(m_ClassifiersFolder)) { m_ClassifiersFolder = null; } if (m_ClassifiersFolder == null) { m_ClassifiersFolder = FindDirectory(Application.dataPath, CLASSIFIERS_DIRECTORY); } if (m_ClassifiersFolder == null) { m_ClassifiersFolder = Application.dataPath + "/Watson/Editor" + CLASSIFIERS_DIRECTORY; Directory.CreateDirectory(m_ClassifiersFolder); } m_ClassifierData = new List <ClassifierData>(); foreach (var file in Directory.GetFiles(m_ClassifiersFolder, "*.json")) { ClassifierData data = new ClassifierData(); if (data.Load(file)) { m_ClassifierData.Add(data); } } if (!m_NLC.GetClassifiers(OnGetClassifiers)) { Log.Error("NLCTrainer", "Failed to request classifiers, please make sure your NlcV1 service has credentials configured."); } else { m_Refreshing = true; } } }
private void OnGUI() { if (Event.current.type == EventType.repaint && !m_handleRepaintError) { m_handleRepaintError = true; return; } GUILayout.Label(m_WatsonIcon); m_ScrollPos = EditorGUILayout.BeginScrollView(m_ScrollPos); EditorGUILayout.BeginVertical(); if (m_Refreshing) { EditorGUI.BeginDisabledGroup(true); GUILayout.Button("Refreshing..."); EditorGUI.EndDisabledGroup(); } else if (m_ClassifierData == null || GUILayout.Button("Refresh")) { OnRefresh(); } EditorGUILayout.LabelField("Classifiers:"); //EditorGUI.indentLevel += 1; if (m_ClassifierData != null) { ClassifierData deleteClassifier = null; foreach (var data in m_ClassifierData) { EditorGUILayout.BeginHorizontal(); bool expanded = data.Expanded; data.Expanded = EditorGUILayout.Foldout(expanded, data.Name + " [Language: " + data.Language + "]"); if (data.Expanded != expanded) { data.Save(); } if (GUILayout.Button("Import", GUILayout.Width(100))) { var path = EditorUtility.OpenFilePanel("Select Training File", "", "csv"); if (!string.IsNullOrEmpty(path)) { try { data.Import(path); } catch { EditorUtility.DisplayDialog("Error", "Failed to load training data: " + path, "OK"); } } } if (GUILayout.Button("Export", GUILayout.Width(100))) { var path = EditorUtility.SaveFilePanel("Export Training file", Application.dataPath, "", "csv"); if (!string.IsNullOrEmpty(path)) { File.WriteAllText(path, data.Export()); } } if (GUILayout.Button("Save", GUILayout.Width(100))) { data.Save(); } if (GUILayout.Button("Delete", GUILayout.Width(100))) { if (EditorUtility.DisplayDialog("Confirm", "Please confirm you want to delete classifier: " + data.Name, "Yes", "No")) { deleteClassifier = data; } } if (GUILayout.Button("Train", GUILayout.Width(100))) { string classifierName = data.Name + "/" + DateTime.Now.ToString(); if (EditorUtility.DisplayDialog("Confirm", "Please confirm you want to train a new instance: " + classifierName, "Yes", "No")) { if (!m_NLC.TrainClassifier(classifierName, data.Language, data.Export(), OnClassiferTrained)) { EditorUtility.DisplayDialog("Error", "Failed to train classifier.", "OK"); } } } EditorGUILayout.EndHorizontal(); if (expanded) { EditorGUI.indentLevel += 1; bool instancesExpanded = data.InstancesExpanded; data.InstancesExpanded = EditorGUILayout.Foldout(instancesExpanded, "Instances"); if (instancesExpanded != data.InstancesExpanded) { data.Save(); } if (instancesExpanded) { EditorGUI.indentLevel += 1; if (m_Classifiers != null) { for (int i = 0; i < m_Classifiers.classifiers.Length; ++i) { Classifier cl = m_Classifiers.classifiers[i]; if (!cl.name.StartsWith(data.Name + "/")) { continue; } EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Name: " + cl.name); if (GUILayout.Button("Delete", GUILayout.Width(100))) { if (EditorUtility.DisplayDialog("Confirm", string.Format("Confirm delete of classifier {0}", cl.classifier_id), "YES", "NO") && !m_NLC.DeleteClassifer(cl.classifier_id, OnDeleteClassifier)) { EditorUtility.DisplayDialog("Error", "Failed to delete classifier.", "OK"); } } EditorGUILayout.EndHorizontal(); EditorGUI.indentLevel += 1; EditorGUILayout.LabelField("ID: " + cl.classifier_id); EditorGUILayout.LabelField("Created: " + cl.created.ToString()); EditorGUILayout.LabelField("Status: " + cl.status); EditorGUI.indentLevel -= 1; } } EditorGUI.indentLevel -= 1; } if (data.Data == null) { data.Data = new Dictionary <string, List <string> >(); } if (data.DataExpanded == null) { data.DataExpanded = new Dictionary <string, bool>(); } bool classesExpanded = data.ClassesExpanded; data.ClassesExpanded = EditorGUILayout.Foldout(classesExpanded, "Classes"); if (classesExpanded != data.ClassesExpanded) { data.Save(); } if (classesExpanded) { EditorGUI.indentLevel += 1; EditorGUILayout.BeginHorizontal(); m_NewClassName = EditorGUILayout.TextField(m_NewClassName); EditorGUI.BeginDisabledGroup(string.IsNullOrEmpty(m_NewClassName)); GUI.SetNextControlName("AddClass"); if (GUILayout.Button("Add Class", GUILayout.Width(100))) { data.Data[m_NewClassName] = new List <string>(); data.Save(); m_NewClassName = string.Empty; GUI.FocusControl("AddClass"); } EditorGUI.EndDisabledGroup(); EditorGUILayout.EndHorizontal(); string deleteClass = string.Empty; foreach (var kp in data.Data) { bool classExpanded = true; data.DataExpanded.TryGetValue(kp.Key, out classExpanded); EditorGUILayout.BeginHorizontal(); data.DataExpanded[kp.Key] = EditorGUILayout.Foldout(classExpanded, "Class: " + kp.Key); if (classExpanded != data.DataExpanded[kp.Key]) { data.Save(); } if (GUILayout.Button("Delete", GUILayout.Width(100))) { if (EditorUtility.DisplayDialog("Confirm", "Please confirm you want to delete class: " + kp.Key, "Yes", "No")) { deleteClass = kp.Key; } } EditorGUILayout.EndHorizontal(); if (classExpanded) { EditorGUI.indentLevel += 1; EditorGUILayout.BeginHorizontal(); m_NewPhrase = EditorGUILayout.TextField(m_NewPhrase); EditorGUI.BeginDisabledGroup(string.IsNullOrEmpty(m_NewPhrase)); GUI.SetNextControlName("AddPhrase"); if (GUILayout.Button("Add Phrase", GUILayout.Width(100))) { kp.Value.Add(m_NewPhrase); data.Save(); m_NewPhrase = string.Empty; GUI.FocusControl("AddPhrase"); } EditorGUI.EndDisabledGroup(); EditorGUILayout.EndHorizontal(); for (int i = 0; i < kp.Value.Count; ++i) { EditorGUILayout.BeginHorizontal(); kp.Value[i] = EditorGUILayout.TextField(kp.Value[i]); if (GUILayout.Button("Delete", GUILayout.Width(100))) { kp.Value.RemoveAt(i--); } EditorGUILayout.EndHorizontal(); } EditorGUI.indentLevel -= 1; } } if (!string.IsNullOrEmpty(deleteClass)) { data.Data.Remove(deleteClass); data.DataExpanded.Remove(deleteClass); data.Save(); } EditorGUI.indentLevel -= 1; } EditorGUI.indentLevel -= 1; } } if (deleteClassifier != null) { File.Delete(deleteClassifier.FileName); m_ClassifierData.Remove(deleteClassifier); AssetDatabase.Refresh(); } } //EditorGUI.indentLevel -= 1; EditorGUILayout.LabelField("Create Classifier:"); EditorGUI.indentLevel += 1; m_NewClassifierName = EditorGUILayout.TextField("Name", m_NewClassifierName); m_NewClassifierLang = EditorGUILayout.TextField("Language", m_NewClassifierLang); EditorGUI.BeginDisabledGroup(string.IsNullOrEmpty(m_NewClassifierLang) || string.IsNullOrEmpty(m_NewClassifierName)); if (GUILayout.Button("Create")) { m_NewClassifierName = m_NewClassifierName.Replace("/", "_"); string classifierFile = m_ClassifiersFolder + "/" + m_NewClassifierName + ".json"; if (!File.Exists(classifierFile) || EditorUtility.DisplayDialog("Confirm", string.Format("Classifier file {0} already exists, are you sure you wish to overwrite?", classifierFile), "YES", "NO")) { ClassifierData newClassifier = new ClassifierData(); newClassifier.Name = m_NewClassifierName; newClassifier.Language = m_NewClassifierLang; newClassifier.Save(classifierFile); m_NewClassifierName = string.Empty; OnRefresh(); } } EditorGUI.EndDisabledGroup(); EditorGUI.indentLevel -= 1; bool showAllClassifiers = m_DisplayClassifiers; m_DisplayClassifiers = EditorGUILayout.Foldout(showAllClassifiers, "All Classifier Instances"); if (showAllClassifiers) { EditorGUI.indentLevel += 1; if (m_Classifiers != null) { for (int i = 0; i < m_Classifiers.classifiers.Length; ++i) { Classifier cl = m_Classifiers.classifiers[i]; EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Name: " + cl.name); if (GUILayout.Button("Delete", GUILayout.Width(100))) { if (EditorUtility.DisplayDialog("Confirm", string.Format("Confirm delete of classifier {0}", cl.classifier_id), "YES", "NO") && !m_NLC.DeleteClassifer(cl.classifier_id, OnDeleteClassifier)) { EditorUtility.DisplayDialog("Error", "Failed to delete classifier.", "OK"); } } EditorGUILayout.EndHorizontal(); EditorGUI.indentLevel += 1; EditorGUILayout.LabelField("ID: " + cl.classifier_id); EditorGUILayout.LabelField("Created: " + cl.created.ToString()); EditorGUILayout.LabelField("Status: " + cl.status); EditorGUI.indentLevel -= 1; } } EditorGUI.indentLevel -= 1; } //EditorGUILayout.LabelField("Create Classifier:" ); //EditorGUI.indentLevel += 1; //m_NewClassifierName = EditorGUILayout.TextField("Name", m_NewClassifierName ); //m_NewClassifierLang = EditorGUILayout.TextField("Language", m_NewClassifierLang ); //EditorGUI.BeginDisabledGroup( string.IsNullOrEmpty(m_NewClassifierLang) || string.IsNullOrEmpty(m_NewClassifierName) ); //if ( GUILayout.Button( "Create" ) ) //{ // var path = EditorUtility.OpenFilePanel( "Select Training File", "", "csv" ); // if (! string.IsNullOrEmpty( path ) ) // { // string trainingData = File.ReadAllText( path ); // if (! string.IsNullOrEmpty( trainingData ) ) // { // string name = m_NewClassifierName; // if ( string.IsNullOrEmpty( name ) ) // name = DateTime.Now.ToString(); // if (! m_NLC.TrainClassifier( name, m_NewClassifierLang, trainingData, OnClassiferTrained ) ) // EditorUtility.DisplayDialog( "Error", "Failed to train classifier.", "OK" ); // } // else // EditorUtility.DisplayDialog( "Error", "Failed to load training data: " + path, "OK" ); // } // m_NewClassifierName = null; //} //EditorGUI.EndDisabledGroup(); //EditorGUI.indentLevel -= 1; EditorGUILayout.EndVertical(); EditorGUILayout.EndScrollView(); }