Lookup (Section 3.4) matches a language priority list consisting of basic language ranges to sets of language tags to find the one exact language tag that best matches the range.
2165
function lookup(tag, range) {
  var pos

  tag = lower(tag)
  range = lower(range)

  while (true) {
    if (range === asterisk || tag === range) {
      return true
    }

    pos = range.lastIndexOf(dash)

    if (pos === -1) {
      return false
    }

    if (pos > 3 && range.charAt(pos - 2) === dash) {
      pos -= 2
    }

    range = range.substring(0, pos)
  }
}
245247
def matches(charset)
      values.select {|v|
        v == charset || v == '*'
      }.sort {|a, b|
        # "*" gets least precedence, any others should be equal.
        a == '*' ? 1 : (b == '*' ? -1 : 0)
      }
    end
1041919
def find_chunks(tagged, language="en"):
    
    chunked = [x for x in tagged]
    tags = "".join("%s%s" % (tag, SEPARATOR) for token, tag in tagged)
    # Use Germanic or Romance chunking rules according to given language.
    for tag, rule in CHUNKS[int(language in ("ca", "es", "pt", "fr", "it", "pt", "ro"))]:
        for m in rule.finditer(tags):
            # Find the start of chunks inside the tags-string.
            # Number of preceding separators = number of preceding tokens.
            i = m.start()
            j = tags[:i].count(SEPARATOR)
            n = m.group(0).count(SEPARATOR)
            for k in range(j, j+n):
                if len(chunked[k]) == 3:
                    continue
                if len(chunked[k]) < 3:
                    # A conjunction or comma cannot be start of a chunk.
                    if k == j and chunked[k][1] in ("CC", "CJ", ","):
                        j += 1
                    # Mark first token in chunk with B-.
                    elif k == j:
                        chunked[k].append("B-" + tag)
                    # Mark other tokens in chunk with I-.
                    else:
                        chunked[k].append("I-" + tag)
    # Mark chinks (tokens outside of a chunk) with O-.
    for chink in filter(lambda x: len(x) < 3, chunked):
        chink.append("O")
    # Post-processing corrections.
    for i, (word, tag, chunk) in enumerate(chunked):
        if tag.startswith("RB") and chunk == "B-NP":
            # "Perhaps you" => ADVP + NP
            # "Really nice work" => NP
            # "Really, nice work" => ADVP + O + NP
            if i < len(chunked)-1 and not chunked[i+1][1].startswith("JJ"):
                chunked[i+0][2] = "B-ADVP"
                chunked[i+1][2] = "B-NP"
            if i < len(chunked)-1 and chunked[i+1][1] in ("CC", "CJ", ","):
                chunked[i+1][2] = "O"
            if i < len(chunked)-2 and chunked[i+1][2] == "O":
                chunked[i+2][2] = "B-NP"
    return chunked
1102967
private function retrieveGenericMatches($acceptLanguages, &$matches)
    {
        $genericMatches = [];

        foreach ($acceptLanguages as $option) {
            $option  = array_map('trim', explode(';', $option));
            $locale  = $option[0];
            $quality = $this->getQualityFactor($locale, $option);

            // Unweighted values, get high weight by their position in the list
            $quality          = isset($quality) ? $quality : 1000 - count($matches);
            $matches[$locale] = $quality;

            // If for some reason the Accept-Language header only sends language with country we should make
            // the language without country an accepted option, with a value less than it's parent.
            $localeOptions = explode('-', $locale);
            array_pop($localeOptions);

            while ( ! empty($localeOptions)) {
                //The new generic option needs to be slightly less important than it's base
                $quality -= 0.001;
                $opt      = implode('-', $localeOptions);

                if (empty($genericMatches[$opt]) || $genericMatches[$opt] > $quality) {
                    $genericMatches[$opt] = $quality;
                }

                array_pop($localeOptions);
            }
        }

        return $genericMatches;
    }
