public static void AnalyzeSearchWordSentiment(string indexPath, string field, string[] keywords, int printDocumentCnt = 10, string histogramField = null)
        {
            var searcher = LuceneOperations.GetIndexSearcher(indexPath);
            var reader   = searcher.GetIndexReader();
            var docIDs   = LuceneOperations.Search(searcher, StringOperations.GetMergedString(keywords, " "), field);

            Console.WriteLine("Find {0}% ({1}/{2}) documents containing: {3}", (100.0 * docIDs.Count / reader.NumDocs()), docIDs.Count, reader.NumDocs(), StringOperations.GetMergedString(keywords, " "));

            var              progress      = new ProgramProgress(docIDs.Count);
            var              sentiAnalyzer = new SentimentAnalyzer();
            SentimentType    sentimentType;
            double           sentimentScore;
            HeapSortDouble   hsdPos     = new HeapSortDouble(printDocumentCnt);
            HeapSortDouble   hsdNeg     = new HeapSortDouble(printDocumentCnt);
            Counter <string> counterPos = null;
            Counter <string> counterNeg = null;
            Counter <string> counterNeu = null;

            if (histogramField != null)
            {
                counterPos = new Counter <string>();
                counterNeg = new Counter <string>();
                counterNeu = new Counter <string>();
            }
            int posCnt = 0;
            int negCnt = 0;
            int neuCnt = 0;

            foreach (var docID in docIDs)
            {
                var document = reader.Document(docID);
                var content  = document.Get(field);
                sentiAnalyzer.GetSentiment(content, out sentimentType, out sentimentScore);

                switch (sentimentType)
                {
                case SentimentType.Positive:
                    posCnt++;
                    hsdPos.Insert(docID, Math.Abs(sentimentScore));
                    if (histogramField != null)
                    {
                        counterPos.Add(document.Get(histogramField));
                    }
                    break;

                case SentimentType.Negative:
                    negCnt++;
                    hsdNeg.Insert(docID, Math.Abs(sentimentScore));
                    if (histogramField != null)
                    {
                        counterNeg.Add(document.Get(histogramField));
                    }
                    break;

                case SentimentType.Neutral:
                    neuCnt++;
                    if (histogramField != null)
                    {
                        counterNeu.Add(document.Get(histogramField));
                    }
                    break;

                default:
                    throw new NotImplementedException();
                }

                progress.PrintIncrementExperiment();
            }

            Console.WriteLine("Positive document ratio {0}% ({1}/{2})", Math.Round(100.0 * posCnt / docIDs.Count), posCnt, docIDs.Count);
            Console.WriteLine("Negatvie document ratio {0}% ({1}/{2})", Math.Round(100.0 * negCnt / docIDs.Count), negCnt, docIDs.Count);
            Console.WriteLine("Neutral document ratio {0}% ({1}/{2})", Math.Round(100.0 * neuCnt / docIDs.Count), neuCnt, docIDs.Count);

            Console.WriteLine(StringOperations.WrapWithDash("Positive documents"));
            foreach (var kvp in hsdPos.GetSortedDictionary())
            {
                Console.WriteLine(kvp.Value + "\t" + reader.Document(kvp.Key).Get(field));
            }

            Console.WriteLine(StringOperations.WrapWithDash("Negative documents"));
            foreach (var kvp in hsdNeg.GetSortedDictionary())
            {
                Console.WriteLine(kvp.Value + "\t" + reader.Document(kvp.Key).Get(field));
            }

            progress.PrintTotalTime();

            if (histogramField != null)
            {
                string[]           featureStrings = new[] { "Pos", "Neg", "Neu" };
                Counter <string>[] counters       = new[] { counterPos, counterNeg, counterNeu };
                for (int i = 0; i < featureStrings.Length; i++)
                {
                    Console.WriteLine(StringOperations.WrapWithDash(histogramField + " " + featureStrings[i]));
                    int index = 0;
                    foreach (var kvp in counters[i].GetCountDictionary().OrderByDescending(kvp => kvp.Value))
                    {
                        Console.WriteLine(kvp.Key + "\t" + kvp.Value);
                        if (++index >= 100)
                        {
                            break;
                        }
                    }
                }
            }

            Console.ReadKey();
        }
