You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

news_crawl.py 2.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Generate News Crawl corpus dataset."""
  16. import argparse
  17. from src.utils import Dictionary
  18. from src.utils.preprocess import create_pre_training_dataset
  19. parser = argparse.ArgumentParser(description='Create News Crawl Pre-Training Dataset.')
  20. parser.add_argument("--src_folder", type=str, default="", required=True,
  21. help="Raw corpus folder.")
  22. parser.add_argument("--existed_vocab", type=str, default="", required=True,
  23. help="Existed vocab path.")
  24. parser.add_argument("--mask_ratio", type=float, default=0.4, required=True,
  25. help="Mask ratio.")
  26. parser.add_argument("--output_folder", type=str, default="", required=True,
  27. help="Dataset output path.")
  28. parser.add_argument("--max_len", type=int, default=32, required=False,
  29. help="Max length of sentences.")
  30. parser.add_argument("--suffix", type=str, default="", required=False,
  31. help="Add suffix to output file.")
  32. parser.add_argument("--processes", type=int, default=2, required=False,
  33. help="Size of processes pool.")
  34. if __name__ == '__main__':
  35. args, _ = parser.parse_known_args()
  36. if not (args.src_folder and args.output_folder):
  37. raise ValueError("Please enter required params.")
  38. if not args.existed_vocab:
  39. raise ValueError("`--existed_vocab` is required.")
  40. vocab = Dictionary.load_from_persisted_dict(args.existed_vocab)
  41. create_pre_training_dataset(
  42. folder_path=args.src_folder,
  43. output_folder_path=args.output_folder,
  44. vocabulary=vocab,
  45. prefix="news.20", suffix=args.suffix,
  46. mask_ratio=args.mask_ratio,
  47. min_sen_len=10,
  48. max_sen_len=args.max_len,
  49. dataset_type="tfrecord",
  50. cores=args.processes
  51. )
  52. print(f" | Vocabulary size: {vocab.size}.")