Esempio n. 1
0
        public ActivityResult Run(PipelineContext context)
        {
            var            pipe   = context.Pipeline as NewsAnalysisPipeline;
            ActivityResult result = new ActivityResult();
            IDictionary <long, SentimentResult> dict = new Dictionary <long, SentimentResult>();

            SentimentAnalyzer analyzer = new SentimentAnalyzer();

            if (pipe != null)
            {
                var newsList = context[pipe.NewsContextKey] as IEnumerable <NewsStream>;

                if (newsList != null)
                {
                    var textList = newsList.Select(i => new KVPair <string, string> {
                        Key = i.Id.ToString(), Value = i.NewsArticleDescription
                    }).ToList();
                    var sentiment = analyzer.BatchAnalyze(textList).Result;
                    for (var i = 0; i < textList.Count; i++)
                    {
                        var id = newsList.ElementAt(i).Id;
                        dict.Add(id, sentiment.FirstOrDefault(s => s.Key == id.ToString())?.Value);
                    }
                }
            }

            result.Result     = dict;
            result.ObjectType = dict.GetType();
            return(result);
        }
Esempio n. 2
0
        public void TestMethod1()
        {
            System.Collections.ArrayList al = Utils.NEGATIVE_WORDS;
            System.Collections.ArrayList nl = new System.Collections.ArrayList();

            SentimentAnalyzer sa = new SentimentAnalyzer("this is a test");

            sa.analyse();
        }
 private void btnAnalyze_Click(object sender, EventArgs e)
 {
     if (string.IsNullOrEmpty(txtParsedText.Text))
     {
         MessageBox.Show("No parsed text found, please choose a WAV file and parse it.", AppConfig.GetConfigValue("APP_MSG_TITLE"), MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }
     using (var analyzer = new SentimentAnalyzer(SentimentAnalyzeSource.Text, txtParsedText.Text.Trim()))
         txtAudioResult.Text = analyzer.Analyze().ToString();
 }
Esempio n. 4
0
        public MainForm()
        {
            InitializeComponent();

            _preprocessor = new TextPreprocessor(new EnglishStemmer());
            _analyzer     = new SentimentAnalyzer(_preprocessor);
            _logs         = new SortableBindingList <SentimentLog>();

            Helpers.ApplyStyle(ref dgvLogs);
            dgvLogs.DataSource = _logs;
        }
Esempio n. 5
0
        public async Task <ActionResult> TwitterSearchAsync(string query, int maxNumberOfResults, int dataSourceId)
        {
            TwitterAdapter.ConsumerKey    = System.Web.HttpContext.Current.Application["TwitterConsumerKey"].ToString();
            TwitterAdapter.ConsumerSecret = System.Web.HttpContext.Current.Application["TwitterConsumerSecret"].ToString();

            var results = await TwitterAdapter.SearchAsync(query, maxNumberOfResults);

            var json = JsonConvert.SerializeObject(results);

            DEV_DB_LICEntities2 db = new DEV_DB_LICEntities2();

            UserSearch usearch = new UserSearch()
            {
                QueryText = query, DataSourceId = dataSourceId, JsonResult = json, CreatedOn = DateTime.Now
            };

            db.UserSearches.Add(usearch);

            /*var model = new TwitterSearch() { Query = query, ResultsXml = results };*/

            db.SaveChanges();

            db.Database.ExecuteSqlCommand("usp_ImportTweetsBySearchId @param1", new SqlParameter("param1", usearch.Id));

            var resultsList = db.SearchResults.Where(r => r.UserSearchId == usearch.Id);

            List <TweetSentiment> tweetSentiments = new List <TweetSentiment>();

            foreach (var r in resultsList)
            {
                tweetSentiments.Add(new TweetSentiment(r.Id.ToString(), r.TweetText));
            }

            string apiKey = System.Web.HttpContext.Current.Application["CognitiveServicesKey"].ToString();

            var sentimentResults = await SentimentAnalyzer.AnalyzeAsync(tweetSentiments, apiKey);

            foreach (var s in sentimentResults)
            {
                var recordToUpdate = db.SearchResults.SingleOrDefault(r => r.Id.ToString() == s.Id);
                if (recordToUpdate != null)
                {
                    recordToUpdate.Score      = s.Score;
                    recordToUpdate.KeyPhrases = s.KeyPhrases;
                }
            }

            db.SaveChanges();

            return(this.RedirectToAction("Details", "UserSearches", new { id = usearch.Id }));
        }
Esempio n. 6
0
        /// <summary>
        /// This is the main entry point for your service replica.
        /// This method executes when this replica of your service becomes primary and has write status.
        /// </summary>
        /// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service replica.</param>
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            IReliableDictionary <string, string> myDictionary = await StateManager.GetOrAddAsync <IReliableDictionary <string, string> >("offsetDictionary");

            var            eventHubClient = EventHubClient.CreateFromConnectionString(Config.EhConnectionString, Config.EhCommentPath);
            EventHubClient publishClient  = EventHubClient.CreateFromConnectionString(Config.EhConnectionString, Config.EhAnalyticPath);

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                using (ITransaction tx = StateManager.CreateTransaction())
                {
                    StateHandler stateHandler = new StateHandler(tx, StateManager);

                    EventReader reader = new EventReader(eventHubClient, stateHandler);

                    IEnumerable <AnalysedRedditComment> comments = await reader.GetComments();

                    IEnumerable <IReadOnlyDictionary <string, AnalysedRedditComment> > batches = BatchComments(comments, BatchSize);

                    foreach (IReadOnlyDictionary <string, AnalysedRedditComment> batch in batches)
                    {
                        LanguageAnalyzer  languageAnalyzer  = new LanguageAnalyzer();
                        SentimentAnalyzer sentimentAnalyzer = new SentimentAnalyzer();
                        KeyPhraseAnalyzer keyPhraseAnalyzer = new KeyPhraseAnalyzer();

                        await languageAnalyzer.DoAnalysis(batch); // do language analysis first

                        await sentimentAnalyzer.DoAnalysis(batch);

                        await keyPhraseAnalyzer.DoAnalysis(batch);

                        ProcessMessages(batch.Values, publishClient);
                    }

                    // If an exception is thrown before calling CommitAsync, the transaction aborts, all changes are
                    // discarded, and nothing is saved to the secondary replicas.
                    await tx.CommitAsync();
                }

                await Task.Delay(TimeSpan.FromSeconds(PullDelay), cancellationToken);
            }
            // ReSharper disable once FunctionNeverReturns
        }
        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. 8
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);
        }
    // Use this for initialization
    void Start()
    {
        uiManagerScript = uiManager.GetComponent <UIManager> ();

        // Initialize the current emotion
        // currentFacialEmotion = new EmotionStruct();
        // currentWordSentimentEmotion = new EmotionStruct();
        // currentVocalEmotion = new ToneAnalysis();

        // Enable and disable emotion modality game objects based on the flags
        // if (useFacialEmotion)
        // {
        //  facialEmotionAnalyzerObject.SetActive(false);
        // }

        // if (useWordSentimentEmotion)
        // {
        //  wordSentimentEmotionAnalyzerObject.SetActive(false);
        // }

        // if (useVocalToneEmotion)
        // {
        //  vocalEmotionAnalyzerObject.SetActive(false);
        // }

        // // Find the script for facial emotion analysis
        // try
        // {
        //  facialAnalyzer = (FacialEmotionAnalyzer) facialEmotionAnalyzerObject.GetComponent(typeof(FacialEmotionAnalyzer)); // this seems to fail silently...
        // }
        // catch (System.Exception)
        // {
        //  Debug.Log("Unable to find facial emotion analyzer. This functionality will be disabled.");
        //  useFacialEmotion = false;
        // }

        // // Find the script for sentiment analysis of speech to text results
        // try
        // {
        //  wordAnalyzer = (SentimentAnalyzer) wordSentimentEmotionAnalyzerObject.GetComponent(typeof(SentimentAnalyzer));
        // }
        // catch (System.Exception)
        // {
        //  Debug.Log("Unable to find the sentiment analyzer. This functionality will be disabled.");
        //  useWordSentimentEmotion = false;
        // }
        // // Find the script for vocal analysis results
        // try
        // {
        //  vocalAnalyzer = (MicControlC) vocalEmotionAnalyzerObject.GetComponent(typeof(MicControlC));
        // }
        // catch (System.Exception)
        // {
        //  Debug.Log("Unable to find the vocal analyzer. This functionality will be disabled.");
        //  useVocalToneEmotion = false;
        // }


        if (!useFacialEmotion)
        {
            facialEmotionAnalyzerObject.SetActive(false);
        }
        else
        {
            facialAnalyzer  = (FacialEmotionAnalyzer)facialEmotionAnalyzerObject.GetComponent(typeof(FacialEmotionAnalyzer));
            facialAnalyzer2 = (FacialEmotionAnalyzer2)facialEmotionAnalyzer2Object.GetComponent(typeof(FacialEmotionAnalyzer2));
            Debug.Log(facialAnalyzer2);
        }

        if (!useWordSentimentEmotion)
        {
            wordSentimentEmotionAnalyzerObject.SetActive(false);
        }
        else
        {
            wordAnalyzer = (SentimentAnalyzer)wordSentimentEmotionAnalyzerObject.GetComponent(typeof(SentimentAnalyzer));
        }

        if (!useVocalToneEmotion)
        {
            vocalEmotionAnalyzerObject.SetActive(false);
        }
        else
        {
            vocalAnalyzer = (MicControlC)vocalEmotionAnalyzerObject.GetComponent(typeof(MicControlC));
            StartCoroutine(passAudio());
        }
    }