Пример #1
0
        static void Main(string[] args)
        {
            // Install NuGet WordCloud 1.0.0.3
            // include using for WordCloud and System.Drawing.
            // The output JPG resolution is set here to 1024x768
            WordCloud.WordCloud wc = new WordCloud.WordCloud(1024, 768, true);

            // Provide the text, from which the word cloud will be generated
            var text = "Unsatisfactory Holiday at Hotel Balfour, Torrevieja on 12 August 2014 to 19 August 2014 Booking ref: 123456789 I have just returned from a holiday at Hotel Balfour, Torrevieja with my wife and children, which was most disappointing.Please find below a list of our complaints: 1) There was no shower in the hotel as specified in the brochure 2) The kitchens were closed for the whole of our stay 3) The hotel was 5 miles from the beach and not 1 mile as it said in the brochure We contacted your representative at the resort on 14 August 2015, but they were unable to resolve the matter and advised us to complain upon our return home.Under The Package Travel, Package Holidays and Package Tours Regulations 1992 you have a responsibility to provide all the elements of the package contracted for as they were described.We are legally entitled to receive compensation from you for loss of value, consequential losses and for the disappointment and loss of enjoyment we suffered.As you failed to provide us with the holiday we booked we are seeking £150 compensation from you for the problems we encountered, and for the distress and disappointment we suffered as a result.I have also sent a copy of this letter and enclosure to ABTA(of which I note you are a member).I look forward to receiving a response from you within 14 days of receipt of this letter.";

            // TODO: submit text to Azure Cognitive Services (Text Analytics) to exract key phrases and words. Below is a smaple of these words
            List <String> words = new List <string> {
                "Holiday", "Hotel Balfour", "Torrevieja", "disappointment", "letter",
                "compensation", "brochure", "complaints", "representative", "resort",
                "matter", "return home", "response", "problems", "distress", "result"
            };

            // TODO: write a mehtod to find the occurances/frequency of every key phrase in the text, Below is a sample of these frequencies
            List <int> frequencies = new List <int> {
                4, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1
            };

            wc.Draw(words, frequencies).Save(@".\WordCloud.JPG");
        }
