Skip to content

ShingleBased.GetProfile is not public #21

Description

@kentcb

Hi,

Just trying this library out and the README gives this example:

strings1="My first string";strings2="My other string...";// Let's work with sequences of 2 characters...varcosine=newCosine(2);// For cosine similarity I need the profile of stringsStringProfileprofile1=cosine.GetProfile(s1);StringProfileprofile2=cosine.GetProfile(s2);// Prints 0.516185Console.WriteLine(profile1.CosineSimilarity(profile2));

However, this doesn't compile because ShingleBased.GetProfileis protected and because StringProfile no longer appears to be a thing (or maybe that's only in the Java code...?).

Anyway, I can subclass Cosine to hackily gain access to GetProfile, but then I can't really do anything meaningful with it because there is no StringProfile.CosineSimilarity available. I ended up having to copy/paste Cosine in its entirety and hacking in a GetProfile and CosineSimilarity method to gain access to this functionality:

publicclassCosine:ShingleBased,INormalizedStringSimilarity,INormalizedStringDistance{publicCosine(intk):base(k){}publicCosine(){}publicdoubleSimilarity(strings1,strings2){if(s1==null){thrownewArgumentNullException(nameof(s1));}if(s2==null){thrownewArgumentNullException(nameof(s2));}if(s1.Equals(s2)){return1;}if(s1.Length<k||s2.Length<k){return0;}varprofile1=GetProfile(s1);varprofile2=GetProfile(s2);returnCosineSimilarity(profile1,profile2);}publicdoubleCosineSimilarity(IDictionary<string,int>profile1,IDictionary<string,int>profile2)=>DotProduct(profile1,profile2)/(Norm(profile1)*Norm(profile2));privatestaticdoubleNorm(IDictionary<string,int>profile){doubleagg=0;foreach(varentryinprofile){agg+=1.0*entry.Value*entry.Value;}returnMath.Sqrt(agg);}privatestaticdoubleDotProduct(IDictionary<string,int>profile1,IDictionary<string,int>profile2){// Loop over the smallest mapvarsmall_profile=profile2;varlarge_profile=profile1;if(profile1.Count<profile2.Count){small_profile=profile1;large_profile=profile2;}doubleagg=0;foreach(varentryinsmall_profile){if(!large_profile.TryGetValue(entry.Key,outvari))continue;agg+=1.0*entry.Value*i;}returnagg;}publicdoubleDistance(strings1,strings2)=>1.0-Similarity(s1,s2);publicdoubleSimilarity(IDictionary<string,int>profile1,IDictionary<string,int>profile2)=>DotProduct(profile1,profile2)/(Norm(profile1)*Norm(profile2));publicnewIDictionary<string,int>GetProfile(strings)=>base.GetProfile(s);}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions