MCPcopy Create free account
hub / github.com/ActiveState/code / build_index

Function build_index

recipes/Python/577179_Create_book_index/recipe-577179.py:58–87  ·  view source on GitHub ↗
(text)

Source from the content-addressed store, hash-verified

56 return None
57
58def build_index(text):
59
60 index = {}
61 pattern_re = re.compile(
62 r'(?P<topic>\D.+?) (?:see (?P<see_ref>.*)|(?P<pages>[0-9,\-n]+(?!\.)) ?(?P<subtopic>.*))')
63
64 for line in text:
65 if line == '':
66 continue
67 if opts.debug:
68 print 'line =', line
69 topic, see_ref, pages, subtopic = pattern_re.match(line).groupdict().values()
70 topic, see_ref, pages, subtopic = map(strip_var, (topic, see_ref, pages, subtopic))
71 chunks = topic.split(' ')
72 if len(chunks) > 1:
73 if a_name(chunks[0]):
74 pre, last = topic.split(' ', 1)
75 topic = chunks[-1] + ', ' + ' '.join(chunks[0:-1])
76 if topic not in index:
77 index[topic] = {}
78 if see_ref:
79 if see_ref.startswith('also '):
80 index[topic].setdefault('also', []).append(see_ref[5:])
81 else:
82 index[topic].setdefault('see', []).append(see_ref)
83 elif subtopic:
84 index[topic].setdefault('subtopics', {}).setdefault(subtopic, []).append(pages)
85 else:
86 index[topic].setdefault('pages', []).append(pages)
87 return index
88
89def entitle(s):
90 ''&#x27;title case first word of refs

Callers 1

recipe-577179.pyFile · 0.70

Calls 10

mapFunction · 0.85
a_nameFunction · 0.85
startswithMethod · 0.80
compileMethod · 0.45
valuesMethod · 0.45
matchMethod · 0.45
splitMethod · 0.45
joinMethod · 0.45
appendMethod · 0.45
setdefaultMethod · 0.45

Tested by

no test coverage detected