-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathformat_document_level_corpus.py
112 lines (97 loc) · 3.92 KB
/
format_document_level_corpus.py
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
# coding=utf-8
# Copyright (c) 2020, Hicham EL BOUKKOURI. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Script for formatting document-level corpora: single file with one sentence
per line and a blank line between sentences from different documents.
"""
import os
import logging
import argparse
import nltk
from tqdm import tqdm
from transformers import BasicTokenizer
WORKDIR = os.environ['WORKDIR']
FORMATTED_DATA_DIRECTORY = os.path.join(WORKDIR, 'data', 'formatted')
os.makedirs(FORMATTED_DATA_DIRECTORY, exist_ok=True)
LOGGING_FORMAT = "%(asctime)s | PID: %(process)d | %(filename)s | %(levelname)s - %(message)s"
logging.basicConfig(format=LOGGING_FORMAT, datefmt="%d/%m/%Y %H:%M:%S", level=logging.INFO)
def main():
"""Script for formatting document-level corpora."""
parser = argparse.ArgumentParser(
description=\
'Formats a document-level corpus.'
)
parser.add_argument(
'--document_level_corpus_path',
required=True, type=str,
help=\
'Path to the document level corpus: one document per line '
'+ blank line between documents.',
)
args = parser.parse_args()
logging.info('Preparing to format a document-level corpus using parameters:')
for argname, argvalue in vars(args).items():
logging.info(' * %s: %s', argname, argvalue)
# Make sure document-level corpus exists
assert os.path.exists(args.document_level_corpus_path)
# Make output directory
formatted_corpus_path = os.path.join(
FORMATTED_DATA_DIRECTORY,
os.path.basename(
os.path.dirname(
args.document_level_corpus_path)),
'formatted.txt'
)
os.makedirs(
os.path.dirname(formatted_corpus_path),
exist_ok=True
)
# Make sur output corpus does not already exist
if os.path.exists(formatted_corpus_path):
logging.warning(
'Found corpus file: %s',
formatted_corpus_path.replace(WORKDIR, '$WORKDIR'))
logging.warning('Aborted formatting.')
return
# Tokenizer & sentence segmenter
tokenizer = BasicTokenizer()
split_into_sentences = nltk.tokenize.sent_tokenize
logging.info('Using NLTK sentence segmenter.')
split_into_tokens = tokenizer.tokenize
logging.info('Using huggingface/transformers BasicTokenizer.')
# Actual formatting
logging.info('Formatting corpus...')
input_file_path = args.document_level_corpus_path
output_file_path = formatted_corpus_path
n_tokens = 0
n_sentences = 0
with open(input_file_path, "r", encoding="utf-8") as input_file:
with open(output_file_path, "w", encoding="utf-8") as output_file:
for line in tqdm(input_file, desc='Segmenting corpus'):
if line.strip(): # if document
sentences = split_into_sentences(line)
for sentence in sentences:
tokens = split_into_tokens(sentence.strip())
new_line = ' '.join(tokens) + '\n'
output_file.write(new_line)
n_sentences += 1
n_tokens += len(tokens)
else: # if blank line
output_file.write('\n')
logging.info('Done formatting.')
logging.info('* Total number of sentences: %s', n_sentences)
logging.info('* Total number of tokens: %s', n_tokens)
if __name__ == "__main__":
main()