Exemplo n.º 1
0
    def set_offline_pronunciation(self, pron_url, word_form, word_spelling,
                                  file_name):
        default_url = "[Offline Dictionary Pronunciation]"
        assign_url = coutput.coalesce(pron_url, default_url)

        if self.pronunciation is None:
            self.pronunciation = WordPronunciation(assign_url)
            self.pronunciation.form = word_form
            self.pronunciation.spelling = word_spelling

        self.pronunciation.audio_file = file_name
Exemplo n.º 2
0
def format_txt(caption, element):
    if type(element) is not list:
        elementText = "{0}: {1}\n".format(
            caption, coutput.coalesce(element, DICT_UNICODE_FALLBACK_STR))
    else:
        if len(element) <= 0:
            elementText = "{0}: {1}\n".format(caption,
                                              DICT_UNICODE_FALLBACK_STR)
        else:
            elementText = "{0}:\n".format(caption)
            for line in element:
                elementText = elementText + "\t{0}\n".format(line)

    return elementText
Exemplo n.º 3
0
    def set_simplified_word_entry(self):

        simplifiedWordEntry = None

        matchEntries = []
        matchInflection = None
        matchType = "none"
        matchEntryFound = False

        # Identify matching entry

        # Pass #1: Find matching entry word
        if not matchEntryFound:
            for we in self.word_entries:
                if self.key_word == we.entry_word:
                    matchEntries.append(we)
                    matchEntryFound = True
                    matchType = "entryword"

        # Pass #2: Find matching inflection
        if not matchEntryFound:
            for we in self.word_entries:
                for infl in we.inflections:
                    if self.key_word == infl.spelling:
                        matchEntries.append(we)
                        matchInflection = infl
                        matchEntryFound = True
                        matchType = "inflection"
                        break

                if matchEntryFound:
                    break

        # Pass #3: Default as first entry, if no match found
        if not matchEntryFound:
            for we in self.word_entries:
                matchEntries.append(we)
                matchEntryFound = True
                matchType = "default"
                break

        # Populate conformed entry attributes
        coutput.print_watcher('matchEntryFound')
        coutput.print_watcher('matchType')

        # If matching entry is found, populate pronunciation attributes
        if matchEntryFound:

            if matchType == "inflection":
                simplifiedWordEntry = SimplifiedWordEntry(
                    matchEntries[0].source, self.key_word,
                    matchInflection.spelling)
                simplifiedWordEntry.functional_label = coutput.coalesce(
                    matchInflection.functional_label,
                    matchEntries[0].functional_label)

                if matchInflection.pronunciation is not None:
                    coutput.print_watcher(
                        'matchInflection.pronunciation.audio_url')
                    simplifiedWordEntry.pronunciation = WordPronunciation(
                        matchInflection.pronunciation.audio_url)
                    simplifiedWordEntry.pronunciation.word_pronunciation = matchInflection.pronunciation.word_pronunciation
                    simplifiedWordEntry.pronunciation.form = matchInflection.pronunciation.form
                    simplifiedWordEntry.pronunciation.spelling = matchInflection.pronunciation.spelling

                    coutput.print_watcher(
                        'simplifiedWordEntry.pronunciation.word_pronunciation')
                    coutput.print_watcher(
                        'simplifiedWordEntry.pronunciation.form')
                    coutput.print_watcher(
                        'simplifiedWordEntry.pronunciation.spelling')

                if matchInflection.respelling is not None:
                    simplifiedWordEntry.respelling = WordRespelling(
                        matchInflection.respelling.text,
                        matchInflection.respelling.source)
                    simplifiedWordEntry.respelling.form = matchInflection.respelling.form
                    simplifiedWordEntry.respelling.spelling = matchInflection.respelling.spelling

            else:
                simplifiedWordEntry = SimplifiedWordEntry(
                    matchEntries[0].source, self.key_word,
                    matchEntries[0].entry_word)
                simplifiedWordEntry.functional_label = matchEntries[
                    0].functional_label

                coutput.print_watcher('matchEntries[0].pronunciation')
                simplifiedWordEntry.pronunciation = copy.deepcopy(
                    matchEntries[0].pronunciation)

                simplifiedWordEntry.respelling = copy.deepcopy(
                    matchEntries[0].respelling)

            # Consolidate etymology and senses (definitions and examples)
            etymologies = []
            definitions = []

            for we in matchEntries:

                coutput.print_watcher('we')

                if we.etymology != DICT_UNICODE_EMPTY_STR and we.etymology not in etymologies:
                    etymologies.append(we.etymology)

                flText = DICT_UNICODE_EMPTY_STR
                if we.functional_label != DICT_UNICODE_EMPTY_STR:
                    flText = "({0}) ".format(we.functional_label)

                for sense in we.senses:
                    defnText = flText + str(sense.definition)
                    if defnText not in definitions:
                        definitions.append(defnText)

                # Handle inflections within matching entries
                for infl in we.inflections:

                    flText = DICT_UNICODE_EMPTY_STR
                    if infl.functional_label != DICT_UNICODE_EMPTY_STR:
                        flText = "({0}) ".format(infl.functional_label)

                    for sense in infl.senses:
                        defnText = flText + str(sense.definition)
                        if defnText not in definitions:
                            definitions.append(defnText)

            simplifiedWordEntry.etymology = "; ".join(et for et in etymologies)
            simplifiedWordEntry.definitions = definitions[:]

        # Else if no matching entry is found, create a skeleton entry
        else:
            simplifiedWordEntry = SimplifiedWordEntry(DICT_UNICODE_EMPTY_STR,
                                                      self.key_word,
                                                      DICT_UNICODE_EMPTY_STR)

        # Set conformed entry
        self.simplified_word_entry = simplifiedWordEntry

        coutput.print_watcher('simplifiedWordEntry')
Exemplo n.º 4
0
                    elementText = element.get_text().strip()
                    elementText = currFuncLabel + u', ' + elementText
                    currFuncLabel = elementText.strip()
                    #coutput.print_watcher(MOD_ERR_DEBUG, _FUNC_NAME_, 'currFuncLabel')

                # Process word syllables: <span class="word-syllables">
                for element in entry.find_all('span', class_="word-syllables"):
                    elementText = element.get_text().strip()
                    currWordSyllables = elementText
                    #coutput.print_watcher(MOD_ERR_DEBUG, _FUNC_NAME_, 'currWordSyllables')

                # Process pronunciation: <a class="play-pron">
                for element in entry.find_all('a', class_="play-pron"):
                    currPronunciation = self.build_pronunciation(
                        element,
                        coutput.coalesce(currWordSyllables, currEntryWord))
                    #coutput.print_watcher(MOD_ERR_DEBUG, _FUNC_NAME_, 'currPronunciation')

                # Process respellings: <span class="prs">
                for element in entry.find_all('span', class_="prs"):
                    currRespelling = self.build_respelling(
                        element,
                        coutput.coalesce(currWordSyllables, currEntryWord))
                    #coutput.print_watcher(MOD_ERR_DEBUG, _FUNC_NAME_, 'currRespelling')

            # Process dictionary entry (container): <div id="dictionary-entry-1"> or <div id="medical-entry-1"> or <div id="legal-entry-1">
            elif self.config.is_entry_element(entry):
                coutput.print_watcher(MOD_ERR_DEBUG, _FUNC_NAME_, 'entry')

                wordEntry = cdict.WordEntry(self.config.name, self.key_word)
                wordEntry.entry_word = currEntryWord