Пример #2
0
        /// <summary>
        /// Writes various results to a StreamWriter.
        /// </summary>
        /// <param name="writer">A StreamWriter instance.</param>
        /// <param name="writeCommunityParameters">Set true to write community parameters.</param>
        /// <param name="writeWorkerParameters">Set true to write worker parameters.</param>
        /// <param name="writeWorkerCommunities">Set true to write worker communities.</param>
        /// <param name="writeProbWords">Set true to write word probabilities</param>
        /// <param name="topWords">Number of words to select</param>
        public void WriteResults(StreamWriter writer, bool writeCommunityParameters, bool writeWorkerParameters, bool writeWorkerCommunities, bool writeProbWords, int topWords = 30)
        {
            base.WriteResults(writer, writeCommunityParameters, writeWorkerCommunities, writeWorkerCommunities);
            DataMappingWords MappingWords = Mapping as DataMappingWords;

            if (writeProbWords && this.ProbWords != null)
            {
                int NumClasses      = ProbWords.Length;
                var classifiedWords = new Dictionary <string, KeyValuePair <string, double> >();
                for (int c = 0; c < NumClasses; c++)
                {
                    string className = string.Empty;
                    if (MappingWords != null)
                    {
                        if (MappingWords.WorkerCount > 100) // Assume it's CF
                        {
                            className = MappingWords.CFLabelName[c];
                        }
                        else
                        {
                            className = MappingWords.SPLabelName[c];
                        }
                        writer.WriteLine($"Class {className}");
                    }

                    Vector probs           = ProbWords[c].GetMean();
                    var    probsDictionary = probs.Select((value, index) => new KeyValuePair <string, double>(MappingWords.Vocabulary[index], Math.Log(value))).OrderByDescending(x => x.Value).ToArray();
                    topWords = Math.Min(topWords, probsDictionary.Length);
                    for (int w = 0; w < topWords; w++)
                    {
                        writer.WriteLine($"\t{probsDictionary[w].Key}: \t{probsDictionary[w].Value:0.000}");
                        if (!string.IsNullOrEmpty(className))
                        {
                            KeyValuePair <string, double> classifiedWord;
                            if (!classifiedWords.TryGetValue(probsDictionary[w].Key, out classifiedWord) ||
                                classifiedWord.Value < probsDictionary[w].Value)
                            {
                                classifiedWords[probsDictionary[w].Key] = new KeyValuePair <string, double>(className, probsDictionary[w].Value);
                            }
                        }
                    }
                }
                writer.WriteLine();

                //Create wordCloud
                var wordCloudPositif = new WordCloud.WordCloud(500, 500, true);
                var wordCloudNegatif = new WordCloud.WordCloud(500, 500, true);
                var wordCloudNeutral = new WordCloud.WordCloud(500, 500, true);
                var wordCloudUnknown = new WordCloud.WordCloud(500, 500, true);
                var wordCloud        = new WordCloud.WordCloud(500, 500, true);

                //Create ListWords
                List <String> wordsPositif = new List <string> {
                };
                List <String> wordsNegatif = new List <string> {
                };
                List <String> wordsNeutral = new List <string> {
                };
                List <String> wordsUnknown = new List <string> {
                };
                List <String> words        = new List <string> {
                };

                //Create ListFrequencies
                List <int> frequenciesPositif = new List <int> {
                };
                List <int> frequenciesNegatif = new List <int> {
                };
                List <int> frequenciesNeutral = new List <int> {
                };
                List <int> frequenciesUnknown = new List <int> {
                };
                List <int> frequencies        = new List <int> {
                };

                //Create Bitmap
                Bitmap myBitmapPositif = null;
                Bitmap myBitmapNegatif = null;
                Bitmap myBitmapNeutral = null;
                Bitmap myBitmapUnknown = null;
                Bitmap myBitmap        = null;

                writer.WriteLine($"Main classes:");
                foreach (var wordByClass in classifiedWords.GroupBy(classified => classified.Value.Key))
                {
                    writer.WriteLine($"Class {wordByClass.Key}:");


                    foreach (var word in wordByClass.OrderByDescending(w => w.Value.Value))
                    {
                        writer.WriteLine($"\t{word.Key}");
                        if (wordByClass.Key == "Negative")
                        {
                            wordsNegatif.Add($"{word.Key}");
                            frequenciesNegatif.Add(1);
                        }
                        else if (wordByClass.Key == "Neutral")
                        {
                            wordsNeutral.Add($"{word.Key}");
                            frequenciesNeutral.Add(1);
                        }
                        else if (wordByClass.Key == "Positive")
                        {
                            wordsPositif.Add($"{word.Key}");
                            frequenciesPositif.Add(1);
                        }
                        else if (wordByClass.Key == "NotRelated")
                        {
                            words.Add($"{word.Key}");
                            frequencies.Add(1);
                        }
                        else if (wordByClass.Key == "Unknown")
                        {
                            wordsUnknown.Add($"{word.Key}");
                            frequenciesUnknown.Add(1);
                        }
                    }
                }

                //Creation wordCloud Positif
                myBitmapPositif = new Bitmap(wordCloudPositif.Draw(wordsPositif, frequenciesPositif));
                myBitmapPositif.Save("C:/Users/athen/OneDrive/Documents/EPF/5A/IA/Sentimental-Analysis/WordClouds/Positive.jpg");

                //Creation wordCloud Negatif
                myBitmapNegatif = new Bitmap(wordCloudNegatif.Draw(wordsNegatif, frequenciesNegatif));
                myBitmapNegatif.Save("C:/Users/athen/OneDrive/Documents/EPF/5A/IA/Sentimental-Analysis/WordClouds/Negative.jpg");

                //Creation wordCloud Neutral
                myBitmapNeutral = new Bitmap(wordCloudNeutral.Draw(wordsNeutral, frequenciesNeutral));
                myBitmapNeutral.Save("C:/Users/athen/OneDrive/Documents/EPF/5A/IA/Sentimental-Analysis/WordClouds/Neutral.jpg");

                //Creation wordCloud Unkown
                myBitmapUnknown = new Bitmap(wordCloudUnknown.Draw(wordsUnknown, frequenciesUnknown));
                myBitmapUnknown.Save("C:/Users/athen/OneDrive/Documents/EPF/5A/IA/Sentimental-Analysis/WordClouds/Unknown.jpg");

                //Creation wordCloud NotRelated
                myBitmap = new Bitmap(wordCloud.Draw(words, frequencies));
                myBitmap.Save("C:/Users/athen/OneDrive/Documents/EPF/5A/IA/Sentimental-Analysis/WordClouds/notRelated.jpg");
            }
        }
