Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

parse-marc.rs 5.2 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  1. #[macro_use]
  2. extern crate structopt;
  3. extern crate quick_xml;
  4. extern crate flate2;
  5. extern crate bookdata;
  6. use std::io::prelude::*;
  7. use std::io::{self, BufReader};
  8. use std::fs::File;
  9. use std::path::PathBuf;
  10. use std::str;
  11. use structopt::StructOpt;
  12. use quick_xml::Reader;
  13. use quick_xml::events::Event;
  14. use flate2::read::GzDecoder;
  15. use bookdata::pgutils::write_encoded;
  16. use bookdata::tsv::split_first;
  17. #[derive(StructOpt, Debug)]
  18. #[structopt(name="parse-marc")]
  19. struct Opt {
  20. #[structopt(name = "FILE", parse(from_os_str))]
  21. infile: Option<PathBuf>
  22. }
  23. struct Field<'a> {
  24. ind1: &'a [u8],
  25. ind2: &'a [u8],
  26. code: &'a [u8]
  27. }
  28. fn process_delim_file<R: BufRead, W: Write>(r: &mut R, w: &mut W) -> io::Result<i32> {
  29. let mut count = 0;
  30. for line in r.lines() {
  31. let lstr = line?;
  32. let (_id, xml) = split_first(&lstr).expect("invalid line");
  33. let mut parse = Reader::from_str(xml);
  34. process_record(&mut parse, w, &mut count);
  35. }
  36. Ok(count)
  37. }
  38. fn write_codes<W: Write>(w: &mut W, rno: i32, fno: i32, tag: &[u8], fld: Option<&Field>) -> io::Result<()> {
  39. let ids = format!("{}\t{}\t", rno, fno);
  40. w.write_all(ids.as_str().as_bytes())?;
  41. w.write_all(tag)?;
  42. w.write_all(b"\t")?;
  43. match fld {
  44. Some(f) => {
  45. w.write_all(f.ind1)?;
  46. w.write_all(b"\t")?;
  47. w.write_all(f.ind2)?;
  48. w.write_all(b"\t")?;
  49. w.write_all(f.code)?;
  50. w.write_all(b"\t")?;
  51. },
  52. None => {
  53. w.write_all(b"\\N\t\\N\t\\N\t")?;
  54. }
  55. }
  56. Ok(())
  57. }
  58. fn write_nl<W: Write>(w: &mut W) -> io::Result<()> {
  59. w.write_all(b"\n")
  60. }
  61. fn process_record<B: BufRead, W: Write>(rdr: &mut Reader<B>, out: &mut W, lno: &mut i32) {
  62. let mut buf = Vec::new();
  63. let mut output = false;
  64. let mut fno = 0;
  65. let mut tag = Vec::with_capacity(5);
  66. let mut ind1 = Vec::with_capacity(10);
  67. let mut ind2 = Vec::with_capacity(10);
  68. loop {
  69. match rdr.read_event(&mut buf) {
  70. Ok(Event::Start(ref e)) => {
  71. let name = str::from_utf8(e.local_name()).unwrap();
  72. match name {
  73. "record" => {
  74. *lno += 1
  75. },
  76. "leader" => {
  77. write_codes(out, *lno, fno, b"LDR", None).expect("output error");
  78. output = true;
  79. },
  80. "controlfield" => {
  81. fno += 1;
  82. let mut done = false;
  83. for ar in e.attributes() {
  84. let a = ar.expect("decode error");
  85. if a.key == b"tag" {
  86. let tag = a.unescaped_value().expect("decode error");
  87. write_codes(out, *lno, fno, &tag, None).expect("output error");
  88. done = true;
  89. }
  90. }
  91. assert!(done, "no tag found for control field");
  92. output = true;
  93. },
  94. "datafield" => {
  95. fno += 1;
  96. for ar in e.attributes() {
  97. let a = ar.expect("decode error");
  98. let v = a.unescaped_value().expect("decode error");
  99. match a.key {
  100. b"tag" => tag.extend_from_slice(&*v),
  101. b"ind1" => ind1.extend_from_slice(&*v),
  102. b"ind2" => ind2.extend_from_slice(&*v),
  103. _ => ()
  104. }
  105. }
  106. assert!(tag.len() > 0, "no tag found for data field");
  107. assert!(ind1.len() > 0, "no ind1 found for data field");
  108. assert!(ind2.len() > 0, "no ind2 found for data field");
  109. },
  110. "subfield" => {
  111. fno += 1;
  112. let mut done = false;
  113. for ar in e.attributes() {
  114. let a = ar.expect("decode error");
  115. if a.key == b"code" {
  116. let code = a.unescaped_value().expect("decode error");
  117. let field = Field { ind1: &ind1, ind2: &ind2, code: &code };
  118. write_codes(out, *lno, fno, &tag, Some(&field)).expect("output error");
  119. done = true;
  120. }
  121. }
  122. assert!(done, "no code found for subfield");
  123. output = true;
  124. }
  125. _ => ()
  126. }
  127. },
  128. Ok(Event::End(ref e)) => {
  129. let name = str::from_utf8(e.local_name()).unwrap();
  130. match name {
  131. "leader" | "controlfield" | "subfield" => {
  132. write_nl(out).expect("output error");
  133. output = false;
  134. },
  135. "datafield" => {
  136. tag.clear();
  137. ind1.clear();
  138. ind2.clear();
  139. },
  140. _ => ()
  141. }
  142. },
  143. Ok(Event::Text(e)) => {
  144. if output {
  145. let t = e.unescaped().expect("decode error");
  146. write_encoded(out, &t).expect("output error")
  147. }
  148. },
  149. Ok(Event::Eof) => break,
  150. Err(e) => panic!("Error at position {}: {:?}", rdr.buffer_position(), e),
  151. _ => ()
  152. }
  153. }
  154. }
  155. fn main() -> io::Result<()> {
  156. let opt = Opt::from_args();
  157. let out = io::stdout();
  158. let mut outlock = out.lock();
  159. match opt.infile {
  160. Some(f) => {
  161. eprintln!("reading from compressed file {:?}", f);
  162. let mut fs = File::open(f)?;
  163. let mut gzf = GzDecoder::new(fs);
  164. let mut bfs = BufReader::new(gzf);
  165. process_delim_file(&mut bfs, &mut outlock)?;
  166. Ok(())
  167. },
  168. None => {
  169. let mut input = io::stdin();
  170. let mut lock = input.lock();
  171. process_delim_file(&mut lock, &mut outlock)?;
  172. Ok(())
  173. }
  174. }
  175. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...