public override void PerformFunction(MySqlDataManipulator manipulator)
        {
            //Ensure that all KeywordPredictor models are loaded
            //If one is not, then a company requesting that model through its settings will cause an error
            if (!GlobalModelHelper.LoadOrTrainGlobalModels(ReflectionHelper.GetAllKeywordPredictors()))
            {
                throw new NullReferenceException("One or more global models failed to load. Server cannot start.");
            }
            DatabaseQueryProcessor processor = new DatabaseQueryProcessor(DatabaseQueryProcessorSettings.RetrieveCompanySettings(manipulator, CompanyId));

            List <RepairJobEntry> validatedData = manipulator.GetDataEntriesWhere(CompanyId, "id > 0", validated: true);
            List <string>         sentences;

            if (Flag.ToLower().Equals("complaint"))
            {
                //train model
                sentences = validatedData.Select(entry => entry.Complaint).ToList();
                if (!processor.TrainClusteringModels(manipulator, CompanyId, sentences, false))
                {
                    Console.WriteLine("Failed to train problem prediction models for company " + CompanyId);
                    return;
                }
                //register the complaint groups that the clusterer predicts with the repair job entry in the database
                foreach (RepairJobEntry entry in validatedData)
                {
                    string groups = JsonDataObjectUtil <List <int> > .ConvertObject(processor.PredictGroupsInJobData(entry, CompanyId, manipulator));

                    entry.ComplaintGroups = groups;
                    manipulator.UpdateDataEntryGroups(CompanyId, entry, complaint: true);
                }
            }
            Console.WriteLine("Trained clustering models for company " + CompanyId);
        }
Exemplo n.º 2
0
 static void PerformTraining()
 {
     try
     {
         while (true)
         {
             MySqlDataManipulator manipulator = new MySqlDataManipulator();
             if (!manipulator.Connect(MySqlDataManipulator.GlobalConfiguration.GetConnectionString()))
             {
                 throw new ArgumentException("MySqlDataManipulator failed to connect to the database");
             }
             Console.WriteLine("Checking company training statuses");
             List <CompanyId> companies = manipulator.GetCompaniesWithNamePortion("");
             foreach (CompanyId company in companies)
             {
                 if (manipulator.GetCountInTable(TableNameStorage.CompanyValidatedRepairJobTable.Replace("(n)", company.Id.ToString())) != 0)
                 {
                     DateTime             lastTrainedTime = DateTime.Parse(company.LastTrainedTime);
                     CompanySettingsEntry trainInterval   = manipulator.GetCompanySettingsWhere(company.Id, "SettingKey=\"" + CompanySettingsKey.RetrainInterval + "\"")[0];
                     bool shouldTrain = lastTrainedTime.AddDays(int.Parse(trainInterval.SettingValue)) <= DateTime.Now;
                     if (shouldTrain)
                     {
                         Console.WriteLine("Performing training for company " + company.LegalName);
                         DatabaseQueryProcessor processor = new DatabaseQueryProcessor(DatabaseQueryProcessorSettings.RetrieveCompanySettings(manipulator, company.Id));
                         CompanyModelUtils.TrainClusteringModel(manipulator, processor, company.Id, training: false);
                         company.LastTrainedTime = DateTime.Now.ToString();
                         manipulator.UpdateCompanyTrainingTime(company);
                         double automatedTestingResults = CompanyModelUtils.PerformAutomatedTesting(manipulator, company.Id, processor);
                         company.ModelAccuracy = (float)(100 - automatedTestingResults);
                         manipulator.UpdateCompanyAutomatedTestingResults(company);
                         Console.WriteLine("Accuracy after training: " + company.ModelAccuracy);
                     }
                 }
                 if (manipulator.GetCountInTable(TableNameStorage.CompanyNonValidatedRepairJobTable.Replace("(n)", company.Id.ToString())) != 0)
                 {
                     DateTime lastValidatedTime = DateTime.Parse(company.LastValidatedTime);
                     bool     shouldValidate    = lastValidatedTime.AddDays(14) <= DateTime.Now;
                     if (shouldValidate)
                     {
                         Console.WriteLine("Attempting to validate some non-validated data for company " + company.LegalName);
                         DatabaseQueryProcessor processor = new DatabaseQueryProcessor(DatabaseQueryProcessorSettings.RetrieveCompanySettings(manipulator, company.Id));
                         CompanyModelUtils.PerformDataValidation(manipulator, company.Id, processor);
                     }
                 }
             }
             manipulator.Close();
             Thread.Sleep(TimeSpan.FromMinutes(120));
         }
     }
     catch (ThreadInterruptedException)
     {
         Console.WriteLine("Retraining Thread Exiting");
     }
 }