Example #1
0
    def print_word_tip(self):
        overrideTipFileName = SB_DICT_OVERRIDE_DIR + SB_DICT_OVERRIDE_MSG.format(WORD=self.activeWord).replace(" ", "_")

        # Check for word message/instruction override
        if os.path.isfile(overrideTipFileName) and os.path.getsize(overrideTipFileName) > 0:
            activeTip = cfile.read(overrideTipFileName)
            coutput.print_tip(activeTip)
Example #2
0
    def save_evaluation_practice_words(self, practiceMode, saveEnabled):
        _FUNC_NAME_ = "save_evaluation_practice_words"

        if saveEnabled:
            if len(self.activePracticeWords) > 0:

                if practiceMode.lower() == "test":
                    practiceFileName = SB_DATA_DIR + SB_PRACTICE_WORD_FILE
                    practiceFileName = practiceFileName.format(LISTID=self.contestList)
                elif practiceMode.lower() == "revise":
                    practiceFileName = SB_DATA_DIR + SB_REVISION_WORD_FILE

                currentPracticeWordList = []

                # Get previously saved practice words
                if os.path.isfile(practiceFileName) and os.path.getsize(practiceFileName) > 0:
                    currentPracticeWordList = cfile.read(practiceFileName).splitlines()                # Use of splitlines() avoids the newline character from being stored in the word list

                # Save practice words to practice file, if not already saved
                for word in self.activePracticeWords:
                    
                    DEBUG_VAR="word"
                    coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, "{0} :: {1}".format(DEBUG_VAR, type(word)))
                    coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, eval(DEBUG_VAR))

                    practiceFileText = SB_EMPTY_STRING
                    if word not in currentPracticeWordList:
                        practiceFileText = practiceFileText + word + SB_NEWLINE

                if practiceFileText != SB_EMPTY_STRING:
                    cfile.append(practiceFileName, practiceFileText)
Example #3
0
    def build_pronunciation_guide(self):

        pronunciation_guide = []
        if self.pronunciation_guide_file != DICT_UNICODE_EMPTY_STR:
            guideFile = pkg_resources.resource_filename(
                __name__, self.pronunciation_guide_file)
            pronunciation_guide = cfile.read(guideFile).splitlines()

        return pronunciation_guide
Example #4
0
SB_USER_AGENT = {
    'user-agent':
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36'
}

########################################################################
# Sample application to test the python module
########################################################################

dictConfig = cdictapi.DictionaryConfig()
dictAssist = cdictassist.DictionaryAssistant(dictConfig)

listFile = 'data/spelling_bee_overrides.txt'
connectionPool = urllib3.PoolManager(10, headers=SB_USER_AGENT)

for activeWord in cfile.read(listFile).splitlines():

    print "+++++++++++++++++++++++++++++++++++++++++++"
    print activeWord
    print "+++++++++++++++++++++++++++++++++++++++++++"

    #activeWord=u'cephalalgia'
    saveFile = activeWord + u".html"
    overrideFile = activeWord + u".dat"

    ##### Online Entry #####
    activeEntry = dictAssist.download_entry(connectionPool, activeWord)
    cfile.write(saveFile, activeEntry)

    ##### Offline Entry #####
    connectionData = cfile.read(saveFile)
