def find(self, translation, count=MATCH_LIMIT, partial=False, regex=False): """ Find translations that are equal or similar (different case, prefixes, suffixes, etc.) to the given one. Special search types such as partial words and regular expressions are also available. Return a list of suggestion tuples with these translations along with the strokes that can produce them. """ suggestions = [] # Don't bother looking for suggestions on empty strings or whitespace if translation and not translation.isspace(): # Perform the requested type of lookup. The results should be sorted correctly when received. max_matches = min(count, MATCH_LIMIT) if regex: try: items = self.dictionary.find_regex(translation, max_matches) except re.error as e: return [ Suggestion("Invalid regular expression", [(e.msg, )]) ] elif partial: items = self.dictionary.find_partial(translation, max_matches) else: items = self.dictionary.find_similar(translation)[:max_matches] # Package the lookup results into namedtuples for display. for (t, kl) in items: s = Suggestion(t, sort_steno_strokes(kl)) # If we got an exact match, put it at the top of the list, otherwise append the results in order. if t == translation: suggestions.insert(0, s) else: suggestions.append(s) return suggestions
def format_label(fmt, strokes, translation): if strokes: strokes = ", ".join("/".join(s) for s in sort_steno_strokes(strokes)) if translation: translation = escape_translation(translation) return fmt.format(strokes=strokes, translation=translation)
def _format_label(self, fmt, strokes, translation): if strokes: strokes = ', '.join(self._special_fmt % html_escape('/'.join(s)) for s in sort_steno_strokes(strokes)) if translation: translation = self._special_fmt % html_escape(escape_translation(translation)) return fmt.format(strokes=strokes, translation=translation)
def _format_label(self, fmt, strokes, translation=None, filename=None): if strokes: strokes = ', '.join(self._special_fmt % html_escape('/'.join(s)) for s in sort_steno_strokes(strokes)) if translation: translation = self._special_fmt_bold % html_escape(escape_translation(translation)) if filename: filename = html_escape(filename) return fmt.format(strokes=strokes, translation=translation, filename=filename)
def find(self, translation): suggestions = [] mods = [ u'%s', # Same u'{^%s}', # Prefix u'{^}%s', u'{^%s^}', # Infix u'{^}%s{^}', u'{%s^}', # Suffix u'%s{^}', u'{&%s}', # Fingerspell u'{#%s}', # Command ] possible_translations = set([translation]) # Only strip spaces, so patterns with \n or \t are correctly handled. stripped_translation = translation.strip(' ') if stripped_translation and stripped_translation != translation: possible_translations.add(stripped_translation) lowercase_translation = translation.lower() if lowercase_translation != translation: possible_translations.add(lowercase_translation) similar_words = self.dictionary.casereverse_lookup(translation.lower()) if similar_words: possible_translations |= set(similar_words) for t in possible_translations: for modded_translation in [mod % t for mod in mods]: strokes_list = self.dictionary.reverse_lookup( modded_translation) if not strokes_list: continue strokes_list = sort_steno_strokes(strokes_list) suggestion = Suggestion(modded_translation, strokes_list) suggestions.append(suggestion) return suggestions
def find(self, translation): suggestions = [] mods = [ '%s', # Same '{^%s}', # Prefix '{^}%s', '{^%s^}', # Infix '{^}%s{^}', '{%s^}', # Suffix '%s{^}', '{&%s}', # Fingerspell '{#%s}', # Command ] possible_translations = {translation} # Only strip spaces, so patterns with \n or \t are correctly handled. stripped_translation = translation.strip(' ') if stripped_translation and stripped_translation != translation: possible_translations.add(stripped_translation) lowercase_translation = translation.lower() if lowercase_translation != translation: possible_translations.add(lowercase_translation) similar_words = self.dictionary.casereverse_lookup(translation.lower()) if similar_words: possible_translations |= set(similar_words) for t in possible_translations: for modded_translation in [mod % t for mod in mods]: strokes_list = self.dictionary.reverse_lookup(modded_translation) if not strokes_list: continue strokes_list = sort_steno_strokes(strokes_list) suggestion = Suggestion(modded_translation, strokes_list) suggestions.append(suggestion) return suggestions