Пример #3
0
        /*
        public class WordBitmap {
            public byte[] pixels;
            public int width;
            public int height;
            public int stride;

            private BitmapData bitmapData;
            private Bitmap bmps;

            public WordBitmap(Bitmap bmp) {
                //copy the pixels
                bitmapData = bmp.LockBits(
                    new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
                    ImageLockMode.ReadOnly,
                    PixelFormat.Format32bppArgb);
                int numbytes = Math.Abs(bitmapData.Stride) * bitmapData.Height;
                pixels = new byte[numbytes];

                // Copy the RGB values into the array.
                System.Runtime.InteropServices.Marshal.Copy(bitmapData.Scan0, pixels, 0, numbytes);
                stride = bitmapData.Stride;
                bmps = bmp;
                width = bmp.Width;
                height = bmp.Height;
            }

            public void Dispose() {
                bmps.UnlockBits(bitmapData);
                bitmapData = null;
                pixels = null;
            }
        } */
        public static void Main(string[] args)
        {
            string fnin = null;
            string fnout = null;
            string fontt = null;
            string svgfn = null;
            string pdffn = null;
            string cf = null;
            bool cd = false;

            for(int i=0;i<args.Length;i++) if(args[i][0] == '-') switch(args[i][1]) {
            case 'i':
                fnin = args[i+1];
                break;
            case 'o':
                fnout = args[i+1];
                break;
            case 'f':
                fontt = args[i+1];
                break;
            case 'c':
                cf = args[i+1];
                break;
            case 'd':
                cd = true;
                break;
            case 's':
                svgfn = args[i+1];
                break;
            case 'p':
                pdffn = args[i+1];
                break;
            default:
                Console.Error.WriteLine("Unknown switch: {0}!",args[i]);
                break;
            }

            if(cd) {
                if(cf == null) {
                    Console.Error.WriteLine("Error: No configfile specified!");
                    return;
                }
                StreamWriter sw = new StreamWriter(cf);
                WordCloud cloud1 = new WordCloud();
                XmlSerializer s = new XmlSerializer(typeof(WordCloud));
                s.Serialize(sw,cloud1);
                sw.Close();
                return;
            }

            if(fnin == null || (fnout == null && pdffn == null && svgfn == null)) {
                Console.Error.WriteLine("Error: no input or output file name given!");
                return;
            }

            WordCloud cloud;
            if(cf != null) {
                StreamReader s1 = new StreamReader(cf);
                XmlSerializer s = new XmlSerializer(typeof(WordCloud));
                cloud = (WordCloud)s.Deserialize(s1);
            }
            else cloud = new WordCloud();

            if(fontt != null) {
                cloud.fontname = fontt;
            }

            cloud.svgfn = svgfn;
            cloud.pdffn = pdffn;

            StreamReader sr = new StreamReader(fnin);

            Color c = new Color(0,0,0);
            int r,g,b;
            if(cloud.colorstring != null)
                if(cloud.colorstring.Length >= 7)
                    if(cloud.colorstring[0] == '#')
            if(int.TryParse(cloud.colorstring.Substring(1,2),System.Globalization.NumberStyles.AllowHexSpecifier,null,out r)
             && int.TryParse(cloud.colorstring.Substring(3,2),System.Globalization.NumberStyles.AllowHexSpecifier,null,out g)
             && int.TryParse(cloud.colorstring.Substring(5,2),System.Globalization.NumberStyles.AllowHexSpecifier,null,out b))
                            c = new Color( ((double)r) / 255.0, ((double)g) / 255.0, ((double)b) / 255.0 );

            cloud.Entries = new List<WordCloudEntry>();
            while(!sr.EndOfStream) {
                string line = sr.ReadLine();
                if(line == null) break;
                if(line.Length == 0) break;
                string[] e = line.Split(new char[]{':'});
                if(e.Length < 2) break;
                WordCloudEntry e2 = new WordCloudEntry();
                e2.Word = e[0];
                e2.SizeValue = Convert.ToDouble(e[1]);
                e2.ColorValue = 0.0;
                e2.Color = c;
                e2.Angle = 0.0;
                cloud.Entries.Add(e2);
            }
            sr.Close();
            cloud.InternalRegenerateCloud();
            if(fnout != null) cloud.Save(fnout);
        }