public bool Save(MySqlDataManipulator manipulator, int companyId)
        {
            List <KeywordGroupEntry> toUpload = new List <KeywordGroupEntry>();

            foreach (KeywordGroup group in ContainedGroups)
            {
                toUpload.Add(new KeywordGroupEntry(group.SelectedKeywords.ToString()));
            }
            List <KeywordGroupEntry> previous;

            previous = manipulator.GetCompanyComplaintGroups(companyId);
            bool res = manipulator.DeleteCompanyComplaintGroups(companyId);

            if (!res)
            {
                return(false);
            }
            res = manipulator.AddCompanyComplaintGroups(companyId, toUpload);
            if (!res)
            {
                res = manipulator.AddCompanyComplaintGroups(companyId, previous);
                if (!res)
                {
                    throw new Exception("Company " + companyId + " keyword clusterer failed to add previous" +
                                        " groups back after deletion. This is really bad.");
                }
                return(false);
            }
            return(true);
        }
        public bool Load(MySqlDataManipulator manipulator, int companyId)
        {
            if (ContainedGroups.Count > 0)
            {
                ContainedGroups.Clear();
            }
            List <KeywordGroupEntry> toLoad = manipulator.GetCompanyComplaintGroups(companyId);

            if (toLoad == null)
            {
                return(false);
            }
            foreach (KeywordGroupEntry entry in toLoad)
            {
                string       def        = entry.GroupDefinition;
                string[]     definition = def.Split(" ");
                KeywordGroup g          = new KeywordGroup(definition[0]);
                for (int i = 1; i < definition.Length; i++)
                {
                    g.SelectedKeywords.AddKeyword(definition[i]);
                }
                ContainedGroups.Add(g);
            }
            return(true);
        }
Beispiel #3
0
        /// <summary>
        /// Attempts to return a list of the top 3 most similar complaint groups from the database
        /// </summary>
        /// <param name="entryIn">The query to predict the most similar complaint groups of</param>
        /// <param name="manipulator">The object to use to access the database</param>
        /// <param name="companyId">The id of the company the request is being made for. Determines which tables to use in the database</param>
        /// <returns>Json formatted string that contains the top 3 complaint groups that are most similar to the query made, and their database ids</returns>
        public string ProcessQueryForComplaintGroups(RepairJobEntry entryIn, MySqlDataManipulator manipulator, int companyId, int numGroupsRequested = 3)
        {
            List <string>         tokens       = SentenceTokenizer.TokenizeSentence(entryIn.Complaint);
            List <List <string> > taggedTokens = KeywordTagger.Tag(tokens);
            List <string>         keywords     = KeywordPredictor.PredictKeywords(taggedTokens);
            KeywordExample        example      = new KeywordExample();

            foreach (string keyword in keywords)
            {
                example.AddKeyword(keyword);
            }
            KeywordClusterer.Load(manipulator, companyId);
            List <int> groups = KeywordClusterer.PredictTopNSimilarGroups(example, numGroupsRequested);
            List <KeywordGroupEntry> companyComplaintGroups = manipulator.GetCompanyComplaintGroups(companyId);

            if (companyComplaintGroups == null)
            {
                throw new NullReferenceException("Company " + companyId + " complaint groups were not available in database");
            }
            List <KeywordGroupEntry> ret = new List <KeywordGroupEntry>();
            bool uncategorizedAdded      = false;

            foreach (int i in groups)
            {
                if (i == 0 && !uncategorizedAdded)
                {
                    ret.Add(new KeywordGroupEntry("Uncategorized")
                    {
                        Id = 0
                    });
                    uncategorizedAdded = true;
                }
                else if (i != 0)
                {
                    companyComplaintGroups[i - 1].Id = i;
                    ret.Add(companyComplaintGroups[i - 1]);
                }
            }
            JsonListStringConstructor constructor = new JsonListStringConstructor();

            ret.ForEach(obj => constructor.AddElement(ConvertKeywordGroupEntry(obj)));
            return(constructor.ToString());

            JsonDictionaryStringConstructor ConvertKeywordGroupEntry(KeywordGroupEntry e)
            {
                JsonDictionaryStringConstructor r = new JsonDictionaryStringConstructor();

                r.SetMapping("GroupDefinition", e.GroupDefinition);
                r.SetMapping("Id", e.Id);
                return(r);
            }
        }