Debian Codegraph @

pymzml/utils/index_moby_dick.py

raw symbols SCIP
1 """
2 Example of how to use the :py:class:`~pymzml.utils.GSGW.GSGW` class to create
3 igzip file to load and create a copy of "Moby Dick" with every chapter indexed
4 with its number.
5 The file created can be read by :py:module:`~pymzml.utils.read_moby_dick`
6 """
7
8 # Python mzML module - pymzml
9 # Copyright (C) 2010-2019 M. Kösters, C. Fufezan
10 # The MIT License (MIT)
11
12 # Permission is hereby granted, free of charge, to any person obtaining a copy
13 # of this software and associated documentation files (the "Software"), to deal
14 # in the Software without restriction, including without limitation the rights
15 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 # copies of the Software, and to permit persons to whom the Software is
17 # furnished to do so, subject to the following conditions:
18
19 # The above copyright notice and this permission notice shall be included in all
20 # copies or substantial portions of the Software.
21
22 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 # SOFTWARE.
29
30 import urllib.request
31 import re
32 from GSGW import GSGW
33 import pymzml.regex_patterns as regex_patterns
34
35 moby_dick_url = (
36 "http://faculty.washington.edu/stepp/courses/"
37 "2004autumn/tcss143/lectures/files/2004-11-08/mobydick.txt"
38 )
39
40 response = urllib.request.urlopen(moby_dick_url)
41 moby_dick_txt = response.read().decode("utf-8").split("\n")
42
43
44 def index_by_chapter(txt):
45 """
46 Iterate the text file while collecting the data for each chapter and compressing it
47
48 Args:
49 txt(str): Moby Dick text as string
50 """
51 chapter_start = regex_patterns.MOBY_DICK_CHAPTER_PATTERN
52 general_seekable_gzip_writer = GSGW(
53 file="Moby_Dick_indexed.gz",
54 max_idx=42,
55 max_idx_len=9,
56 max_offset_len=6,
57 output_path="./Moby_Dick_indexed.gz",
58 )
59
60 with general_seekable_gzip_writer as index_writer:
61 current_chapter = ""
62 for line in txt:
63 if re.match(chapter_start, line):
64 match = re.match(chapter_start, line)
65 current_chapter_number = int(match.group(1)) - 1
66 print("indexing chapter {0}".format(current_chapter_number), end="\r")
67 index = "Chapter{0}".format(current_chapter_number)
68 index_writer.add_data(current_chapter, index)
69 current_chapter = ""
70 else:
71 current_chapter += line
72 current_chapter_number += 1
73
74 index = "Chapter{0}".format(current_chapter_number)
75 index_writer.add_data(current_chapter, index)
76 print("index chapter {0} ".format(index), end="\r")
77 index_writer.write_index()
78 print("Indexed {0} chapters".format(current_chapter_number))
79
80
81 if __name__ == "__main__":
82 index_by_chapter(moby_dick_txt)