Ejemplo n.º 1
0
class Autocomplete(Index):
    def __init__(self):
        super(Autocomplete, self).__init__()
        self.dir_name = "data/autocomplete_index/"
        self.writers = {}
        self.letter = FirstLetter()

    def create(self):
        return

    def _create(self, letter):
        schema = Schema(verb_form=TEXT(stored=True, analyzer=self.analyzer),
                        infinitive=STORED,
                        autocomplete_sorting=TEXT(sortable=True))

        dir_name = f'{self.dir_name}{letter}/'
        self._create_dir(dir_name)
        ix = create_in(dir_name, schema)
        return ix.writer()

    def write_entry(self, verb_form, file_path, is_infinitive, infinitive,
                    mode, tense):

        if self._verbs_to_ignore_in_autocomplete(mode, tense):
            return

        letter = self.letter.from_word(verb_form)
        if letter not in self.letter.get_letters():
            raise IndexError(
                f'Letter {letter} is not supported by the client. Review get_letters()'
            )

        if letter in self.writers:
            writer = self.writers[letter]
        else:
            writer = self._create(letter)
            self.writers[letter] = writer

        autocomplete_sorting = self._get_autocomple_sorting_key(
            verb_form, is_infinitive, infinitive)

        writer.add_document(verb_form=verb_form,
                            infinitive=infinitive,
                            autocomplete_sorting=autocomplete_sorting)

    def _get_autocomple_sorting_key(self, verb_form, is_infinitive,
                                    infinitive):
        SORTING_PREFIX = '_'
        if is_infinitive:
            # By starting with '_', it forces infinitives to appear first in search
            return f'{SORTING_PREFIX}{infinitive}'
        else:
            return f'{verb_form}{SORTING_PREFIX}{infinitive}'

    def save(self):
        for writer in self.writers.values():
            writer.commit()
Ejemplo n.º 2
0
class IndexLetter(Index):
    def __init__(self):
        super(IndexLetter, self).__init__()
        self.dir_name = "data/indexletter_index/"
        self.writer = None
        self.entries = 0
        self.letter = FirstLetter()

    def create(self):
        schema = Schema(verb_form=TEXT(stored=True,
                                       sortable=True,
                                       analyzer=self.analyzer),
                        index_letter=TEXT(sortable=True,
                                          analyzer=self.analyzer),
                        infinitive=TEXT(stored=True, analyzer=self.analyzer))

        self._create_dir(self.dir_name)
        ix = create_in(self.dir_name, schema)
        self.writer = ix.writer()

    def write_entry(self, verb_form, is_infinitive, infinitive):
        if is_infinitive:
            index_letter = self.letter.from_word(verb_form)
        else:
            index_letter = None

        if index_letter is not None:
            self.entries = self.entries + 1
            self.writer.add_document(verb_form=verb_form,
                                     index_letter=index_letter,
                                     infinitive=infinitive)

    def save(self):
        self.writer.commit()
Ejemplo n.º 3
0
def open_indexes():
    dir_name = "../data/autocomplete_index/"

    idxs = {}
    for letter in FirstLetter().get_letters():
        try:
            dir_name_letter = f'{dir_name}{letter}'
            ix = open_dir(dir_name_letter)
            idxs[letter] = ix
        except:
            print(f'No index found for {letter}')

    return idxs
Ejemplo n.º 4
0
class Autocomplete(SearchBase):
    def __init__(self, word):
        self._word = word
        self.searcher = None
        self.query = None
        self.num_results = 0
        self.letter = FirstLetter()

    def get_num_results(self):
        return self.num_results

    def get_results(self):
        letter = self.letter.from_word(self.word)

        if letter not in idxs:
            results = []
        else:
            self._search(letter)
            results = self.searcher.search(self.query,
                                           limit=10,
                                           sortedby='autocomplete_sorting')

        self.num_results = len(results)
        return results

    def _search(self, letter):
        ix = idxs[letter]

        self.searcher = ix.searcher()
        fields = []
        qs = u' verb_form:({0}*)'.format(self._word)

        self.query = MultifieldParser(fields, ix.schema).parse(qs)

    def get_json(self):
        OK = 200
        status = OK
        results = self.get_results()
        all_results = []
        for result in results:
            verb = {}
            verb['verb_form'] = result['verb_form']
            verb['infinitive'] = result['infinitive']
            all_results.append(verb)

        return json.dumps(all_results, indent=4,
                          separators=(',', ': ')), status
Ejemplo n.º 5
0
class Search(Index):
    def __init__(self):
        super(Search, self).__init__()
        self.dir_name_search = "data/search_index/"
        self.writer = None
        self.letter = FirstLetter()

    def create(self):
        analyzer_no_diatritics = self.analyzer | CharsetFilter(accent_map)
        schema = Schema(
            verb_form=TEXT(stored=True, sortable=True, analyzer=self.analyzer),
            verb_form_no_diacritics=TEXT(analyzer=analyzer_no_diatritics),
            index_letter=TEXT(sortable=True, analyzer=self.analyzer),
            file_path=TEXT(stored=True, sortable=True))

        self._create_dir(self.dir_name_search)
        ix = create_in(self.dir_name_search, schema)
        self.writer = ix.writer()

    def write_entry(self, verb_form, file_path, is_infinitive, infinitive,
                    mode, tense):

        if self._verbs_to_ignore_in_autocomplete(mode, tense):
            return

        if is_infinitive:
            index_letter = self.letter.from_word(verb_form)
        else:
            index_letter = None

        self.writer.add_document(verb_form=verb_form,
                                 verb_form_no_diacritics=verb_form,
                                 file_path=file_path,
                                 index_letter=index_letter)

    def save(self):
        self.writer.commit()
Ejemplo n.º 6
0
 def __init__(self):
     super(Search, self).__init__()
     self.dir_name_search = "data/search_index/"
     self.writer = None
     self.letter = FirstLetter()
Ejemplo n.º 7
0
 def __init__(self, word):
     self._word = word
     self.searcher = None
     self.query = None
     self.num_results = 0
     self.letter = FirstLetter()
Ejemplo n.º 8
0
 def __init__(self):
     super(IndexLetter, self).__init__()
     self.dir_name = "data/indexletter_index/"
     self.writer = None
     self.entries = 0
     self.letter = FirstLetter()
Ejemplo n.º 9
0
 def __init__(self):
     super(Autocomplete, self).__init__()
     self.dir_name = "data/autocomplete_index/"
     self.writers = {}
     self.letter = FirstLetter()