|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
From Python to C#
I'm total new to Python and I'm rewriting this method into C#
can so one check to see if I did it right, please. Tommy. There's one line in the Python that I could understand what the person was trying to do. here it is w = 1./3.*(common1 / float(len1) + common1 / float(len2) + \ (common1-transposition) / common1) also workstr2 = workstr2[:index]+'*'+workstr2[index+1:] whats the story with the ":" ? ============================================== Python ============================================== def jaro(str1, str2): """Return approximate string comparator measure (between 0.0 and 1.0) USAGE: score = jaro(str1, str2) ARGUMENTS: str1 The first string str2 The second string DESCRIPTION: As desribed in 'An Application of the Fellegi-Sunter Model of Record Linkage to the 1990 U.S. Decennial Census' by William E. Winkler and Yves Thibaudeau. """ # Quick check if the strings are the same - - - - - - - - - - - - - - - - - - # if (str1 == str2): return 1.0 len1 = len(str1) len2 = len(str2) halflen = max(len1, len2) / 2 + 1 ass1 = '' # Characters assigned in str1 ass2 = '' # Characters assigned in str2 workstr1 = str1 # Copy of original string workstr2 = str2 common1 = 0 # Number of common characters common2 = 0 # Analyse the first string - - - - - - - - - - - - - - - - - - - - - - - - - # for i in range(len1): start = max(0,i-halflen) end = min(i+halflen+1,len2) index = workstr2.find(str1[i],start,end) if (index > -1): # Found common character common1 += 1 ass1 = ass1+str1[i] workstr2 = workstr2[:index]+'*'+workstr2[index+1:] # Analyse the second string - - - - - - - - - - - - - - - - - - - - - - - - - # for i in range(len2): start = max(0,i-halflen) end = min(i+halflen+1,len1) index = workstr1.find(str2[i],start,end) if (index > -1): # Found common character common2 += 1 ass2 = ass2 + str2[i] workstr1 = workstr1[:index]+'*'+workstr1[index+1:] if (common1 != common2): print 'error:Jaro: Something is wrong. String 1: "%s", string2: "%s"' % \ (str1, str2) + ', common1: %i, common2: %i' % (common1, common2) + \ ', common should be the same.' common1 = float(common1+common2) / 2.0 ##### This is just a fix ##### if (common1 == 0): return 0.0 # Compute number of transpositions - - - - - - - - - - - - - - - - - - - - - # transposition = 0 for i in range(len(ass1)): if (ass1[i] != ass2[i]): transposition += 1 transposition = transposition / 2.0 common1 = float(common1) w = 1./3.*(common1 / float(len1) + common1 / float(len2) + \ (common1-transposition) / common1) # A log message for high volume log output (level 3) - - - - - - - - - - - - # print '3: Jaro comparator string 1: "%s", string 2: "%s"' % (str1, str2) print '3: Common: %i' % (common1) print '3: Assigned 1: %s, assigned 2: %s' % (ass1, ass2) print '3: Transpositions: %i' % (transposition) print '3: Final approximate string weight: %f' % (w) return w # ================================================== =========================== ============================================== C# ============================================== using System; namespace ClassLibrary1 { public class Class1 { public float JaroM(string string1, string string2) { /* Return approximate string comparator measure (between 0.0 and 1.0) USAGE: score = jaro(str1, str2) ARGUMENTS: str1 The first string str2 The second string DESCRIPTION: As desribed in 'An Application of the Fellegi-Sunter Model of Record Linkage to the 1990 U.S. Decennial Census' by William E. Winkler and Yves Thibaudeau. */ //Quick check if the strings are the same - - - - - - - - - - - - - - - - - - if (string1 == string2) return 1.0F; float w; int len1 = string1.Length; int len2 = string2.Length; int halflen = Max(len1, len2) / 2 + 1; string ass1 = ""; // Characters assigned in str1 string ass2 = ""; // Characters assigned in str2 string workstr1 = string1; // Copy of original string string workstr2 = string2; double common1 = 0; // Number of common characters double common2 = 0; //Analyse the first string - - - - - - - - - - - - - - - - - - - - - - - - - int start; int end; int index; for(int i = 0; i < len1; ++i) { start = Max(0, i - halflen); end = Min(i + halflen + 1, len2); index = workstr2.IndexOf(string1[i], start, end); if (index > -1) // Found common character { common1 += 1; ass1 = ass1 + string1[i]; workstr2 = workstr2[index] + "*" + workstr2[index + 1]; } } //Analyse the second string - - - - - - - - - - - - - - - - - - - - - - - - - for(int i = 0; i < len2; ++i) { start = Max(0, i - halflen); end = Min(i + halflen + 1, len1); index = workstr1.IndexOf(string2[i], start, end); if (index > -1) // Found common character { common2 += 1; ass2 = ass2 + string2[i]; workstr1 = workstr1[index] + "*" + workstr1[index + 1]; } } if (common1 != common2) { //print 'error:Jaro: Something is wrong. String 1: "%s", string2: "%s"' % (str1, str2) + ', common1: %i, common2: %i' % (common1, common2) + ', common should be the same.' common1 = (common1 + common2) / 2.0 ; // This is just a fix ##### } if (common1 == 0) return 0.0F; //Compute number of transpositions - - - - - - - - - - - - - - - - - - - - - double transposition = 0; for(int i = 0; i < ass1.Length; ++i) { if (ass1[i] != ass2[i]) { transposition += 1; } } transposition = transposition / 2.0; common1 = common1; //I'm unsure as to what they are trying to do here w = 1. / 3. * (common1 / len1 + common1 / len2 + (common1 - transposition) / common1); //A log message for high volume log output (level 3) - - - - - - - - - - - - //print '3: Jaro comparator string 1: "%s", string 2: "%s"' % (str1, str2) //print '3: Common: %i' % (common1) //print '3: Assigned 1: %s, assigned 2: %s' % (ass1, ass2) //print '3: Transpositions: %i' % (transposition) //print '3: Final approximate string weight: %f' % (w) return w; } public int Min(int lNumber1, int lNumber2){ if(lNumber1 < lNumber2) return lNumber1; return lNumber2; } public int Max(int lNumber1, int lNumber2){ if(lNumber1 > lNumber2) return lNumber1; return lNumber2; } } } Last edited by thomaskeegan : May 22nd, 2003 at 07:05 AM. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > From Python to C# |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|