Ejemplo n.º 1
0
        public void Initialize(LocationStore locationStore, ClassStore classStore)
        {
            char[] delimiters = new char[] { ' ', '\t', '\r', '\n' };
            if (Type == CommentType.SingleLine)
            {
                string normalizedContent = WordTransform.RemoveSpecialCharacters(Content);
                WordsCount = normalizedContent.Split(delimiters, StringSplitOptions.RemoveEmptyEntries).Length;
            }

            if (locationStore.LocationsRelativeToMethod.ContainsKey(LineEnd))
            {
                LocationRelativeToMethod = locationStore.LocationsRelativeToMethod[LineEnd].Location;
                MethodName = locationStore.LocationsRelativeToMethod[LineEnd].Method;
            }
            if (locationStore.LocationsRelativeToClass.ContainsKey(LineEnd))
            {
                LocationRelativeToClass = locationStore.LocationsRelativeToClass[LineEnd].Location;
                Class @class = classStore.Classes.Single(c => c.Name == locationStore.LocationsRelativeToClass[LineEnd].Class.Name &&
                                                         c.Namespace == locationStore.LocationsRelativeToClass[LineEnd].Class.Namespace);
                @class.Comments.Add(this);
                this.Class = @class;
            }

            HasNothing         = new Regex("nothing", RegexOptions.IgnoreCase).IsMatch(Content);
            HasQuestionMark    = new Regex(@"\?($|\W)").IsMatch(Content);
            HasExclamationMark = new Regex("!").IsMatch(Content);
            HasCode            = CodeDetector.HasCode(Content);

            if (MethodName != null && LocationRelativeToMethod == LocationRelativeToMethod.MethodDescription)
            {
                CoherenceCoefficient = CommentsAnalysis.CoherenceCoefficient.Compute(Content, MethodName);
            }
        }
        public static double Compute(string commentContent, string methodName)
        {
            commentContent = WordTransform.RemoveSpecialCharacters(commentContent);
            string[] commentWords = commentContent.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            commentWords = PreprocessWords(commentWords);

            if (commentWords.Length == 0)
            {
                return(0);
            }

            methodName = WordTransform.RemoveSpecialCharacters(methodName);
            string[] methodWords = Regex.Replace(methodName, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 ").Split(" ");
            methodWords = PreprocessWords(methodWords);

            int matchedWords = 0;

            foreach (var commentWord in commentWords)
            {
                if (Array.Exists(methodWords, methodWord => methodWord.ToLower() == commentWord.ToLower()))
                {
                    matchedWords++;
                }
            }
            return((double)matchedWords / commentWords.Length);
        }