<commit_before>﻿// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;

namespace Microsoft.CodeAnalysis.Sarif.Converters
{
    internal class FileInfoFactory
    {
        private readonly Dictionary<string, IList<FileReference>> _fileInfoDictionary;
        private readonly Func<string, string> _mimeTypeDeterminer;

        internal FileInfoFactory(Func<string, string> mimeTypeDeterminer)
        {
            _mimeTypeDeterminer = mimeTypeDeterminer;
            _fileInfoDictionary = new Dictionary<string, IList<FileReference>>();
        }

        internal Dictionary<string, IList<FileReference>> Create(IEnumerable<Result> results)
        {
            foreach (Result result in results)
            {
                if (result.Locations != null)
                {
                    foreach (Location location in result.Locations)
                    {
                        if (location.AnalysisTarget != null)
                        {
                            AddFileReference(location.AnalysisTarget);
                        }

                        if (location.ResultFile != null)
                        {
                            AddFileReference(location.ResultFile);
                        }
                    }
                }

                if (result.Stacks != null)
                {
                    foreach (IList<AnnotatedCodeLocation> stack in result.Stacks)
                    {
                        foreach (AnnotatedCodeLocation stackFrame in stack)
                        {
                            AddFileReference(stackFrame.PhysicalLocation);
                        }
                    }

                }

                if (result.ExecutionFlows != null)
                {
                    foreach (IList<AnnotatedCodeLocation> codeFlow in result.ExecutionFlows)
                    {
                        foreach (AnnotatedCodeLocation codeLocation in codeFlow)
                        {
                            AddFileReference(codeLocation.PhysicalLocation);
                        }
                    }
                }

            }

            return _fileInfoDictionary;
        }

        private void AddFileReference(PhysicalLocation physicalLocation)
        {
            string key = physicalLocation.Uri.OriginalString;

            if (!_fileInfoDictionary.ContainsKey(key))
            {
                _fileInfoDictionary.Add(
                    key,
                    new List<FileReference>
                    {
                        new FileReference
                        {
                            Uri = physicalLocation.Uri,
                            MimeType = _mimeTypeDeterminer(key)
                        }
                    });
            }
        }
    }
}
<commit_msg>PR feedback: "mimeTypeDeterminer" => "...Classifier"<commit_after>﻿// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;

namespace Microsoft.CodeAnalysis.Sarif.Converters
{
    internal class FileInfoFactory
    {
        private readonly Dictionary<string, IList<FileReference>> _fileInfoDictionary;
        private readonly Func<string, string> _mimeTypeClassifier;

        internal FileInfoFactory(Func<string, string> mimeTypeClassifier)
        {
            _mimeTypeClassifier = mimeTypeClassifier;
            _fileInfoDictionary = new Dictionary<string, IList<FileReference>>();
        }

        internal Dictionary<string, IList<FileReference>> Create(IEnumerable<Result> results)
        {
            foreach (Result result in results)
            {
                if (result.Locations != null)
                {
                    foreach (Location location in result.Locations)
                    {
                        if (location.AnalysisTarget != null)
                        {
                            AddFileReference(location.AnalysisTarget);
                        }

                        if (location.ResultFile != null)
                        {
                            AddFileReference(location.ResultFile);
                        }
                    }
                }

                if (result.Stacks != null)
                {
                    foreach (IList<AnnotatedCodeLocation> stack in result.Stacks)
                    {
                        foreach (AnnotatedCodeLocation stackFrame in stack)
                        {
                            AddFileReference(stackFrame.PhysicalLocation);
                        }
                    }

                }

                if (result.ExecutionFlows != null)
                {
                    foreach (IList<AnnotatedCodeLocation> codeFlow in result.ExecutionFlows)
                    {
                        foreach (AnnotatedCodeLocation codeLocation in codeFlow)
                        {
                            AddFileReference(codeLocation.PhysicalLocation);
                        }
                    }
                }

            }

            return _fileInfoDictionary;
        }

        private void AddFileReference(PhysicalLocation physicalLocation)
        {
            string key = physicalLocation.Uri.OriginalString;

            if (!_fileInfoDictionary.ContainsKey(key))
            {
                _fileInfoDictionary.Add(
                    key,
                    new List<FileReference>
                    {
                        new FileReference
                        {
                            Uri = physicalLocation.Uri,
                            MimeType = _mimeTypeClassifier(key)
                        }
                    });
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.06215353603225265, 'fraction_numerical': 0.0, 'mean_word_length': 1.141726618705036, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3091963', 'n_tokens_mistral': 1322, 'n_tokens_neox': 1201, 'n_words': 257}