order_bounding_boxes_in_each_block.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from bs4 import BeautifulSoup
  2. import subprocess
  3. import re
  4. def get_bound_box(file):
  5. response = open(file)
  6. html_doc = response.read()
  7. response.close()
  8. html_file = BeautifulSoup(html_doc, 'html.parser')
  9. all_elements = []
  10. blocks = html_file.findAll('block')
  11. number_blocks = len(blocks)
  12. number_words = 0
  13. for block in blocks:
  14. list_elements = []
  15. words = block.findAll('word')
  16. number_words += len(words)
  17. for word in words:
  18. word_list = [word["xmin"], word["ymin"], word["xmax"], word["ymax"], word.string]
  19. list_elements.append(word_list)
  20. all_elements.append(list_elements)
  21. new_all_elements = []
  22. for element in all_elements:
  23. later_bigger = (float(element[-1][0])-(float(element[0][0]))) #check if xmin from first element is bigger than xmin from last element
  24. if later_bigger >= -5:
  25. new_all_elements.append(element)
  26. else:
  27. new_element = sorted(element, key=lambda k: [float(k[0])])
  28. new_all_elements.append(new_element)
  29. return new_all_elements, number_blocks, number_words
  30. def pdf_to_html(uuid,filepath, path):
  31. filename = path +"/temporary/" +str(uuid)+"out.html"
  32. print(filename)
  33. subprocess.call(['pdftotext', '-bbox-layout',
  34. filepath, filename])
  35. return filename
  36. def extract_isos(result):
  37. reg = r"(ISO\s\d\d\d\d*\W?\d?\W?\d?)|(EN\s\d*)"
  38. details_ = []
  39. reg_general = r"ISO\s?\d*\s*\W\s*[fmcv][HKL]"
  40. general_tol = ""
  41. for element in result:
  42. new_arr = ""
  43. for x in element:
  44. new_arr += x[4] + " "
  45. if re.search(reg,new_arr):
  46. found = re.findall(reg, new_arr)
  47. for f in found:
  48. if len(f[0]) != 0:
  49. details_.append(f[0].replace(")",""))
  50. if len(f[1]) != 0:
  51. details_.append(f[1])
  52. if re.search(reg_general, new_arr):
  53. general_tol = new_arr
  54. return details_, str(general_tol)
  55. def get_tables(result):
  56. reg = r"(Start drawing)|(All dimensions)"
  57. tables = []
  58. for element in result:
  59. new = []
  60. if re.search(reg, element):
  61. new.extend(result[element])
  62. new.append(element)
  63. tables.append(new)
  64. number = len(tables)
  65. return tables