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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//! # Abbreviation Extractor
//!
//! Abbreviation Extractor is a high-performance Rust library with Python bindings for extracting
//! abbreviation-definition pairs from text, particularly focused on biomedical literature. It implements
//! an improved version of the Schwartz-Hearst algorithm as described in:
//!
//! A. Schwartz and M. Hearst (2003) A Simple Algorithm for Identifying Abbreviations Definitions
//! in Biomedical Text. Biocomputing, 451-462.
//!
//! ## Overview
//!
//! This library provides functionality to extract abbreviation-definition pairs from text. It supports
//! both single-threaded and parallel processing, making it suitable for various text processing tasks.
//! The library is designed with a focus on biomedical literature but can be applied to other domains as well.
//!
//! Key components of the library include:
//! - Support for parallel processing of large datasets
//! - Customizable extraction parameters like selecting the most common or first definition for each abbreviation
//! - Python bindings for easy integration with Python projects
//! - Tokenization of input text for more accurate extraction
//!
//!
//! ## Basic Usage
//!
//! ### Rust
//!
//! ```rust
//! use abbreviation_extractor::{extract_abbreviation_definition_pairs, AbbreviationOptions};
//!
//! let text = "The World Health Organization (WHO) is a specialized agency.";
//! let options = AbbreviationOptions::default();
//! let result = extract_abbreviation_definition_pairs(text, options).unwrap();
//!
//! for pair in result {
//! println!("Abbreviation: {}, Definition: {}", pair.abbreviation, pair.definition);
//! }
//! ```
//!
//! ### Python
//!
//! ```python
//! from abbreviation_extractor import extract_abbreviation_definition_pairs
//!
//! text = "The World Health Organization (WHO) is a specialized agency."
//! result = extract_abbreviation_definition_pairs(text)
//!
//! for pair in result:
//! print(f"Abbreviation: {pair.abbreviation}, Definition: {pair.definition}")
//! ```
//!
//! ## Customizing Extraction
//!
//! You can customize the extraction process using `AbbreviationOptions`:
//!
//! ```rust
//! use abbreviation_extractor::{extract_abbreviation_definition_pairs, AbbreviationOptions};
//!
//! let text = "The World Health Organization (WHO) is a specialized agency. \
//! The World Heritage Organization (WHO) is different.";
//!
//! // Get only the most common definition for each abbreviation
//! let options = AbbreviationOptions::new(true, false, true);
//! let result = extract_abbreviation_definition_pairs(text, options);
//!
//! // Get only the first definition for each abbreviation
//! let options = AbbreviationOptions::new(false, true, true);
//! let result = extract_abbreviation_definition_pairs(text, options);
//!
//! // Disable tokenization (if the input is already tokenized)
//! let options = AbbreviationOptions::new(false, false, false);
//! let result = extract_abbreviation_definition_pairs(text, options);
//! ```
//!
//! ## Parallel Processing
//!
//! For processing multiple texts in parallel, you can use the `extract_abbreviation_definition_pairs_parallel` function:
//!
//! ### Rust
//!
//! ```rust
//! use abbreviation_extractor::{extract_abbreviation_definition_pairs_parallel, AbbreviationOptions};
//!
//! let texts = vec![
//! "The World Health Organization (WHO) is a specialized agency.",
//! "The United Nations (UN) works closely with WHO.",
//! "The European Union (EU) is a political and economic union.",
//! ];
//!
//! let options = AbbreviationOptions::default();
//! let result = extract_abbreviation_definition_pairs_parallel(texts, options);
//!
//! for extraction in result.extractions {
//! println!("Abbreviation: {}, Definition: {}", extraction.abbreviation, extraction.definition);
//! }
//! ```
//!
//! ### Python
//!
//! ```python
//! from abbreviation_extractor import extract_abbreviation_definition_pairs_parallel
//!
//! texts = [
//! "The World Health Organization (WHO) is a specialized agency.",
//! "The United Nations (UN) works closely with WHO.",
//! "The European Union (EU) is a political and economic union.",
//! ]
//!
//! result = extract_abbreviation_definition_pairs_parallel(texts)
//!
//! for extraction in result.extractions:
//! print(f"Abbreviation: {extraction.abbreviation}, Definition: {extraction.definition}")
//! ```
//!
//! ## Processing Large Files
//!
//! For extracting abbreviations from large files, you can use the `extract_abbreviations_from_file` function:
//!
//! ### Rust
//!
//! ```rust
//! use abbreviation_extractor::{extract_abbreviations_from_file, AbbreviationOptions, FileExtractionOptions};
//!
//! let file_path = "path/to/your/large/file.txt";
//! let abbreviation_options = AbbreviationOptions::default();
//! let file_options = FileExtractionOptions::default();
//!
//! let result = extract_abbreviations_from_file(file_path, abbreviation_options, file_options);
//!
//! for extraction in result.extractions {
//! println!("Abbreviation: {}, Definition: {}", extraction.abbreviation, extraction.definition);
//! }
//! ```
//!
//! ### Python
//!
//! ```python
//! from abbreviation_extractor import extract_abbreviations_from_file
//!
//! file_path = "path/to/your/large/file.txt"
//! result = extract_abbreviations_from_file(file_path)
//!
//! for extraction in result.extractions:
//! print(f"Abbreviation: {extraction.abbreviation}, Definition: {extraction.definition}")
//! ```
//!
//! You can customize the file extraction process by specifying additional parameters:
//!
//! ```python
//! result = extract_abbreviations_from_file(
//! file_path,
//! most_common_definition=True,
//! first_definition=False,
//! tokenize=True,
//! num_threads=4,
//! show_progress=True,
//! chunk_size=2048 * 1024 # 2MB chunks
//! )
//! ```
//!
//! ## Functions
//!
//! The main functions provided by this library are:
//!
//! - [`extract_abbreviation_definition_pairs`]: Extracts abbreviation-definition pairs from a single text.
//! - [`extract_abbreviation_definition_pairs_parallel`]: Extracts abbreviation-definition pairs from multiple texts in parallel.
//! - [`extract_abbreviations_from_file`]: Extracts abbreviation-definition pairs from a large file.
//!
//! For detailed information on each function, please refer to their individual documentation.
//!
//! ## Structs/Enums
//! - [`AbbreviationOptions`]: Defines the `AbbreviationOptions` struct for customizing abbreviation extraction
//! - [`FileExtractionOptions`]: Defines the `FileExtractionOptions` struct for customizing file extraction for [`extract_abbreviations_from_file`]
//! - [`AbbreviationDefinition`]: Defines the `AbbreviationDefinition` struct for storing abbreviation-definition pairs
//! - [`ExtractionResult`]: Defines the `ExtractionResult` struct returned by [`extract_abbreviation_definition_pairs_parallel`] and [`extract_abbreviations_from_file`]
//! - [`ExtractionError`]: Defines the `ExtractionError` enum for error handling
//!
//! ## Modules
//!
//! - [`candidate`]: Defines the `Candidate` struct used in the extraction process
//! - [`extraction`]: Contains the core logic for extracting abbreviation-definition pairs
//! - [`utils`]: Utility functions and regular expressions used in the extraction process
//! - [`abbreviation_definitions`]: Defines the `AbbreviationDefinition` and `AbbreviationOptions` structs
use ;
use PyRuntimeError;
use *;
pub use ;
pub use Candidate;
pub use ;
/// Extracts abbreviation-definition pairs from a single text.
///
/// This function is exposed to Python and serves as a wrapper around the Rust
/// `extract_abbreviation_definition_pairs` function.
///
/// # Arguments
///
/// * `text` - The input text to extract abbreviation-definition pairs from.
/// * `most_common_definition` - If `Some(true)`, only the most common definition for each
/// abbreviation is returned. Default is `None` (false).
/// * `first_definition` - If `Some(true)`, only the first definition for each abbreviation
/// is returned. Default is `None` (false).
/// * `tokenize` - If `Some(true)`, the input text is tokenized before processing. Default is `None` (true).
/// * `ignore_errors` - If `Some(false)`, errors during extraction are ignored and an empty vector is returned.
///
/// # Returns
///
/// A `PyResult` containing a vector of `AbbreviationDefinition` structs.
/// Extracts abbreviation-definition pairs from multiple texts in parallel.
///
/// This function is exposed to Python and serves as a wrapper around the Rust
/// `extract_abbreviation_definition_pairs_parallel` function.
///
/// # Arguments
///
/// * `texts` - A vector of input texts to extract abbreviation-definition pairs from.
/// * `most_common_definition` - If `Some(true)`, only the most common definition for each
/// abbreviation is returned. Default is `None` (false).
/// * `first_definition` - If `Some(true)`, only the first definition for each abbreviation
/// is returned. Default is `None` (false).
/// * `tokenize` - If `Some(true)`, the input texts are tokenized before processing. Default is `None` (true).
///
/// # Returns
///
/// A `PyResult` containing an `ExtractionResult` struct.
/// Extracts abbreviation-definition pairs from a file.
///
/// This function is exposed to Python and serves as a wrapper around the Rust
/// `extract_abbreviations_from_file` function.
///
/// # Arguments
///
/// * `file_path` - The path to the file to extract abbreviations from.
/// * `most_common_definition` - If `Some(true)`, only the most common definition for each
/// abbreviation is returned. Default is `None` (false).
/// * `first_definition` - If `Some(true)`, only the first definition for each abbreviation
/// is returned. Default is `None` (false).
/// * `tokenize` - If `Some(true)`, the input text is tokenized before processing. Default is `None` (true).
/// * `chunk_size` - The size of chunks to read from the file at a time. Default is `1024 * 1024` (1MB).
/// * `num_threads` - The number of threads to use for parallel processing. Default is `num of cpus`.
/// * `show_progress` - If `Some(true)`, a progress bar is displayed during extraction. Default is `None` (true).
///
/// # Returns
///
/// A `PyResult` containing an `ExtractionResult` struct.
/// Initializes the Python module.
///
/// This function is called when the Python module is imported. It adds the Python-facing
/// functions to the module.
///
/// # Arguments
///
/// * `m` - The Python module to add the functions to.
///
/// # Returns
///
/// A `PyResult<()>` indicating success or failure of module initialization.