From 87e6b72f183896933a79f9f25b25df8aaac2708f Mon Sep 17 00:00:00 2001 From: Caleb Evans Date: Sun, 18 Jan 2015 17:56:57 -0800 Subject: [PATCH] Rewrite reference regex; utilize dict.get() method --- YouVersion Suggest.alfredworkflow | Bin 23824 -> 23876 bytes yv_suggest/search.py | 80 ++++++++++++++---------------- 2 files changed, 37 insertions(+), 43 deletions(-) diff --git a/YouVersion Suggest.alfredworkflow b/YouVersion Suggest.alfredworkflow index 80ed29936fecc6c6ece78b9f479b0f4004af6b0d..de7a88bb4ce49ae5843be31a2c4a71a4865247de 100644 GIT binary patch delta 3500 zcmV;d4O8-vx&g$x0gx>Nf_l7>Fh>LTp}4VnR|5mqnz)nH1IhzTDwj*~u0HOwDHI7wtSOQuNGUp>|NHIYMeyb3ai?ix64+hrdlw*% z&ps_8vZNUcllWv~$KBo_H1?AqjAthsA8xPuhZ|?7*75hRf4hIY{p0PBq*2In^7g~U z&GjYO=nn?xX&TYN;Pvfm^7iKX{Vf3+gTe642HBYBd3rP$tX3=6Lnzlz76`}&Z?hz& zS-!pjVtqhxgFM)PWd!vKJ1`oAetv3wclA_u{a>e|~Y$-`_bu@Bg$n z-0kmQ9q#WQ?)~HS)$W19{O;JxGLJu%R3x1M77t?&S1L-jaaJ;PezsV6v_o;}Q))hG>bp-a#FH7N)3@8@0rIHwV-|imC znufDdm34pXgS1`>blZ%ychF`fp`I4wy!d`luxv01;{lDABwgq8B(`3V-%=WrNeC(+ z8J*G$!j}?|%n))#CoIgVWrd40$#Q(3WXX!r%mM-z9;4*LyBji1GICKR#H9 zdPmlG8O@6%ZfOLk?x%Qo+m_ja$UCt|BKUwpjS^A z47@ex%NU`-Jys`;KN;Z5=ZvbSgq2Tj>g9iP^~%=F;+Y#73>WeOr9{pQBZ_vo#UYQ8 z;Vnk0=m?@U3208UMHmB1NRH$Q{PiEa8P!QbzK?OjJN{A^=FbEYl1X`s z*rF2P17f)7F}9Fr6#_C(m?bfQ%j_Py0IIo^R7dN2*)@5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q=*M1Lrvy)p_0S@b`Ek-7QP~hea a0050FllWXC14;e0lOA0l24PzO0000MW{MgB diff --git a/yv_suggest/search.py b/yv_suggest/search.py index 02b518f..9df486b 100644 --- a/yv_suggest/search.py +++ b/yv_suggest/search.py @@ -38,11 +38,20 @@ def get_versions(): default_version = 'NIV' # Pattern for parsing any bible reference -ref_patt = '^((\d )?[a-z ]+)( (\d+)((\:|\.)(\d+)(-(\d+))?)?( [a-z]+\d*)?)?$' -# Pattern for parsing a chapter.verse reference (irrespective of book) -chapter_dot_verse_patt = '(\d+)\.(\d+)' -# Pattern for matching separator tokens at end of incomplete references -incomplete_ref_token_patt = '[\-\.\:]$' +ref_patt = '^{book}(?: {ch}(?:{sep}{v}{v_end}?)?{version}?)?$'.format( + # Book name (including preceding number, if amu) + book='((?:\d )?[a-z ]+)', + # Chapter number + ch='(\d+)', + # Chapter-verse separator + sep='(\:|\.)', + # Verse number + v='(\d+)', + # End verse for a verse range + v_end='(?:-(\d+))', + # Version (translation) used to view reference + version='(?: ([a-z]+\d*))' +) # Guess a version based on the given partial version def guess_version(partial_version): @@ -89,7 +98,7 @@ def format_query_str(query_str): # Lowercase query for consistency query_str = query_str.lower() # Remove tokens at end of incomplete references - query_str = re.sub(incomplete_ref_token_patt, '', query_str) + query_str = re.sub('[\-\.\:]$', '', query_str) return query_str # Builds the query object from the given query string @@ -103,41 +112,28 @@ def get_query_object(query_str): # Create query object for storing query data query = {} - # If reference is in form chapter.verse - if re.search(chapter_dot_verse_patt, query_str): - # Convert chapter.verse to chapter:verse - query['separator'] = '.' - else: - query['separator'] = ':' - # Parse partial book name if given - if ref_matches.group(1): - query['book'] = ref_matches.group(1) - else: - query['book'] = None + query['book'] = ref_matches.group(1) # Parse chapter if given - if ref_matches.group(4): - query['chapter'] = int(ref_matches.group(4)) - else: - query['chapter'] = None + if ref_matches.group(2): + query['chapter'] = int(ref_matches.group(2)) - # Parse verse if given - if ref_matches.group(7): - query['verse'] = int(ref_matches.group(7)) - else: - query['verse'] = None + # Store separator used to separate chapter from verse number + if ref_matches.group(3): + query['separator'] = ref_matches.group(3) - if ref_matches.group(9): - query['verse_end'] = int(ref_matches.group(9)) - else: - query['verse_end'] = None + # Parse verse if given + if ref_matches.group(4): + query['verse'] = int(ref_matches.group(4)) - # Parse version if given - if ref_matches.group(10): - query['version'] = ref_matches.group(10).lstrip() - else: - query['version'] = None + # Parse verse range if given + if ref_matches.group(5): + query['verse_end'] = int(ref_matches.group(5)) + + # Parse version if given + if ref_matches.group(6): + query['version'] = ref_matches.group(6).lstrip() else: @@ -170,7 +166,7 @@ def get_result_list(query_str): # Filter book list to match query matching_books = get_matching_books(query) - if query['version']: + if query.get('version'): # Guess version if possible matched_version = guess_version(query['version']) else: @@ -181,11 +177,9 @@ def get_result_list(query_str): for book in matching_books: # Result information - result = { - 'uid': None - } + result = {} - if query['chapter']: + if query.get('chapter'): # If chapter exists within the book if query['chapter'] <= book['chapters']: @@ -200,7 +194,7 @@ def get_result_list(query_str): chapter=query['chapter'] ) - if query['verse']: + if query.get('verse'): # Find verse if given result['uid'] += '.{verse}'.format( @@ -211,7 +205,7 @@ def get_result_list(query_str): sep=query['separator'] ) - if query['verse_end']: + if query.get('verse_end'): result['uid'] += '-{verse}'.format( verse=query['verse_end'] @@ -227,7 +221,7 @@ def get_result_list(query_str): result['title'] = book['name'] # Create result data using the given information - if result['uid']: + if result.get('uid'): result['uid'] = '{version}/{uid}'.format( version=matched_version.lower(), uid=result['uid']