Esempio n. 2
0
        public async Task LearningUserPreferencesIntent(IDialogContext context, LuisResult result)
        {
            if (firstAcces == true)
            {
                if (setPreferencesClose == true && confirmWait == false && changePreferenceUnderstand == false)
                {
                    if (result.Entities.Count > 0)
                    {
                        LinkedList <string> detectedSentences = await ProcessText.InvokeRequestResponseService(result.Query);

                        int j = 0;

                        LinkedList <string> .Enumerator enumeratorSentence = detectedSentences.GetEnumerator();

                        while (enumeratorSentence.MoveNext())
                        {
                            double?score = await SentimentAnalyzer.GetSentiment(enumeratorSentence.Current, Convert.ToString(j++));

                            //per ogni entità controllo se sia presente in questa frase
                            IEnumerator <EntityRecommendation> e = result.Entities.GetEnumerator();
                            while (e.MoveNext())
                            {
                                if (enumeratorSentence.Current.Contains(e.Current.Entity))
                                {
                                    entityScore.Add(e.Current, score);
                                }
                            }
                            e.Dispose();
                        }
                        enumeratorSentence.Dispose();

                        Dictionary <EntityRecommendation, double?> .KeyCollection            keys = entityScore.Keys;
                        Dictionary <EntityRecommendation, double?> .KeyCollection.Enumerator key  = keys.GetEnumerator();

                        //controllo se un entità è di più tipi
                        List <EntityRecommendation> copyEntity = new List <EntityRecommendation>(keys);
                        List <string> examinedKeys             = new List <string>();

                        List <EntityRecommendation> resultEntityMultipleType = new List <EntityRecommendation>();
                        try
                        {
                            while (key.MoveNext())
                            {
                                if (!examinedKeys.Contains(key.Current.Entity))
                                {
                                    resultEntityMultipleType = copyEntity.FindAll(x => x.Entity.Equals(key.Current.Entity));
                                    examinedKeys.Add(key.Current.Entity);
                                }
                                List <EntityRecommendation> .Enumerator enu = resultEntityMultipleType.GetEnumerator();

                                if (resultEntityMultipleType.Count == 1)
                                {
                                    if (getSentimentByEntity(key.Current) > 0.5)
                                    {
                                        await context.PostAsync($"I understand what you like {key.Current.Entity}! Confirm ? Respond with yes or no!");
                                    }
                                    else
                                    {
                                        await context.PostAsync($"I understand what you dislike {key.Current.Entity}! Confirm ? Respond with yes or no!");
                                    }

                                    resultEntityMultipleType.Clear();
                                    resultEntity = null;
                                    confirmWait  = true;
                                }

                                if (resultEntityMultipleType.Count > 1)
                                {
                                    string question = string.Empty;
                                    int    i        = 1;

                                    while (enu.MoveNext())
                                    {
                                        if (i == 1)
                                        {
                                            question = $"{enu.Current.Entity} as ";
                                            if (!entitiesPreferences.ContainsKey(enu.Current))
                                            {
                                                List <EntityRecommendation> copy = new List <EntityRecommendation>(resultEntityMultipleType);
                                                entitiesPreferences.Add(enu.Current, copy);
                                            }
                                        }

                                        if (i < resultEntityMultipleType.Count && i > 0 && !question.Contains(enu.Current.Type))
                                        {
                                            question += enu.Current.Type + " or ";
                                        }

                                        if (i == resultEntityMultipleType.Count && !question.Contains(enu.Current.Type))
                                        {
                                            question += enu.Current.Type;
                                        }
                                        if (i == resultEntityMultipleType.Count)
                                        {
                                            question += "?";
                                        }
                                        i++;
                                    }

                                    await context.PostAsync(question);

                                    setPreferencesClose = false;
                                }

                                List <EntityRecommendation> .Enumerator o = resultEntityMultipleType.GetEnumerator();
                                while (o.MoveNext())
                                {
                                    Debug.Print(o.Current.Entity + " " + o.Current.Type);
                                }
                                resultEntityMultipleType.Clear();
                            }
                        }
                        catch (Exception e) {
                            Debug.Print(e.Message);//questa eccezione mi serve per forzare l'uscita dal ciclo
                        }
                    }
                    else
                    {
                        await context.PostAsync("I don't understand who you say! Can you repeat please?");
                    }
                }
                else
                {
                    await context.PostAsync("Respond to the all question please");
                }
            }
            else
            {
                await context.PostAsync("Rude! Say hello!");
            }


            context.Wait(MessageReceived);
        }