public Sentence CreateSentence(BookId bookId, string validatedSentence, int indexInBook)
    {
        validatedSentence = string.Join(" ", _textProcessor.GetWordsEnumerable(validatedSentence));
        var sentenceType = SentenceType.Normal;

        var sentenceId = SentenceId.New();
        var words      = _textProcessor.GetWordsEnumerable(validatedSentence).ToArray();
        var wordsList  = new List <Word>(words.Length);

        var wordsInSentence = words
                              .GroupBy(word => word)
                              .Select(group => new
        {
            Word  = group.Key,
            Count = group.Count()
        }).ToDictionary(x => x.Word);

        foreach (var word in words)
        {
            if (word.All(character => TextConstants.PunctuationCharacters.Contains(character)) &&
                word.Length > 1)
            {
                sentenceType = SentenceType.Other;
            }

            if (word.EndsWith(".?", StringComparison.Ordinal) || word.EndsWith(".!", StringComparison.Ordinal))
            {
                sentenceType = SentenceType.Other;
            }
        }

        var rawWordsInSentence = words
                                 .Select(word => _textProcessor.NormalizeWord(word))
                                 .GroupBy(word => word)
                                 .Select(group => new
        {
            Word  = group.Key,
            Count = group.Count()
        }).ToDictionary(x => x.Word);

        var index = 0;

        foreach (var word in words)
        {
            var keyPairs = CreateKeyPairs(validatedSentence, word);

            var rawWord = _textProcessor.NormalizeWord(word);
            wordsList.Add(new Word(
                              sentenceId, index,
                              word, rawWord,
                              wordsInSentence[word].Count, rawWord == string.Empty ? 0 : rawWordsInSentence[rawWord].Count,
                              keyPairs));
            index++;
        }

        return(new Sentence(bookId, sentenceId, sentenceType, indexInBook, validatedSentence, wordsList));
    }
Exemple #2
0
    public void SentenceId_ShouldBeBetween1And50Characters()
    {
        Assert.Throws <ArgumentNullException>(() => new SentenceId(null !));
        Assert.Throws <ArgumentException>(() => new SentenceId(string.Empty));
        Assert.Throws <ArgumentException>(() => new SentenceId(new string('a', 51)));

        _ = new SentenceId(new string('a', 50));
        _ = new SentenceId("a");
    }
Exemple #3
0
 private static bool Matches(SentenceId sentence) => Id == sentence;
 public ValueTask <SentenceId> NextIdAsync()
 {
     return(new(SentenceId.New()));
 }
Exemple #5
0
 /// <summary>
 /// Creates an unknown sentence from a split of parameters
 /// </summary>
 public RawSentence(TalkerId talkerId, SentenceId id, IEnumerable <string> fields, DateTimeOffset time)
     : base(talkerId, id, time)
 {
     _fields = fields.ToArray();
     Valid   = true;
 }
Exemple #6
0
 public void SentenceId_New_ShouldCreateNewGuid()
 {
     Assert.True(Guid.TryParse(SentenceId.New(), out var id));
     Assert.NotEqual(Guid.Empty, id);
 }
Exemple #7
0
 /// <summary>
 /// Constructs an instance of this abstract class
 /// </summary>
 /// <param name="talker">The talker (sender) of this message</param>
 /// <param name="id">Sentence Id</param>
 /// <param name="time">Date/Time this message was valid (derived from last time message)</param>
 protected NmeaSentence(TalkerId talker, SentenceId id, DateTimeOffset time)
 {
     SentenceId = id;
     TalkerId   = talker;
     DateTime   = time;
 }