Пример #1
0
        private GroupedQueryResults GetSimilaritiesUsingNonBatchedStrategy(IEnumerable <HashedFingerprint> queryFingerprints, QueryConfiguration configuration, IModelService modelService)
        {
            var hashedFingerprints = queryFingerprints.ToList();
            var groupedResults     = new GroupedQueryResults(hashedFingerprints);
            int hashesPerTable     = configuration.FingerprintConfiguration.HashingConfig.NumberOfMinHashesPerTable;

            Parallel.ForEach(hashedFingerprints, queryFingerprint =>
            {
                var subFingerprints = modelService.ReadSubFingerprints(queryFingerprint.HashBins, configuration);
                foreach (var subFingerprint in subFingerprints)
                {
                    int hammingSimilarity = similarityUtility.CalculateHammingSimilarity(queryFingerprint.HashBins, subFingerprint.Hashes, hashesPerTable);
                    groupedResults.Add(queryFingerprint, subFingerprint, hammingSimilarity);
                }
            });

            return(groupedResults);
        }
Пример #2
0
        public void CalculateHammingSimilarityCorrect()
        {
            byte[] first  = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            byte[] second = new byte[] { 1, 2, 3, 8, 5, 9, 7, 8, 11, 13 };

            var result = similarityUtility.CalculateHammingSimilarity(first, second);

            Assert.AreEqual(6, result);
        }
Пример #3
0
        public void ShouldCorrectlyCalculateHammingDistanceBetweenLongs()
        {
            var length = 50000;

            for (int run = 0; run < 1000; run++)
            {
                var x = GenerateByteArray(length);
                var a = hashConverter.ToInts(x, length / 4);

                var y = GenerateByteArray(length);
                var b = hashConverter.ToInts(y, length / 4);

                var byteSimilarity = similarityUtility.CalculateHammingSimilarity(x, y);
                var longSimilarity = similarityUtility.CalculateHammingSimilarity(a, b, 4);

                Assert.AreEqual(byteSimilarity, longSimilarity);
            }
        }
        public QueryResult Query(IModelService modelService, IEnumerable <HashedFingerprint> hashedFingerprints, QueryConfiguration queryConfiguration)
        {
            var    hammingSimilarities = new Dictionary <IModelReference, int>();
            double snipetLength        = 0;

            foreach (var hashedFingerprint in hashedFingerprints)
            {
                var subFingerprints = GetSubFingerprints(modelService, hashedFingerprint, queryConfiguration);
                foreach (var subFingerprint in subFingerprints)
                {
                    int hammingSimilarity = similarityCalculationUtility.CalculateHammingSimilarity(hashedFingerprint.SubFingerprint, subFingerprint.Signature);
                    if (!hammingSimilarities.ContainsKey(subFingerprint.TrackReference))
                    {
                        hammingSimilarities.Add(subFingerprint.TrackReference, 0);
                    }

                    hammingSimilarities[subFingerprint.TrackReference] += hammingSimilarity;
                }

                snipetLength = System.Math.Max(snipetLength, hashedFingerprint.Timestamp);
            }

            if (!hammingSimilarities.Any())
            {
                return(new QueryResult
                {
                    ResultEntries = Enumerable.Empty <ResultEntry>().ToList(),
                    AnalyzedTracksCount = 0,
                    Info = new QueryInfo {
                        SnippetLength = snipetLength
                    }
                });
            }

            var resultEntries = hammingSimilarities.OrderByDescending(e => e.Value)
                                .Take(queryConfiguration.MaximumNumberOfTracksToReturnAsResult)
                                .Select(e => new ResultEntry
            {
                Track = modelService.ReadTrackByReference(e.Key),
                MatchedFingerprints = e.Value
            })
                                .ToList();

            return(new QueryResult
            {
                ResultEntries = resultEntries,
                AnalyzedTracksCount = hammingSimilarities.Count,
                Info = new QueryInfo {
                    SnippetLength = snipetLength
                }
            });
        }
Пример #5
0
        public QueryResult Query(IModelService modelService, IEnumerable <HashedFingerprint> hashedFingerprints, QueryConfiguration queryConfiguration)
        {
            var hammingSimilarities = new Dictionary <IModelReference, int>();

            foreach (var hashedFingerprint in hashedFingerprints)
            {
                var subFingerprints = GetSubFingerprints(modelService, hashedFingerprint, queryConfiguration);
                foreach (var subFingerprint in subFingerprints)
                {
                    int hammingSimilarity = similarityCalculationUtility.CalculateHammingSimilarity(hashedFingerprint.SubFingerprint, subFingerprint.Signature);
                    if (!hammingSimilarities.ContainsKey(subFingerprint.TrackReference))
                    {
                        hammingSimilarities.Add(subFingerprint.TrackReference, 0);
                    }

                    hammingSimilarities[subFingerprint.TrackReference] += hammingSimilarity;
                }
            }

            if (!hammingSimilarities.Any())
            {
                return(NoResult);
            }

            var resultEntries = hammingSimilarities.OrderByDescending(e => e.Value)
                                .Take(queryConfiguration.MaximumNumberOfTracksToReturnAsResult)
                                .Select(e => new ResultEntry
            {
                Track      = modelService.ReadTrackByReference(e.Key),
                Similarity = e.Value
            })
                                .ToList();

            return(new QueryResult
            {
                ResultEntries = resultEntries,
                IsSuccessful = true,
                AnalyzedCandidatesCount = hammingSimilarities.Count
            });
        }
Пример #6
0
        public double GetScore(HashedFingerprint queryPoint, SubFingerprintData databasePoint, QueryConfiguration queryConfiguration)
        {
            int hashesPerTable = queryConfiguration.FingerprintConfiguration.HashingConfig.NumberOfMinHashesPerTable;

            return(similarityUtility.CalculateHammingSimilarity(queryPoint.HashBins, databasePoint.Hashes, hashesPerTable));
        }