/// <summary> /// Checks the contents of the given comment string to determine whether the comment appears /// to be a valid English-language sentence, or whether it appears to be garbage. /// </summary> /// <param name="comment">The comment to check.</param> /// <returns>Returns the type of the comment.</returns> public static InvalidCommentType IsGarbageComment(string comment) { Param.AssertNotNull(comment, "comment"); InvalidCommentType invalid = InvalidCommentType.Valid; string trimmedComment = comment.Trim(); // Make sure the comment string is valid. if (string.IsNullOrEmpty(trimmedComment)) { invalid |= InvalidCommentType.Empty; } else { // Check for the minimum length. if (trimmedComment.Length < MinimumHeaderCommentLength) { invalid |= InvalidCommentType.TooShort; } // Verify that the comment starts with a capital letter. if (!char.IsUpper(trimmedComment[0]) && !char.IsDigit(trimmedComment[0])) { invalid |= InvalidCommentType.NoCapitalLetter; } // Verify that the comment ends with a period. if (trimmedComment[trimmedComment.Length - 1] != '.') { invalid |= InvalidCommentType.NoPeriod; } // Verify that at least 40% of the characters in the comment are letters, and that the comment contains // at least one space between words. float charCount = 0; float nonCharCount = 0; bool space = false; foreach (char character in trimmedComment) { if (char.IsLetter(character)) { ++charCount; } else { if (char.IsWhiteSpace(character)) { space = true; } else { ++nonCharCount; } } } if (charCount == 0 || ((charCount / (charCount + nonCharCount)) * 100) < MinimumCharacterPercentage) { invalid |= InvalidCommentType.TooFewCharacters; } if (!space) { invalid |= InvalidCommentType.NoWhitespace; } } return(invalid); }
/// <summary> /// Checks the contents of the given comment string to determine whether the comment appears /// to be a valid English-language sentence, or whether it appears to be garbage. /// </summary> /// <param name="commentWithAttributesRemoved"> /// The comment to check (which has had its attributes removed). /// </param> /// <param name="commentWithAttributesPreserved"> /// The comment to check with attribute values inserted into the text. /// </param> /// <param name="element"> /// The element containing the text we're checking. /// </param> /// <param name="spellingError"> /// Returns the first word encountered as a spelling error. /// </param> /// <returns> /// Returns the type of the comment. /// </returns> public static InvalidCommentType IsGarbageComment( string commentWithAttributesRemoved, string commentWithAttributesPreserved, CsElement element, out string spellingError) { Param.AssertNotNull(commentWithAttributesRemoved, "commentWithAttributesRemoved"); Param.AssertNotNull(commentWithAttributesPreserved, "commentWithAttributesPreserved"); spellingError = null; InvalidCommentType invalid = InvalidCommentType.Valid; string trimmedCommentWithoutAttributes = commentWithAttributesRemoved.Trim(); string trimmedCommentWithAttributesPreserved = commentWithAttributesPreserved.Trim(); string trimmedCommentWithAttributesPreservedWithoutPeriod = trimmedCommentWithAttributesPreserved.TrimEnd(new[] { '.' }).Trim(); // Make sure the comment string is valid. if (string.IsNullOrEmpty(trimmedCommentWithAttributesPreserved)) { invalid |= InvalidCommentType.Empty; } else { // Check the comment spelling if (TextContainsIncorectSpelling(element, trimmedCommentWithoutAttributes, out spellingError)) { invalid |= InvalidCommentType.IncorrectSpelling; } // Check for the minimum length. if (trimmedCommentWithAttributesPreserved.Length < MinimumHeaderCommentLength) { invalid |= InvalidCommentType.TooShort; } // Verify that the comment starts with a capital letter. if (!char.IsUpper(trimmedCommentWithAttributesPreserved[0]) && !char.IsDigit(trimmedCommentWithAttributesPreserved[0])) { invalid |= InvalidCommentType.NoCapitalLetter; } // Verify that the comment ends with a period. if (trimmedCommentWithAttributesPreserved[trimmedCommentWithAttributesPreserved.Length - 1] != '.') { invalid |= InvalidCommentType.NoPeriod; } // Verify that at least 40% of the characters in the comment are letters, and that the comment contains // at least one space between words. int charCount = 0; int nonCharCount = 0; bool space = false; foreach (char character in trimmedCommentWithAttributesPreservedWithoutPeriod) { if (char.IsLetter(character)) { ++charCount; } else { if (char.IsWhiteSpace(character)) { space = true; } else { ++nonCharCount; } } } if (charCount == 0 || (((charCount * 100) / (charCount + nonCharCount)) < MinimumCharacterPercentage)) { invalid |= InvalidCommentType.TooFewCharacters; } if (!space) { invalid |= InvalidCommentType.NoWhitespace; } } return(invalid); }