111793
def get_all_tags_names_as_list(self, admin=False, language="en"):
        

        if admin:
            if self._all_tags_cache_list_admin != {} and language in self._all_tags_cache_list_admin:
                return self._all_tags_cache_list_admin[language] #Cache hit
        else:
            if self._all_tags_cache_list != {} and language in self._all_tags_cache_list:
                return self._all_tags_cache_list[language] #Cache hit
                        
        #Cache miss, computes everything
        s_stud = set()
        s_admin = set()
        (common, _, org) = self.get_all_tags()
        for tag in common + org:
            # Is tag_name_with_translation correct by doing that like that ?
            tag_name_with_translation = self.gettext(language, tag.get_name()) if tag.get_name() else ""
            s_admin.add(tag_name_with_translation) 
            if tag.is_visible_for_student():
                s_stud.add(tag_name_with_translation) 
        self._all_tags_cache_list_admin[language] = natsorted(s_admin, key=lambda y: y.lower())
        self._all_tags_cache_list[language] = natsorted(s_stud, key=lambda y: y.lower())
        
        if admin:
            return self._all_tags_cache_list_admin[language]
        return self._all_tags_cache_list[language]
1285268
@Override
    public Collection<MatchInfo> find(CharSequence text, int start, EnumSet<NameType> nameTypes) {
        if (text == null || text.length() == 0 || start < 0 || start >= text.length()) {
            throw new IllegalArgumentException("bad input text or range");
        }

        prepareFind();
        TZDBNameSearchHandler handler = new TZDBNameSearchHandler(nameTypes, getTargetRegion());
        TZDB_NAMES_TRIE.find(text, start, handler);
        return handler.getMatches();
    }
615494
def merge(other_lookup_context)
      new_lookup_hash = lookup_hash.dup
      new_lookup_hash[:language_codes] |= other_lookup_context.language_codes

      other_lookup_context.language_codes.each do |language_code|
        new_lookup_hash[language_code] ||= {}
        new_lookup_hash[language_code].merge!(other_lookup_context.lookup_hash[language_code])
      end

      LookupContext.new(new_lookup_hash)
    end
167571
static function prioritizedLanguageCodes()
    {
        $languages = eZContentLanguage::prioritizedLanguages();
        $localeList = array();

        foreach ( $languages as $language )
        {
            $localeList[] = $language->attribute( 'locale' );
        }

        return $localeList;
    }
60941
@Override
    public int matches(Replaceable text,
                       int[] offset,
                       int limit,
                       boolean incremental) {
        // Note (1): We process text in 16-bit code units, rather than
        // 32-bit code points.  This works because stand-ins are
        // always in the BMP and because we are doing a literal match
        // operation, which can be done 16-bits at a time.
        int i;
        int[] cursor = new int[] { offset[0] };
        if (limit < cursor[0]) {
            // Match in the reverse direction
            for (i=pattern.length()-1; i>=0; --i) {
                char keyChar = pattern.charAt(i); // OK; see note (1) above
                UnicodeMatcher subm = data.lookupMatcher(keyChar);
                if (subm == null) {
                    if (cursor[0] > limit &&
                        keyChar == text.charAt(cursor[0])) { // OK; see note (1) above
                        --cursor[0];
                    } else {
                        return U_MISMATCH;
                    }
                } else {
                    int m =
                        subm.matches(text, cursor, limit, incremental);
                    if (m != U_MATCH) {
                        return m;
                    }
                }
            }
            // Record the match position, but adjust for a normal
            // forward start, limit, and only if a prior match does not
            // exist -- we want the rightmost match.
            if (matchStart < 0) {
                matchStart = cursor[0]+1;
                matchLimit = offset[0]+1;
            }
        } else {
            for (i=0; i<pattern.length(); ++i) {
                if (incremental && cursor[0] == limit) {
                    // We've reached the context limit without a mismatch and
                    // without completing our match.
                    return U_PARTIAL_MATCH;
                }
                char keyChar = pattern.charAt(i); // OK; see note (1) above
                UnicodeMatcher subm = data.lookupMatcher(keyChar);
                if (subm == null) {
                    // Don't need the cursor < limit check if
                    // incremental is true (because it's done above); do need
                    // it otherwise.
                    if (cursor[0] < limit &&
                        keyChar == text.charAt(cursor[0])) { // OK; see note (1) above
                        ++cursor[0];
                    } else {
                        return U_MISMATCH;
                    }
                } else {
                    int m =
                        subm.matches(text, cursor, limit, incremental);
                    if (m != U_MATCH) {
                        return m;
                    }
                }
            }
            // Record the match position
            matchStart = offset[0];
            matchLimit = cursor[0];
        }

        offset[0] = cursor[0];
        return U_MATCH;
    }
