def _pinyin_search(request, search_string): # CLEAN UP THE INCOMING PINYIN clean_string = _normalize_pinyin(search_string) ascii_string = _pinyin_to_ascii(search_string) key = settings.PINYIN_WORD_KEY % ascii_string suggested = [] words = [] r_server = _get_redis() try: for o in json.loads(r_server.get(key)): word = ChineseWord(chars=o) for i in word.meanings: # IF THE CLEANED SEARCH STRING AND THE CONVERTED PINYIN MATCH if _normalize_pinyin(i['pinyin']) == clean_string: words.append(word) # IF THERE'S NO NUMBERS IN THE CLEANED_STRING, ADD IT elif not any(ext in clean_string for ext in ['1', '2', '3', '4', '5']): words.append(word) else: suggested.append(word) except TypeError: pass return _render(request, 'website/wordlist.html', locals())
def handle_noargs(self, **options): # 一事無成 一事无成 [yi1 shi4 wu2 cheng2] /to have achieved nothing/to be a total failure/to get nowhere/ # EMPTY ALL ZH + PY KEYS self._del_keys('ZH:*') self._del_keys('PY:*') # NOW LETS START file = open(settings.DICT_FILE_LOCATION) item_count = 0 for line in file: if line.startswith("#"): pass else: # OPEN REDIS CONNECTION NOW r_server = _get_redis() # GATHER ALL THE MAIN VARIABLES new = line.split() numbered_pinyin = line[(line.index('[')+1):(line.index(']'))] f = ReadingFactory() tonal_pinyin = f.convert(numbered_pinyin, 'Pinyin', 'Pinyin', sourceOptions={'toneMarkType': 'numbers', 'yVowel': 'v', 'missingToneMark': 'fifth'}) meanings = line[(line.index('/')+1):(line.rindex('/'))] characters = new[1] # REMOVE ALL THE UGLY CHARACTERS if ',' in characters: characters = characters.replace(',', '') # GET AND CLEAN THE MEASURE WORD mws = None if "CL:" in meanings: new_meanings = meanings.split('/') for idx, val in enumerate(new_meanings): if "CL:" in val: mws = [] for x in val.replace('CL:', '').split(','): x = x[:(x.index('['))] if '|' in x: x = x[(x.index('|')+1):] # ADD THE MEAASURE WORDS ENTRY # ---------------------------- mws_key = settings.MEASURE_WORD_KEY % x if r_server.exists(mws_key): values = json.loads(_search_redis(mws_key)) values['chars'].append(characters) else: values = {'chars': [characters,]} r_server.set(mws_key, json.dumps(values)) mws.append(x) new_meanings.pop(idx) meanings = "/".join(new_meanings) char_key = settings.CHINESE_WORD_KEY % ((len((characters))/3), characters) # CREATE THE PRONUNCIATION/MEANING PAIR pair = {} pair['pinyin'] = tonal_pinyin pair['pinyin_numbered'] = _normalize_pinyin(numbered_pinyin) pair['meaning'] = meanings pair['measure_words'] = mws # ADD THE PINYIN ENTRY # -------------------- py_key = settings.PINYIN_WORD_KEY % _pinyin_to_ascii(numbered_pinyin) if r_server.exists(py_key): values = json.loads(_search_redis(py_key)) if smart_unicode(characters) not in values: values.append(characters) else: values = [characters,] r_server.set(py_key, json.dumps(values)) # ADD THE CHINESE CHARACTER ENTRY # ------------------------------- if r_server.exists(char_key): values = json.loads(_search_redis(char_key)) values['meanings'].append(pair) else: values = { 'chars': characters, 'meanings': [pair,], } r_server.set(char_key, json.dumps(values)) item_count += 1 print item_count print "%s Chinese items added" % item_count file.close()
def search(request, search_string=None, title='Search', words=None): r_server = _get_redis() # replace search string underscores with spaces if search_string: search_string = search_string.strip().replace('_', ' ') # HANDLES EMPTY OR NULL SEARCH STRING if search_string == None and request.method != 'POST': form = SearchForm() return _render(request, 'website/search.html', locals()) # CHECK IF IT'S A POST REQUEST OR URL SEARCH if search_string == None and request.method == 'POST': form = SearchForm(request.POST) if form.is_valid(): search_string = form.cleaned_data['char'] else: # POST AND NO SEARCH STRING - SHOW THEM THE PLAIN JANE SEARCH PAGE form = SearchForm() return _render(request, 'website/search.html', locals()) # HANDLES AN AMBIGUOUS SEARCH if _is_ambiguous(search_string): message = messages.AMBIGUOUS_WORD return render(request, 'problem.html', locals()) if r_server.exists((settings.PINYIN_WORD_KEY % _pinyin_to_ascii(search_string))): return _pinyin_search(request, search_string) if _is_english(search_string): return _english_search(request, search_string) # IF THE SEARCH IS OVER 10 CHARACTERS, RETURN A TEXT #if len(search_string) > 12: # from creader.views import text # return text(request, words=search_string) if not words: things = _split_unicode_chrs(search_string) words = _group_words(things) # IF THE USER WAS LOGGED IN, RECORD IT IN THEIR 'SAVED WORDS' if request.user.is_authenticated(): for x in words: word_searched.send( sender=word_searched, word=x.chars, time=datetime.datetime.now(), user_id=request.user.email ) # if there's only 1 word, take us straight to the single word definition if len(words) == 1: word = words[0] url = reverse('single_word', args=[word]) return HttpResponseRedirect(url) return _render(request, 'website/wordlist.html', locals())