preprocessing.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from google.cloud import storage, translate
  2. from google.oauth2 import service_account
  3. from utils.preprocessing_fcn import batch_translate_text, upload_blob
  4. import logging
  5. import re
  6. import time
  7. import os
  8. project_id = os.getenv('PROJECT_ID')
  9. bucket_name = os.getenv('BUCKET_NAME')
  10. location = os.getenv('LOCATION')
  11. key_path = os.getenv('SA_KEY_PATH')
  12. credentials = service_account.Credentials.from_service_account_file(key_path)
  13. storage_client = storage.Client(credentials=credentials)
  14. translate_client = translate.TranslationServiceClient(credentials=credentials)
  15. lst_raw_txt_blobs = storage_client.list_blobs(bucket_or_name=bucket_name,
  16. prefix='raw_txt')
  17. customize_stop_words = [
  18. 'uoc', 'diagnostic', 'interventional', 'radiology', 'madonna', 'delle', 'grazie', 'hospital',
  19. 'Borgheresi', 'Agostini', 'Ottaviani', 'Floridi', 'Giovagnoni', 'di', 'specialization',
  20. 'Polytechnic', 'University', 'marche', 'ANCONA', 'Italy', 'Azienda', 'Ospedali',
  21. 'Riuniti', 'Yorrette', 'Matera', 'Michele', 'Nardella', 'Gerardo', 'Costanzo',
  22. 'Claudia', 'Lopez', 'st', 'a.', 'a', 'of', 's', 'cien', 'ze', 'diolog', 'ic', 'he',
  23. 'â', '€', 's', 'b', 'case', 'Cuoladi', 'l', 'c', 'ra', 'bergamo', 'patelli', 'est', 'asst',
  24. 'dr', 'Dianluigi', 'Svizzero', 'i', 'riccardo', 'Alessandro', 'Spinazzola', 'angelo',
  25. 'maggiore', 'p', 'r', 't', 'm', 'en', 't', 'o', 'd', 'e', 'n', 'd', 'o', 'g', 'h', 'u'
  26. ]
  27. start_time = time.time()
  28. for blob in lst_raw_txt_blobs:
  29. doc_title = blob.name.split('/')[-1].split('.')[0]
  30. txt_gcs_dest_path = 'gs://' + bucket_name + '/raw_txt/' + doc_title + '.txt'
  31. eng_txt_gcs_dest_path = 'gs://' + bucket_name + '/eng_txt/{}/'.format(doc_title)
  32. processed_eng_gcs_dest_path = 'gs://' + bucket_name + '/curated_eng_txt/' + doc_title + '.txt'
  33. # Translate raw text to english
  34. try:
  35. batch_translate_text(translate_client=translate_client,
  36. project_id=project_id,
  37. location=location,
  38. input_uri=txt_gcs_dest_path,
  39. output_uri=eng_txt_gcs_dest_path)
  40. logging.info("Translation of {} document was successful.".format(doc_title))
  41. except Exception as e:
  42. logging.error("Error", e)
  43. # Process eng raw text
  44. blob_prefix = 'eng_txt/{}/{}_raw_txt_{}_en_translations.txt'.format(doc_title,
  45. bucket_name,
  46. doc_title)
  47. eng_blob = storage_client.get_bucket(bucket_name).get_blob(blob_prefix)
  48. eng_raw_string = eng_blob.download_as_string().decode('utf-8')
  49. # Remove dates
  50. # 1 or 2 digit number followed by back slash followed by 1 or 2 digit number ...
  51. pattern_dates = '(\d{1,2})/(\d{1,2})/(\d{4})'
  52. pattern_fig = 'Figure (\d{1,2})'
  53. pattern_image = '^Image .$'
  54. replace = ''
  55. eng_raw_string = re.sub(pattern_dates, replace, eng_raw_string)
  56. eng_raw_string = re.sub(pattern_fig, replace, eng_raw_string)
  57. eng_raw_string = re.sub(pattern_image, replace, eng_raw_string)
  58. # remove punctuation and special characters
  59. eng_raw_string = re.sub('[^A-Za-z0-9]+', ' ', eng_raw_string)
  60. # Remove custom stop words
  61. tokens = [token for token in eng_raw_string.split() if token not in customize_stop_words]
  62. refined_doc = ''
  63. for word in tokens:
  64. refined_doc += ' {}'.format(word)
  65. # Upload raw text to GCS
  66. upload_blob(refined_doc, processed_eng_gcs_dest_path)
  67. logging.info("The curation of {} text completed successfully.".format(doc_title))
  68. total_time = time.time() - start_time
  69. logging.info('The translation and curation of all documents was successfully completed in {} minutes.'.format(
  70. round(total_time / 60, 1)))