preprocessing.py 3.9 KB

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