public Sentence(IParsedReview review, SentenceItem original) { Review = review ?? throw new ArgumentNullException(nameof(review)); Original = original ?? throw new ArgumentNullException(nameof(original)); Index = review.Sentences.Count; CreateNewPart(); }
public static Document Construct(this LightDocument document, IWordFactory factory) { var result = new Document(document.Text); result.Author = document.Author; result.DocumentTime = document.DocumentTime; result.Id = document.Id; document.Title = document.Title; foreach (var sentence in document.Sentences) { var resultSentence = new SentenceItem(sentence.Text); result.Add(resultSentence); if (sentence.Words != null) { for (var i = 0; i < sentence.Words.Length; i++) { var word = sentence.Words[i]; var wordItem = factory.CreateWord(word.Text, word.Tag); wordItem.WordIndex = i; WordEx wordData = WordExFactory.Construct(wordItem); wordData.Phrase = word.Phrase; resultSentence.Add(wordData); } } } return(result); }
public Document ReparseDocument(IRatingAdjustment adjustment) { if (adjustment?.Review?.Document == null) { throw new System.ArgumentNullException(nameof(adjustment)); } adjustment.CalculateRating(); var document = new Document(); document.DocumentTime = adjustment.Review.Document.DocumentTime; document.Stars = adjustment.Review.Document.Stars; document.Id = adjustment.Review.Document.Id; document.Author = adjustment.Review.Document.Author; if (adjustment.Rating != null) { document.Stars = adjustment.Rating.StarsRating; } if (adjustment.Review.Text == null) { document.Text = adjustment.Review.Text; } foreach (var sentence in adjustment.Review.Sentences) { if (string.IsNullOrWhiteSpace(sentence.Text)) { continue; } var sentenceItem = new SentenceItem(sentence.Text); document.Add(sentenceItem); foreach (var wordItem in sentence.Occurrences) { var word = WordExFactory.Construct(wordItem); word.IsStop = wordItem.IsStopWord; word.Phrase = wordItem.Parent?.Text; word.NormalizedEntity = wordItem.NormalizedEntity; word.Value = wordItem.Relationship.Sentiment?.DataValue?.Value; word.IsAspect = wordItem.IsFeature; SentimentValue value = adjustment.GetSentiment(wordItem); if (value != null) { word.CalculatedValue = value.DataValue.Value; } else if (word.Value != null) { word.CalculatedValue = 0; } sentenceItem.Add(word); } } document.Text = adjustment.Review.Document.Text; return(document); }
public void IsQuestion() { bool result = new SentenceItem("Test question").IsQuestion(); Assert.IsFalse(result); result = new SentenceItem("Test question?").IsQuestion(); Assert.IsTrue(result); }
public void CountCommas() { int result = new SentenceItem("Test question").CountCommas(); Assert.AreEqual(0, result); result = new SentenceItem("Test, question?").CountCommas(); Assert.AreEqual(1, result); }
public void CountSemicolons() { int result = new SentenceItem("Test question").CountSemicolons(); Assert.AreEqual(0, result); result = new SentenceItem("Test, question;?").CountSemicolons(); Assert.AreEqual(1, result); }
public void CountCharacters() { int result = new SentenceItem("Test question").CountCharacters(); Assert.AreEqual(12, result); result = new SentenceItem("Test question?").CountCharacters(); Assert.AreEqual(12, result); }
public void CountPunctuations() { int result = new SentenceItem("Test question").CountPunctuations(); Assert.AreEqual(0, result); result = new SentenceItem("Test question? Test, 2, 3").CountPunctuations(); Assert.AreEqual(3, result); }
private void CreateSentence(SentenceItem sentence) { if (sentence is null) { throw new ArgumentNullException(nameof(sentence)); } review.AddNewSentence(sentence); }
public void AddNewSentence(SentenceItem sentence) { CurrentSentence = new Sentence(this, sentence); if (allSentences.Count > 0) { allSentences[allSentences.Count - 1].Next = CurrentSentence; CurrentSentence.Previous = allSentences[allSentences.Count - 1]; } allSentences.Add(CurrentSentence); }
public static Document Construct(this LightDocument document, IWordFactory factory) { var result = new Document(document.Text); result.Author = document.Author; result.DocumentTime = document.DocumentTime; result.Id = document.Id; document.Title = document.Title; foreach (var sentence in document.Sentences) { var resultSentence = new SentenceItem(sentence.Text); result.Add(resultSentence, false); if (sentence.Words != null) { for (var i = 0; i < sentence.Words.Length; i++) { var word = sentence.Words[i]; var wordItem = factory.CreateWord(word.Text, word.Tag); wordItem.WordIndex = i; WordEx wordData = WordExFactory.Construct(wordItem); wordData.Phrase = word.Phrase; if (!string.IsNullOrEmpty(word.Entity)) { if (entityCache.TryGetValue(word.Entity, out var entity)) { wordData.EntityType = entity; } else { wordData.CustomEntity = word.Entity; } } else { wordData.EntityType = NamedEntities.None; } resultSentence.Add(wordData); } } } return(result); }
private static void ProcessSentence(Document document, string sentence, IEnumerable <WordEx> words) { if (string.IsNullOrWhiteSpace(sentence)) { return; } var currentSentence = new SentenceItem(sentence); foreach (WordEx item in words) { currentSentence.Add(item); } if (currentSentence.Words.Count > 0) { document.Add(currentSentence); } }
public void Setup() { document = new Document("Test"); for (var i = 0; i < 10; i++) { var sentence = new SentenceItem("S1"); sentence.Add("w1"); sentence.Add("w2"); sentence.Add("w3"); document.Add(sentence, false); foreach (var wordEx in sentence.Words) { wordEx.Value = 1; } } info = new DirectoryInfo(Path.Combine(TestContext.CurrentContext.TestDirectory, ".")); foreach (var file in info.GetFiles("*.arff")) { file.Delete(); } }
public static int CountCommas(this SentenceItem sentence) { return(sentence.Text.Count(item => item == ',')); }
public static int CountSemicolons(this SentenceItem sentence) { return(sentence.Text.Count(item => item == ';')); }
public static int CountCharacters(this SentenceItem sentence) { return(sentence.Text.Count(char.IsLetterOrDigit)); }
public static int CountPunctuations(this SentenceItem sentence) { return(sentence.Text.Count(char.IsPunctuation)); }
/// <summary> /// Is current sentence is question /// </summary> public static bool IsQuestion(this SentenceItem sentence) { return(sentence.Text.IndexOf('?') >= 0); }
public Document ReparseDocument(IRatingAdjustment adjustment) { if (adjustment?.Review?.Document == null) { throw new ArgumentNullException(nameof(adjustment)); } adjustment.CalculateRating(); var document = new Document(); document.DocumentTime = adjustment.Review.Document.DocumentTime; document.Stars = adjustment.Review.Document.Stars; document.Id = adjustment.Review.Document.Id; document.Author = adjustment.Review.Document.Author; if (adjustment.Rating != null) { document.Stars = adjustment.Rating.StarsRating; } bool buildText = false; if (adjustment.Review.Text != null) { document.Text = adjustment.Review.Text; buildText = true; } var vector = new SentimentVector(); foreach (var sentence in adjustment.Review.Sentences) { if (string.IsNullOrWhiteSpace(sentence.Text)) { continue; } var sentenceItem = new SentenceItem(sentence.Text); document.Add(sentenceItem, buildText); foreach (var wordItem in sentence.Occurrences) { var word = WordExFactory.Construct(wordItem); word.IsStop = wordItem.IsStopWord; word.Phrase = wordItem.Parent?.Text; word.NormalizedEntity = wordItem.NormalizedEntity; word.Span = word.Text; var sentiment = wordItem.Relationship.Sentiment?.DataValue; if (sentiment != null) { word.Value = sentiment.Value; word.Span = wordItem.Relationship.Sentiment.Span; } word.IsAspect = wordItem.IsFeature; SentimentValue value = adjustment.GetSentiment(wordItem); if (value != null) { word.CalculatedValue = value.DataValue.Value; } else if (word.Value != null) { word.CalculatedValue = 0; } if (adjustment.Review.Context.ExtractAttributes) { PopulateAttributes(vector, word, wordItem); } sentenceItem.Add(word); } } document.Text = adjustment.Review.Document.Text; if (adjustment.Review.Context.ExtractAttributes) { document.Attributes = vector.GetProbabilities().ToDictionary(item => item.Data, item => item.Probability.ToString()); } return(document); }
public void Setup() { item = new SentenceItem("Test"); }