def refine_ref_by_text(ref, text): """ Returns a ref (string) which refines 'ref' (string) by comparing 'text' (string), to the hebrew text stored in the Library. """ try: oref = model.Ref(ref).section_ref() except: return ref needle = strip_tags(text).strip().replace("\n", "") hay = model.TextChunk(oref, lang="he").text start, end = None, None for n in range(len(hay)): if not isinstance(hay[n], basestring): # TODO handle this case # happens with spanning ref like "Shabbat 3a-3b" return ref if needle in hay[n]: start, end = n + 1, n + 1 break if not start and string_overlap(hay[n], needle): start = n + 1 elif string_overlap(needle, hay[n]): end = n + 1 break if start and end: if start == end: refined = "%s:%d" % (oref.normal(), start) else: refined = "%s:%d-%d" % (oref.normal(), start, end) ref = refined return ref
def refine_ref_by_text(ref, text): """ Returns a ref (string) which refines 'ref' (string) by comparing 'text' (string), to the hebrew text stored in the Library. """ try: oref = model.Ref(ref).section_ref() except: return ref needle = strip_tags(text).strip().replace("\n", "") hay = model.TextChunk(oref, lang="he").text start, end = None, None for n in range(len(hay)): if not isinstance(hay[n], basestring): # TODO handle this case # happens with spanning ref like "Shabbat 3a-3b" return ref if needle in hay[n]: start, end = n+1, n+1 break if not start and string_overlap(hay[n], needle): start = n+1 elif string_overlap(needle, hay[n]): end = n+1 break if start and end: if start == end: refined = "%s:%d" % (oref.normal(), start) else: refined = "%s:%d-%d" % (oref.normal(), start, end) ref = refined return ref