/// <summary> /// Computes an English phonetic distance metric. /// </summary> /// <param name="first">First pronunciation to compare</param> /// <param name="second">Second pronunciation to compare</param> /// <returns>The english phonetic distance between a and b</returns> public double Distance(DistanceInput first, DistanceInput second) { if (first == null || second == null) { throw new ArgumentNullException("distance input can't be null"); } if (first.Phrase == null || first.Pronunciation == null) { throw new ArgumentException("First distance input is invalid. Phrase or Punctuation is null"); } if (second.Phrase == null || second.Pronunciation == null) { throw new ArgumentException("Second distance input is invalid. Phrase or Punctuation is null"); } double distance = 0; NativeResourceWrapper.CallNative((buffer) => { int bufferSize = NativeResourceWrapper.BufferSize; var result = EnHybridDistance_Distance(this.Native, first.Phrase, first.Pronunciation.Native, second.Phrase, second.Pronunciation.Native, out distance, buffer, ref bufferSize); NativeResourceWrapper.BufferSize = bufferSize; return(result); }); return(distance); }
/// <summary> /// Instantiate the native resource wrapped /// </summary> /// <param name="args">The parameter is not used.</param> /// <returns>A pointer to the native resource.</returns> protected override IntPtr CreateNativeResources(params object[] args) { if (args.Length != 5) { throw new ArgumentException("Fuzzy matcher needs parameters to instantiate native resource."); } this.isAccelerated = (bool)args[0]; var targets = args[1] as IList <Target>; var distance = args[2] as DistanceFunc; var extractionToPronounceable = args[3] as Func <Extraction, Pronounceable>; var targetToExtraction = args[4] as Func <Target, Extraction>; // Managed object needs to be initialized *before* creating the native fuzzy matcher. this.InitializeManaged(targets, distance, targetToExtraction, extractionToPronounceable); int targetsCount = targets.Count; IntPtr native = IntPtr.Zero; NativeResourceWrapper.CallNative((buffer) => { int bufferSize = NativeResourceWrapper.BufferSize; var result = FuzzyMatcherBase.FuzzyMatcher_Create(targetsCount, this.nativeDistanceDelegate, this.isAccelerated, out native, buffer, ref bufferSize); NativeResourceWrapper.BufferSize = bufferSize; return(result); }); return(native); }
/// <summary> /// Instantiate the native resource wrapped /// </summary> /// <param name="args">The parameter is not used.</param> /// <returns>A pointer to the native resource.</returns> protected override IntPtr CreateNativeResources(params object[] args) { IntPtr native = IntPtr.Zero; NativeResourceWrapper.CallNative((buffer) => { int bufferSize = NativeResourceWrapper.BufferSize; var result = EnPhoneticDistance_Create(out native, buffer, ref bufferSize); NativeResourceWrapper.BufferSize = bufferSize; return(result); }); return(native); }
/// <summary> /// Find the __k__ nearest elements. /// </summary> /// <param name="query">The search target.</param> /// <param name="limit">The maximum distance to a match.</param> /// <param name="count">The maximum number of result to return.</param> /// <returns>The __k__ nearest matches to target within limit</returns> public IList <Match <Target> > FindNearestWithin(Extraction query, double limit, int count) { if (query == null) { throw new ArgumentNullException("query can't be null"); } this.SetCurrentQuery(query); // the number of targets is the maximum count count = Math.Max(Math.Min(count, this.Count), 1); int[] nearestIdxs = new int[count]; for (int idx = 0; idx < count; ++idx) { nearestIdxs[idx] = -1; } double[] distances = new double[count]; NativeResourceWrapper.CallNative((buffer) => { int bufferSize = NativeResourceWrapper.BufferSize; var result = this.NativeFindNearestWithin(this.Native, count, limit, nearestIdxs, distances, buffer, ref bufferSize); NativeResourceWrapper.BufferSize = bufferSize; return(result); }); IList <Match <Target> > matches = new List <Match <Target> >(); for (int idx = 0; idx < count; ++idx) { if (nearestIdxs[idx] == -1) { // no more matches break; } var match = new Match <Target>( this.targets[nearestIdxs[idx]], distances[idx]); matches.Add(match); } this.currentQuery = default(Pronounceable); return(matches); }
/// <summary> /// Computes an English phonetic distance metric. /// </summary> /// <param name="first">First pronunciation to compare</param> /// <param name="second">Second pronunciation to compare</param> /// <returns>The english phonetic distance between a and b</returns> public double Distance(EnPronunciation first, EnPronunciation second) { if (first == null || second == null) { throw new ArgumentNullException("distance input can't be null"); } double distance = 0; NativeResourceWrapper.CallNative((buffer) => { int bufferSize = NativeResourceWrapper.BufferSize; var result = EnPhoneticDistance_Distance(this.Native, first.Native, second.Native, out distance, buffer, ref bufferSize); NativeResourceWrapper.BufferSize = bufferSize; return(result); }); return(distance); }
/// <summary> /// Instantiate the native resource wrapped. /// </summary> /// <param name="args">phonetic Weight Percentage</param> /// <returns>A pointer to the native resource.</returns> protected override IntPtr CreateNativeResources(params object[] args) { double phoneticWeightPercentage = (double)args[0]; if (phoneticWeightPercentage > 1 || phoneticWeightPercentage < 0) { throw new ArgumentOutOfRangeException("phoneticWeightPercentage must be between 0 and 1."); } IntPtr native = IntPtr.Zero; NativeResourceWrapper.CallNative((buffer) => { int bufferSize = NativeResourceWrapper.BufferSize; var result = EnHybridDistance_Create(phoneticWeightPercentage, out native, buffer, ref bufferSize); NativeResourceWrapper.BufferSize = bufferSize; return(result); }); return(native); }