1120139
def get_all_languages(self, sort='code'):
    
    langs = list(self.culturecode_set.all().as_languages())
    langs.extend(self.language_set.exclude(iso_639_1__in=[x.iso_639_1 for x in langs]))
    if sort in ['name_en', 'name_native']:
        langs.sort(key=lambda x: getattr(x, sort))
    elif sort == 'code':
        codes = self.languages.split(',')

        def code_sort(x):
            return codes.index(x.sort_code)

        langs.sort(key=code_sort)
    else:
        raise ValueError(
            'Invalid code option %s.  Valid options are: code, name_en, name_native' % sort)
    return langs
87301
def lang_match_json(row, hdr, accepted_languages):
    ''''''
    if not accepted_languages:
        return True
    languages = set([row[c].get('xml:lang') for c in hdr
                     if c in row and row[c]['type'] == 'literal'])
    return (not languages) or (languages & accepted_languages)
91857
protected static function _matchLanguage($a, $b) {
		$a = explode('-', strtolower($a));
		$b = explode('-', strtolower($b));

		for ($i = 0, $n = min(count($a), count($b)); $i < $n; $i++) {
			if ($a[$i] !== $b[$i]) {
				break;
			}
		}
		return $i === 0 ? 0 : (float)$i / count($a);
	}
635368
protected MatchedLocale getBestMatch(Map<String,Locale> supportedLocales, String acceptLanguage) {
		// Parse into language, country, and variant
		String language;
		String country;
		String variant;
		int pos1 = acceptLanguage.indexOf('-');
		if(pos1 == -1) {
			language = acceptLanguage;
			country = "";
			variant = "";
		} else {
			language = acceptLanguage.substring(0, pos1);
			int pos2 = acceptLanguage.indexOf('-', pos1 + 1);
			if(pos2 == -1) {
				country = acceptLanguage.substring(pos1 + 1);
				variant = "";
			} else {
				country = acceptLanguage.substring(pos1 + 1, pos2);
				variant = acceptLanguage.substring(pos2 + 1);
			}
		}
		if(!language.isEmpty()) {
			language = language.toLowerCase(Locale.ROOT);
			if(!country.isEmpty()) {
				country = country.toUpperCase(Locale.ROOT);
				if(!variant.isEmpty()) {
					// Exact match on language, country, and variant
					Locale match = supportedLocales.get(
						toLocaleString(
							new Locale(language, country, variant)
						)
					);
					if(match != null) {
						return new MatchedLocale(
							match,
							true
						);
					}
				}
				// Match on language and country
				Locale match = supportedLocales.get(
					toLocaleString(
						new Locale(language, country)
					)
				);
				if(match != null) {
					return new MatchedLocale(
						match,
						variant.isEmpty()
					);
				}
			}
			// Match language
			Locale match = supportedLocales.get(
				toLocaleString(
					new Locale(language)
				)
			);
			if(match != null) {
				return new MatchedLocale(
					match,
					country.isEmpty() && variant.isEmpty()
				);
			}
		}
		// No match
		return null;
	}
581804
def lang_match_xml(row, accepted_languages):
    ''''''
    if not accepted_languages:
        return True
    column_languages = set()
    for elem in row:
        lang = elem[0].attrib.get(XML_LANG, None)
        if lang:
            column_languages.add(lang)
    return (not column_languages) or (column_languages & accepted_languages)
1090513
def sort_languages(self, order=Qt.AscendingOrder):
        

        self.beginResetModel()
        self.__languages = sorted(self.__languages, key=lambda x: (x.name), reverse=order)
        self.endResetModel()
293879
private void processMapping() {
        for (Entry<String, Set<String>> desiredToMatchingLanguages : matcherData.matchingLanguages().keyValuesSet()) {
            String desired = desiredToMatchingLanguages.getKey();
            Set<String> supported = desiredToMatchingLanguages.getValue();
            for (R3<ULocale, ULocale, Double> localeToMaxAndWeight : localeToMaxLocaleAndWeight) {
                final ULocale key = localeToMaxAndWeight.get0();
                String lang = key.getLanguage();
                if (supported.contains(lang)) {
                    addFiltered(desired, localeToMaxAndWeight);
                }
            }
        }
        // now put in the values directly, since languages always map to themselves
        for (R3<ULocale, ULocale, Double> localeToMaxAndWeight : localeToMaxLocaleAndWeight) {
            final ULocale key = localeToMaxAndWeight.get0();
            String lang = key.getLanguage();
            addFiltered(lang, localeToMaxAndWeight);
        }
    }
691251
def [](word)
      h = {}
      language_codes.each do |language_code|
        h[language_code] = lookup_hash[language_code][word] || DEFAULT_VALUE
      end
      h
    end