Пример #1
0
        public string AmazonReview(string wordText)
        {
            Dictionary <string, ReviewWord> reviewWordMap = dataTrainer.reviewWords;

            StringBuilder builder = new StringBuilder();

            Console.Write("Start Word: ");

            ReviewWord word = reviewWordMap.ContainsKey(wordText) ? reviewWordMap[wordText] : null;

            if (word == null)
            {
                return("Word not found");
            }

            while (word != null && word.Value != ReviewWord.lastWord)
            {
                builder.Append(word.Value);
                char punctuation = word.getPunctuation();
                if (punctuation != (char)0)
                {
                    builder.Append(word.getPunctuation());
                }
                builder.Append(" ");
                word = word.getNextWord();
            }

            return(builder.ToString());
        }
Пример #2
0
        public void trainReviewData()
        {
            string filePath = Path.Combine(Environment.CurrentDirectory, string.Format("{0}\\{1}", config["DataDirectory"], config["DataFile"]));
            Dictionary <string, ReviewWord> reviewWordMap = new Dictionary <string, ReviewWord>();

            //File doesn't yet exist.  Download and extract it
            if (!File.Exists(filePath))
            {
                downloadWordData(filePath);
            }

            //Read the file line by line and parse the json into ReviewWord objects
            using (FileStream fileStream = File.OpenRead(filePath))
            {
                using (StreamReader reader = new StreamReader(fileStream, Encoding.UTF8, true))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        JObject  o          = JObject.Parse(line);
                        string[] reviewText = o["reviewText"].ToString().ToLower().Split(new char[] { ' ', ';', ':' });

                        for (int i = 0; i < reviewText.Length; i += 2)
                        {
                            string text = reviewText[i];
                            char   punctuation;
                            if (parsePunctuation(text, out punctuation))
                            {
                                text = text.Substring(0, text.Length - 1);
                            }


                            if (text.Trim() == "")
                            {
                                continue;
                            }

                            ReviewWord reviewWord = AddReviewWord(reviewWordMap, text, punctuation);
                            if (reviewWord == null)
                            {
                                continue;
                            }


                            string nextWord = i < reviewText.Length - 1 ? reviewText[i + 1].Trim() : ReviewWord.lastWord;
                            parsePunctuation(nextWord, out punctuation);
                            if (nextWord.Trim() != "")
                            {
                                reviewWord.addNextWord(AddReviewWord(reviewWordMap, nextWord, punctuation));
                            }
                        }
                    }
                }
            }
            this._reviewWords = reviewWordMap;
        }
Пример #3
0
        private static ReviewWord AddReviewWord(Dictionary <string, ReviewWord> reviewWordMap, string text, char punctuation)
        {
            if (text.Length == 1 && ReviewWord.punctuations.Contains(text[0]))
            {
                return(null);
            }


            bool       exists     = reviewWordMap.ContainsKey(text);
            ReviewWord reviewWord = !exists ? new ReviewWord(text) : reviewWordMap[text];

            if (!exists)
            {
                reviewWordMap.Add(text, reviewWord);
                reviewWord.trackUsage();
                reviewWord.trackPunctuation(punctuation);
            }

            return(reviewWord);
        }