The fraction of n-grams matching between two sequences

        Args:
            s1: a string
            s2: another string
            n: an int for the n in n-gram

        Returns:
            float: the fraction of n-grams matching
15440
def __similarity(s1, s2, ngrams_fn, n=3):
    
    ngrams1, ngrams2 = set(ngrams_fn(s1, n=n)), set(ngrams_fn(s2, n=n))
    matches = ngrams1.intersection(ngrams2)
    return 2 * len(matches) / (len(ngrams1) + len(ngrams2))
1277304
public static boolean simpleNameEquals(String s1, String s2) {
		if (isNullOrEmpty(s1)) {
			return isNullOrEmpty(s2);
		}
		return s1.equals(s2);
	}
988650
def diffcolor(s1, s2):
    '''  '''
    string = ''
    for line in diff(s1, s2):
        if line[0] == '-':
            string += ' ' + TermColors.red(line[2:])
        elif line[0] == '+':
            string += ' ' + TermColors.green(line[2:])
        else:
            string += ' ' + line
    return string[1:]
415589
def strxor(s1, s2):
    
    return b"".join(map(lambda x, y: chb(orb(x) ^ orb(y)), s1, s2))
475443
function tn93(s1, s2, matchMode){
    if(!matchMode) matchMode = "AVERAGE";
    var L = Math.min(s1.length, s2.length);

    var dist = 0;
    var pairwiseCounts = [
            /* A, C, G, T */
      /* A */ [0, 0, 0, 0],
      /* C */ [0, 0, 0, 0],
      /* G */ [0, 0, 0, 0],
      /* T */ [0, 0, 0, 0]
    ];

    if(matchMode == 'SKIP'){
      for (var p = 0; p < L; p++){
        var c1 = mapChar[s1.charCodeAt(p)];
        var c2 = mapChar[s2.charCodeAt(p)];
        if (c1 < 4 && c2 < 4){
          pairwiseCounts[c1][c2] += 1;
        }
      }
    } else if(matchMode == 'GAPMM'){
      for (var p = 0; p < L; p++){
        var c1 = mapChar[s1.charCodeAt(p)];
        var c2 = mapChar[s2.charCodeAt(p)];

        if (c1 < 4 && c2 < 4){
          pairwiseCounts[c1][c2] += 1;
        } else { // not both resolved
          if (c1 == 17 || c2 == 17){
            if (c1 == 17 && c2 == 17){
              continue;
            } else {
              if (c1 == 17){
                c1 = 15;
              } else {
                c2 = 15;
              }
            }
          }

          if (c1 < 4){ // c1 resolved and c2 is not
            if (resolutionsCount[c2] > 0){
              for(var j = 0; j < 4; j++){
                if (resolutions[c2][j]){
                  pairwiseCounts[c1][j] += resolutionsCount[c2];
                }
              }
            }
          } else {
            if (c2 < 4){ // c2 resolved an c1 is not
              if (resolutionsCount[c1] > 0){
                for(var j = 0; j < 4; j++){
                  if (resolutions[c1][j]){
                    pairwiseCounts[j][c2] += resolutionsCount[c1];
                  }
                }
              }
            } else {
              // ambig and ambig
              var norm = resolutionsCount[c1] * resolutionsCount[c2];
              if (norm > 0.0){
                for (var j = 0; j < 4; j++){
                  if (resolutions[c1][j]){
                    for (var k = 0; k < 4; k++){
                      if (resolutions[c2][k]){
                        pairwiseCounts[j][k] += norm;
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    } else if(matchMode == 'RESOLVE'){
      for (var p = 0; p < L; p++){
        var c1 = mapChar[s1.charCodeAt(p)];
        var c2 = mapChar[s2.charCodeAt(p)];

        if (c1 < 4 && c2 < 4){
          pairwiseCounts[c1][c2] += 1;
        } else { // not both resolved
          if (c1 == 17 || c2 == 17) continue;
          if (c1 < 4){ // c1 resolved and c2 is not
            if (resolutionsCount[c2] > 0){
              if (resolutions[c2][c1]){
                pairwiseCounts[c1][c1] += 1;
                continue;
              }
              for(var j = 0; j < 4; j++){
                if (resolutions[c2][j]){
                  pairwiseCounts[c1][j] += resolutionsCount[c2];
                }
              }
            }
          } else {
            if (c2 < 4){ // c2 resolved an c1 is not
              if (resolutionsCount[c1] > 0){
                if (resolutions[c1][c2]){
                  pairwiseCounts[c2][c2] += 1;
                  continue;
                }
                for(var j = 0; j < 4; j++){
                  if (resolutions[c1][j]){
                    pairwiseCounts[j][c2] += resolutionsCount[c1];
                  }
                }
              }
            } else {
              // ambig and ambig
              var norm = resolutionsCount[c1] * resolutionsCount[c2];
              if (norm > 0.0){
                matched_count = 0;
                positive_match = [false, false, false, false];
                for (var j = 0; j < 4; j++){
                  if (resolutions[c1][j] && resolutions[c2][j]){
                    matched_count++;
                    positive_match[j] = true;
                  }
                }

                if (matched_count > 0){
                  norm2 = 1/matched_count;
                  for (var j = 0; j < 4; j++){
                    if (positive_match[j]){
                      pairwiseCounts[j][j] += norm2;
                    }
                  }
                  continue;
                }

                for (var j = 0; j < 4; j++){
                  if (resolutions[c1][j]){
                    for (var k = 0; k < 4; k++){
                      if (resolutions[c2][k]){
                        pairwiseCounts[j][k] += norm;
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    } else {
      for (var p = 0; p < L; p++){
        var c1 = mapChar[s1.charCodeAt(p)];
        var c2 = mapChar[s2.charCodeAt(p)];

        if (c1 < 4 && c2 < 4){
          pairwiseCounts[c1][c2] += 1;
        } else { // not both resolved
          if (c1 == 17 || c2 == 17) continue;
          if (c1 < 4){ // c1 resolved and c2 is not
            if (resolutionsCount[c2] > 0){
              for(var j = 0; j < 4; j++){
                if (resolutions[c2][j]){
                  pairwiseCounts[c1][j] += resolutionsCount[c2];
                }
              }
            }
          } else {
            if (c2 < 4){ // c2 resolved an c1 is not
              if (resolutionsCount[c1] > 0){
                for(var j = 0; j < 4; j++){
                  if (resolutions[c1][j]){
                    pairwiseCounts[j][c2] += resolutionsCount[c1];
                  }
                }
              }
            } else {
              // ambig and ambig
              var norm = resolutionsCount[c1] * resolutionsCount[c2];
              if (norm > 0.0){
                for (var j = 0; j < 4; j++){
                  if (resolutions[c1][j]){
                    for (var k = 0; k < 4; k++){
                      if (resolutions[c2][k]){
                        pairwiseCounts[j][k] += norm;
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

    var nucFreq = [0, 0, 0, 0];
    for (var c1 = 0; c1 < 4; c1++){
      for (var c2 = 0; c2 < 4; c2++){
        nucFreq[c1] += pairwiseCounts[c1][c2];
        nucFreq[c2] += pairwiseCounts[c1][c2];
      }
    }

    var totalNonGap	= 2/(nucFreq[0] + nucFreq[1] + nucFreq[2] + nucFreq[3]);
    var AG = (pairwiseCounts[0][2] + pairwiseCounts[2][0]) * totalNonGap;
    var CT = (pairwiseCounts[1][3] + pairwiseCounts[3][1]) * totalNonGap;
    var tv = 1-((pairwiseCounts[0][0] + pairwiseCounts[1][1] + pairwiseCounts[2][2] + pairwiseCounts[3][3]) * totalNonGap + AG + CT);

    if (nucFreq[0] == 0 || nucFreq[1] == 0 || nucFreq[2] == 0 || nucFreq[3] == 0){
      AG = 1 - 2 * (AG + CT) - tv;
      CT = 1 - 2 * tv;
      if (AG > 0 && CT > 0){
        dist = -0.5 * Math.log(AG) - 0.25 * Math.log(CT);
      } else {
        dist = 1.0;
      }
    } else {
      var auxd = 1/(nucFreq[0] + nucFreq[1] + nucFreq[2] + nucFreq[3]);
      var nucF = [0, 0, 0, 0];
      for (var aux1 = 0; aux1 < 4; aux1++){
        nucF[aux1] = nucFreq[aux1] * auxd;
      }
      var fR = nucF[0] + nucF[2];
      var fY = nucF[1] + nucF[3];
      var K1 = 2 * nucF[0] * nucF[2] / fR;
      var K2 = 2 * nucF[1] * nucF[3] / fY;
      var K3 = 2 * (fR * fY - nucF[0] * nucF[2] * fY / fR - nucF[1] * nucF[3] * fR / fY);
      AG	= 1 - AG / K1 - 0.5 * tv / fR;
      CT	= 1 - CT / K2 - 0.5 * tv / fY;
      tv  = 1 - 0.5 * tv / fY / fR;
      dist = -K1 * Math.log(AG) - K2 * Math.log(CT) - K3 * Math.log(tv);
    }

    return(dist);
  }
208520
@Pure
	protected int compareSegments(ST s1, ST s2) {
		assert s1 != null && s2 != null;
		return s1.hashCode() - s2.hashCode();
	}
1335209
def token_sort_ratio(s1, s2, force_ascii=True, full_process=True):
    
    return _token_sort(s1, s2, partial=False, force_ascii=force_ascii, full_process=full_process)
574647
def match(self, s):
      
      self.matches = self.re.search(s)
      return self.matches
42389
def cosine_similarity(sent1: str, sent2: str) -> float:
    

    WORD = re.compile(r'\w+')
    def get_cosine(vec1, vec2):
        intersection = set(vec1.keys()) & set(vec2.keys())
        numerator = sum([vec1[x] * vec2[x] for x in intersection])

        sum1 = sum([vec1[x]**2 for x in vec1.keys()])
        sum2 = sum([vec2[x]**2 for x in vec2.keys()])
        denominator = math.sqrt(sum1) * math.sqrt(sum2)

        if not denominator:
            return 0.0
        else:
            return float(numerator) / denominator

    def text_to_vector(text):
        words = WORD.findall(text)
        return Counter(words)

    vector1 = text_to_vector(sent1)
    vector2 = text_to_vector(sent2)
    cosine = get_cosine(vector1, vector2)

    return cosine
400089
def sge(self, other):
        
        self._check_match(other)
        return self.to_sint() >= other.to_sint()
599278
public char match(String str1, String str2) {
        if (null == str1 || null == str2 || 0 == str1.length() || 0 == str2.length()) {
            return IMappingElement.IDK;
        }
        String[] grams1 = generateNGrams(str1, gramlength);
        String[] grams2 = generateNGrams(str2, gramlength);
        int count = 0;
        for (String aGrams1 : grams1)
            for (String aGrams2 : grams2) {
                if (aGrams1.equals(aGrams2)) {
                    count++;
                    break;
                }
            }
        float sim = (float) 2 * count / (grams1.length + grams2.length); // Dice-Coefficient
        if (threshold <= sim) {
            return IMappingElement.EQUIVALENCE;
        } else {
            return IMappingElement.IDK;
        }
    }
710411
def n2s(self, offset, length):
        """"""
        s = ''
        for dummy in range(length):
            if self.endian == 'I':
                s += chr(offset & 0xFF)
            else:
                s = chr(offset & 0xFF) + s
            offset = offset >> 8
        return s
1299496
def levenshtein(str1, s2):
    '''
    
    '''
    N1 = len(str1)
    N2 = len(s2)

    stringRange = [range(N1 + 1)] * (N2 + 1)
    for i in range(N2 + 1):
        stringRange[i] = range(i,i + N1 + 1)
    for i in range(0,N2):
        for j in range(0,N1):
            if str1[j] == s2[i]:
                stringRange[i+1][j+1] = min(stringRange[i+1][j] + 1,
                                            stringRange[i][j+1] + 1,
                                            stringRange[i][j])
            else:
                stringRange[i+1][j+1] = min(stringRange[i+1][j] + 1,
                                            stringRange[i][j+1] + 1,
                                            stringRange[i][j] + 1)
    return stringRange[N2][N1]
54038
def hamming_distance(s1, s2, equality_function=operator.eq):
    
    if not len(s1) == len(s2):
        raise ValueError("String lengths are not equal")

    # Number of non-matching characters:
    return sum(not equality_function(c1, c2) for c1, c2 in zip(s1, s2))
1298188
public static int compare( String s1, String s2)
	{
		if (s1 == null && s2 == null)
		{
			return 0;
		}
		else if (s1 == null && s2 != null)
		{
			return -1;
		}
		else if (s1 != null && s2 == null)
		{
			return 1;
		}
		else
		{
			return s1.compareTo(s2);
		}
	}
118417
def common_substring(s1, s2):
    
    chunks = []
    path1 = splitall(s1)
    path2 = splitall(s2)
    for (dir1, dir2) in zip(path1, path2):
        if dir1 != dir2:
            break
        chunks.append(dir1)
    return os.path.join(*chunks)
144769
def _one_contains_other(s1, s2):
    
    return min(len(s1), len(s2)) == len(s1 & s2)