Example #5
0
    def lookup_dictionary_by_word(self, word):
        _FUNC_NAME_ = "lookup_dictionary_by_word"

        DEBUG_VAR="self.wordList[0]"
        coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, "{0} :: {1}".format(DEBUG_VAR, type(self.wordList[0])))
        coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, eval(DEBUG_VAR))

        DEBUG_VAR="word"
        coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, "{0} :: {1}".format(DEBUG_VAR, type(word)))
        coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, eval(DEBUG_VAR))

        self.activeWord = word.strip()

        DEBUG_VAR="self.activeWord"
        coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, "{0} :: {1}".format(DEBUG_VAR, type(self.activeWord)))
        coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, eval(DEBUG_VAR))
        
        # Setup connection and error logging
        connectionPool = urllib3.PoolManager(10, headers=SB_USER_AGENT)
        errorFileName = SB_DATA_DIR + SB_ERR_LOG

        # Check offline for dictionary entry
        self.activeEntry = SB_EMPTY_STRING
        self.activeDefinition = []

        overrideDefnFileName = SB_DICT_OVERRIDE_DIR + SB_DICT_OVERRIDE_DEFN.format(WORD=word).replace(" ", "_")
        offlineEntryFileName = SB_DICT_OFFLINE_DIR + SB_DICT_OFFLINE_ENTR.format(WORD=word).replace(" ", "_")

        # Check for dictionary definition override
        if os.path.isfile(overrideDefnFileName) and os.path.getsize(overrideDefnFileName) > 0:
            self.activeEntry = unicode("[Dictionary Definition Override]", 'utf-8')
            self.activeDefinition = cfile.read(overrideDefnFileName).splitlines()

            DEBUG_VAR="self.activeEntry"
            coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, "{0} :: {1}".format(DEBUG_VAR, type(self.activeEntry)))
            coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, eval(DEBUG_VAR))

        # Check primary source for dictionary entry
        elif os.path.isfile(offlineEntryFileName) and os.path.getsize(offlineEntryFileName) > 100:
            coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, "offlineEntryFile size :: {0}".format(os.path.getsize(offlineEntryFileName)))
            self.activeEntry = cfile.read(offlineEntryFileName)
            self.activeDefinition = cdict.parse_word_definition(self.activeWord, self.activeEntry)

            DEBUG_VAR="self.activeEntry"
            coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, "{0} :: {1}".format(DEBUG_VAR, type(self.activeEntry)))
            coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, eval(DEBUG_VAR))

        else:
            # Download dictionary entry
            self.activeEntry = cdict.get_dictionary_entry(connectionPool, self.activeWord)

            DEBUG_VAR="self.activeEntry"
            coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, "{0} :: {1}".format(DEBUG_VAR, type(self.activeEntry)))
            coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, eval(DEBUG_VAR))

            # Save dictionary entry offline
            cfile.write(offlineEntryFileName, self.activeEntry)

            # Retrieve word definition
            self.activeDefinition = cdict.parse_word_definition(self.activeWord, self.activeEntry)
            if len(self.activeDefinition) == 0:
                # Log missing definition error
                errorText = unicode("ERROR:Missing Definition:{0}\n", 'utf-8')
                errorText = errorText.format(self.activeWord)
                cfile.append(errorFileName, errorText)

        # Check offline for word pronunciation
        self.activePronunciation = SB_EMPTY_STRING
        self.activePronunciationWord = SB_EMPTY_STRING

        overrideProncnFileName = SB_DICT_OVERRIDE_DIR + SB_DICT_OVERRIDE_CLIP.format(WORD=self.activeWord).replace(" ", "_")
        offlineProncnFileName = SB_DICT_OFFLINE_DIR + SB_DICT_OFFLINE_CLIP.format(WORD=self.activeWord).replace(" ", "_")

        # Check for dictionary pronunciation override
        if os.path.isfile(overrideProncnFileName) and os.path.getsize(overrideProncnFileName) > 0:
            self.activePronunciation = overrideProncnFileName
            self.activePronunciationWord = self.activeWord

        # Check primary source for dictionary entry and pronunciation
        elif os.path.isfile(offlineEntryFileName) and os.path.getsize(offlineEntryFileName) > 100 and os.path.isfile(offlineProncnFileName) and os.path.getsize(offlineProncnFileName) > 1000:
            coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, "offlineProncnFile size :: {0}".format(os.path.getsize(offlineProncnFileName)))

            self.activePronunciation = offlineProncnFileName

            # Retrieve pronunciation audio clip word form and filename
            [wordClipForm, wordClipURL] = cdict.parse_word_clip(self.activeWord, self.activeEntry)
            self.activePronunciationWord = wordClipForm

        else:
            # Retrieve pronunciation audio clip word form and filename
            [wordClipForm, wordClipURL] = cdict.parse_word_clip(self.activeWord, self.activeEntry)

            # Save pronunciation offline
            if wordClipURL == SB_EMPTY_STRING:
                # Log missing audio error
                errorText = unicode("ERROR:Missing Audio:{0}\n", 'utf-8')
                errorText = errorText.format(self.activeWord)
                cfile.append(errorFileName, errorText)
            else:
                # Download audio clip
                cfile.download(connectionPool, wordClipURL, offlineProncnFileName)

                self.activePronunciation = offlineProncnFileName
                self.activePronunciationWord = wordClipForm

        # Log audio mismatch error
        wordToken = re.sub('[^a-zA-Z]', SB_EMPTY_STRING, self.activeWord.lower())
        pronunciationToken = re.sub('[^a-zA-Z]', SB_EMPTY_STRING, self.activePronunciationWord.lower())
        if self.activePronunciation != SB_EMPTY_STRING and wordToken != pronunciationToken:
            errorText = unicode("ERROR:Audio Mismatch:{0}\n", 'utf-8')
            errorText = errorText.format(self.activeWord)
            cfile.append(errorFileName, errorText)

        # Close connection
        connectionPool.clear()
