preprocessing.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from google.cloud import storage
  2. from google.oauth2 import service_account
  3. from covid19_ISMIR.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. project_id=project_id)
  15. lst_json_blobs = storage_client.list_blobs(bucket_or_name=bucket_name,
  16. prefix='json')
  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_json_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(project_id=project_id,
  36. location=location,
  37. input_uri=txt_gcs_dest_path,
  38. output_uri=eng_txt_gcs_dest_path)
  39. logging.info("Translation of {} document was successful.".format(doc_title))
  40. except Exception, e:
  41. logging.error("Error", e)
  42. # Process eng raw text
  43. blob_prefix = 'eng_txt/{}/{}_raw_txt_{}_en_translations.txt'.format(doc_title,
  44. bucket_name,
  45. doc_title)
  46. eng_blob = storage_client.get_bucket(bucket_name).get_blob(blob_prefix)
  47. eng_raw_string = eng_blob.download_as_string().decode('utf-8')
  48. # Remove dates
  49. # 1 or 2 digit number followed by back slash followed by 1 or 2 digit number ...
  50. pattern_dates = '(\d{1,2})/(\d{1,2})/(\d{4})'
  51. pattern_fig = 'Figure (\d{1,2})'
  52. pattern_image = '^Image .$'
  53. replace = ''
  54. eng_raw_string = re.sub(pattern_dates, replace, eng_raw_string)
  55. eng_raw_string = re.sub(pattern_fig, replace, eng_raw_string)
  56. eng_raw_string = re.sub(pattern_image, replace, eng_raw_string)
  57. # remove punctuation and special characters
  58. eng_raw_string = re.sub('[^A-Za-z0-9]+', ' ', eng_raw_string)
  59. # Remove custom stop words
  60. tokens = [token for token in eng_raw_string.split() if token not in customize_stop_words]
  61. refined_doc = ''
  62. for word in tokens:
  63. refined_doc += ' {}'.format(word)
  64. # Upload raw text to GCS
  65. upload_blob(refined_doc, processed_eng_gcs_dest_path)
  66. logging.info("The curation of {} text completed successfully.".format(doc_title))
  67. total_time = time.time() - start_time
  68. logging.info('The translation and curation of all documents was successfully completed in {} minutes.'.format(
  69. round(total_time / 60, 1)))