winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

ARPCorrelationAlgorithms.h (2608B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #pragma once
      4 
      5 #include <winget/ARPCorrelation.h>
      6 #include <winget/Manifest.h>
      7 #include <winget/NameNormalization.h>
      8 #include <winget/RepositorySearch.h>
      9 #include <winget/RepositorySource.h>
     10 
     11 namespace AppInstaller::Repository::Correlation
     12 {
     13     struct EmptyMatchConfidenceAlgorithm : public IARPMatchConfidenceAlgorithm
     14     {
     15         void Init(const AppInstaller::Manifest::Manifest&) override {}
     16         double ComputeConfidence(const ARPEntry&) const override { return 0; }
     17     };
     18 
     19     // Algorithm that computes the match confidence by looking at the edit distance between
     20     // the sequences of words in the name and publisher.
     21     // The edit distance for this allows only adding or removing words, not editing them,
     22     // as that would make any two names too similar.
     23     struct WordsEditDistanceMatchConfidenceAlgorithm : public IARPMatchConfidenceAlgorithm
     24     {
     25         using WordSequence = std::vector<std::string>;
     26 
     27         struct NameAndPublisher
     28         {
     29             NameAndPublisher(const WordSequence& name, const WordSequence& publisher);
     30             NameAndPublisher(WordSequence&& name, WordSequence&& publisher);
     31 
     32             WordSequence Name;
     33             WordSequence Publisher;
     34             WordSequence NamePublisher;
     35         };
     36 
     37         void Init(const AppInstaller::Manifest::Manifest& manifest) override;
     38         double ComputeConfidence(const ARPEntry& arpEntry) const override;
     39 
     40     private:
     41         WordSequence PrepareString(std::string_view s) const;
     42         WordSequence NormalizeAndPrepareName(std::string_view name) const;
     43         WordSequence NormalizeAndPreparePublisher(std::string_view publisher) const;
     44 
     45         AppInstaller::Utility::NameNormalizer m_normalizer{ AppInstaller::Utility::NormalizationVersion::InitialPreserveWhiteSpace };
     46         std::vector<NameAndPublisher> m_namesAndPublishers;
     47 
     48         // Parameters for the algorithm
     49 
     50         // How much weight to give to the string matching score.
     51         // The rest is assigned by whether the entry is new.
     52         const double m_stringMatchingWeight = 0.8;
     53 
     54         // How much weight to give to the matching score of the package name;
     55         // the rest is assigned to the publisher score.
     56         const double m_nameMatchingScoreWeight = 2. / 3.;
     57 
     58         // Minimum score needed in the name for us to accept a match.
     59         // This prevents us from being misled by a long match in the publisher.
     60         const double m_nameMatchingScoreMinThreshold = 0.2;
     61     };
     62 }