remove_group_duplicates.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # coding=utf-8
  2. # Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import json
  16. import time
  17. import sys
  18. if __name__ == '__main__':
  19. url_filename = sys.argv[1]
  20. data_filename = sys.argv[2]
  21. output_filename = sys.argv[3]
  22. urls = set()
  23. with open(url_filename, 'r') as f:
  24. for line in f:
  25. myjson = json.loads(line)
  26. for key in myjson:
  27. this_urls = myjson[key]
  28. for i in range(1, len(this_urls)):
  29. urls.add(this_urls[i])
  30. print('will be removing {} urls'.format(len(urls)), flush=True)
  31. written_docs = 0
  32. removed_docs = 0
  33. removed_chars = 0
  34. start_time = time.time()
  35. with open(output_filename, 'wb') as fout:
  36. with open(data_filename, 'r') as fin:
  37. for line in fin:
  38. try:
  39. myjson = json.loads(line)
  40. url = myjson['url']
  41. if url in urls:
  42. print('removing', myjson)
  43. removed_docs += 1
  44. removed_chars += len(myjson['text'])
  45. continue
  46. myjson = json.dumps(myjson, ensure_ascii=False)
  47. fout.write(myjson.encode('utf-8'))
  48. fout.write('\n'.encode('utf-8'))
  49. written_docs += 1
  50. if written_docs % 10000 == 0:
  51. print(' [PROCESSED] time (s): {:.2f} | written: {} '
  52. '| removed: {} (char: {})'.format(
  53. time.time() - start_time,
  54. written_docs, removed_docs, removed_chars))
  55. except Exception as e:
  56. print('[SKIPPING]', line, e)
  57. print(' [PROCESSED] time (s): {:.2f} | written: {} '
  58. '| removed: {} (char: {})'.format(
  59. time.time() - start_time,
  60. written_docs, removed_docs, removed_chars))
  61. print('done :-)')