Example #6
0
    def __init__(self, listID, mode, selection):
        _FUNC_NAME_ = "__init__"

        self.contestList = listID
        self.wordList = []

        wordFileDir = SB_WORD_MULTI_FILES.format(WORD_FILE_PATTERN=listID)
        for wordFileName in sorted(glob.glob(wordFileDir)):
            coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, "wordFileName :: {0}".format(wordFileName))
            self.wordList = self.wordList + cfile.read(wordFileName).splitlines()                # Use of splitlines() avoids the newline character from being stored in the word list

        rangeSelection = selection.split("-")
        self.activeChapter = "0"

        if mode.lower() == "chapter":
            self.activeChapter = int(rangeSelection[0])
            self.activeRangeStart = (self.activeChapter - 1) * SB_CHAPTER_SIZE
            self.activeRangeEnd = self.activeRangeStart + SB_CHAPTER_SIZE - 1
            self.activeWordIndexList = list(range(self.activeRangeStart, self.activeRangeEnd+1))

        elif mode.lower() == "count":
            self.activeRangeStart = int(rangeSelection[0]) - 1
            if len(rangeSelection) > 1:
                self.activeRangeEnd = int(rangeSelection[1]) - 1
            else:
                self.activeRangeEnd = len(self.wordList) - 1
            self.activeWordIndexList = list(range(self.activeRangeStart, self.activeRangeEnd+1))

        elif mode.lower() == "word":
            self.activeRangeStart = self.get_word_index(rangeSelection[0])
            if self.activeRangeStart < 0:
                print "ERROR: Unable to locate '{0}' in word list".format(rangeSelection[0])
                exit(1)

            if len(rangeSelection) > 1:
                self.activeRangeEnd = self.get_word_index(rangeSelection[1])
                if self.activeRangeEnd < 0:
                    print "ERROR: Unable to locate '{0}' in word list".format(rangeSelection[1])
                    exit(1)
            else:
                self.activeRangeEnd = len(self.wordList) - 1
            self.activeWordIndexList = list(range(self.activeRangeStart, self.activeRangeEnd+1))
        else:
            self.activeRangeStart = -1
            self.activeRangeEnd = -1

            sampleSize = int(rangeSelection[0])
            if sampleSize > self.word_count():
                sampleSize = self.word_count()

            self.activeWordIndexList = random.sample(xrange(0, self.word_count()), sampleSize)

        if mode.lower() != "random":
            if self.activeRangeEnd >= len(self.wordList):
                self.activeRangeEnd = len(self.wordList) - 1

        self.activeWord = SB_EMPTY_STRING
        self.activeEntry = SB_EMPTY_STRING
        self.activeDefinition = []
        self.activePronunciation = SB_EMPTY_STRING
        self.activePronunciationWord = SB_EMPTY_STRING
        
        self.activeTestDate = SB_EMPTY_STRING
        self.activeTestScore = SB_EMPTY_STRING
        self.activeTestValuations = []
        self.activePracticeWords = []
SDO_ERR_AUDIO_REGEX_PATTERN = re.compile(".*Audio (Missing|Mismatch).*")
SDO_ERR_DEFN_MISSING = False
SDO_ERR_CLIP_MISSING = False

# Set to True to turn debug messages on
SB_ERR_DEBUG = False

################################################################
# Main Program
################################################################

_FUNC_NAME_ = "main"

connectionPool = urllib3.PoolManager(10, headers=SDO_USER_AGENT)

logEntries = cfile.read(SDO_LIST_FILE).splitlines()

print "Downloading overrides ..."

for entry in logEntries:
    coutput.print_watcher(SB_ERR_DEBUG, _FUNC_NAME_, 'entry')

    logValues = entry.split(':')
    
    word = logValues[1]
    wordEntry = cdict.fetch_dictionary_entry(connectionPool, word)
    coutput.print_watcher(SB_ERR_DEBUG, _FUNC_NAME_, 'wordEntry')

    SDO_ERR_DEFN_MISSING = False
    SDO_ERR_CLIP_MISSING = False
    
Example #8
0
                      'utf-8')

word = "turpentine"

########################################################################
# Sample application to test the python module
########################################################################

#connectionPool = urllib3.PoolManager(10, headers=SB_USER_AGENT)
#connectionURL = SB_CONN_URL.format(WORD=word).replace(" ", "%20").encode('utf-8')
#connectionResponse = connectionPool.request('GET', connectionURL)
#connectionData = connectionResponse.data
#connectionPool.clear()

word = u'turpentine'
connectionData = cfile.read('turpentine.xml')

dictConfig = cdictapi.DictionaryConfig()
wordDictionary = cdictapi.DictionaryEntry(dictConfig, word, connectionData)
print wordDictionary.word_entries
print wordDictionary.simplified_word_entry
"""
for entry in soup.find_all("entry", limit=1):
    for ew in entry.find_all("ew", limit=1):
        print ew.get_text()
    for fl in entry.find_all("fl", limit=1):
        print fl.get_text()
    for hw in entry.find_all("hw", limit=1):
        print hw.get_text()
    for pr in entry.find_all("pr", limit=1):
        print pr.get_text()
Example #9
0
# Set to True to turn debug messages on
#SDO_ERR_DEBUG = True
SDO_ERR_DEBUG = False

################################################################
# Main Program
################################################################

_FUNC_NAME_ = "main"

dictConfig = cdictapi.DictionaryConfig()
dictAssist = cdictassist.DictionaryAssistant(dictConfig)

connectionPool = urllib3.PoolManager(10, headers=SDO_USER_AGENT)

logEntries = cfile.read(SDO_LIST_FILE).splitlines()

print "Downloading overrides ..."

for entry in logEntries:
    coutput.print_watcher(SDO_ERR_DEBUG, _FUNC_NAME_, 'entry')

    logValues = entry.split(':')

    word = logValues[1]

    if not os.path.isfile(SDO_OVERRIDE_ENTRY_FILE.format(WORD=word)):
        cfile.write(SDO_OVERRIDE_ENTRY_FILE.format(WORD=word),
                    dictAssist.download_entry(connectionPool, word))

    wordEntry = cfile.read(SDO_OVERRIDE_ENTRY_FILE.format(WORD=word))
########################################################################

dictConfig = cdictapi.DictionaryConfig()
dictAssist = cdictassist.DictionaryAssistant(dictConfig)

activeWord = u'turpentine'
saveFile = activeWord + u".xml"

##### Online Entry #####
#connectionPool = urllib3.PoolManager(10, headers=SB_USER_AGENT)
#activeEntry = dictAssist.download_entry(connectionPool, activeWord)
#cfile.write(saveFile, activeEntry)
#connectionPool.clear()

##### Offline Entry #####
connectionData = cfile.read(saveFile)

dictConfig = cdictapi.DictionaryConfig()
wordDictionary = cdictapi.DictionaryEntry(dictConfig, activeWord,
                                          connectionData)
print wordDictionary.word_entries
print "+++++++++++++++++++++++++++++++++++++++++++"
print wordDictionary.simplified_word_entry

override = wordDictionary.simplified_word_entry.generate_override()
for entry in override:
    print entry
"""
for entry in soup.find_all("entry", limit=1):
    for ew in entry.find_all("ew", limit=1):
        print ew.get_text()
SDO_USER_AGENT = {
    'user-agent':
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36'
}

SDO_CONF_FILE = "conf/spellit_get_words_list.dat"
SDO_OVERRIDE_DEFN_FILE = "data/download/sb_{WORD}.dat"
SDO_OVERRIDE_PRON_FILE = "data/download/sb_{WORD}.mp3"

################################################################
# Main Program
################################################################

connectionPool = urllib3.PoolManager(10, headers=SDO_USER_AGENT)

wordList = cfile.read(SDO_CONF_FILE).splitlines()

print u"Downloading overrides ..."
for word in wordList:
    print u"Word: " + word
    wordEntry = cdict.fetch_dictionary_entry(connectionPool, word)

    if len(wordEntry[1]) > 0:
        cfile.write(SDO_OVERRIDE_DEFN_FILE.format(WORD=word),
                    coutput.multiline_text(wordEntry[1]))
    else:
        coutput.print_color('yellow',
                            "WARNING: Definition override not available")
    if wordEntry[4] != "":
        cfile.download(connectionPool, wordEntry[4],
                       SDO_OVERRIDE_PRON_FILE.format(WORD=word))
import common.rpimod.stdio.fileio as cfile
import common.rpimod.wordproc.dict.dictionary as cdict

SDO_USER_AGENT = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36'}

SDO_CONF_FILE = "conf/spellit_get_words_list.dat"
SDO_OVERRIDE_DEFN_FILE = "data/download/sb_{WORD}.dat"
SDO_OVERRIDE_PRON_FILE = "data/download/sb_{WORD}.mp3"

################################################################
# Main Program
################################################################

connectionPool = urllib3.PoolManager(10, headers=SDO_USER_AGENT)

wordList = cfile.read(SDO_CONF_FILE).splitlines()

print u"Downloading overrides ..."
for word in wordList:
    print u"Word: " + word
    wordEntry = cdict.fetch_dictionary_entry(connectionPool, word)
    
    if len(wordEntry[1]) > 0:
        cfile.write(SDO_OVERRIDE_DEFN_FILE.format(WORD=word), coutput.multiline_text(wordEntry[1]))
    else:
        coutput.print_color('yellow', "WARNING: Definition override not available")
    if wordEntry[4] != "":
        cfile.download(connectionPool, wordEntry[4], SDO_OVERRIDE_PRON_FILE.format(WORD=word))
    else:
        coutput.print_color('yellow', "WARNING: Pronunciation override not available")