generators.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import io
  2. import random
  3. from hashlib import sha1
  4. from math import floor
  5. import requests
  6. import numpy
  7. from PIL import Image
  8. from rdflib import Graph, URIRef, Literal
  9. from rdflib.namespace import Namespace, NamespaceManager
  10. # @TODO Update this to include code point ranges to be sampled
  11. include_ranges = [
  12. ( 0x0021, 0x0021 ),
  13. ( 0x0023, 0x0026 ),
  14. ( 0x0028, 0x007E ),
  15. ( 0x00A1, 0x00AC ),
  16. ( 0x00AE, 0x00FF ),
  17. ( 0x0100, 0x017F ),
  18. ( 0x0180, 0x024F ),
  19. ( 0x2C60, 0x2C7F ),
  20. ( 0x16A0, 0x16F0 ),
  21. ( 0x0370, 0x0377 ),
  22. ( 0x037A, 0x037E ),
  23. ( 0x0384, 0x038A ),
  24. ( 0x038C, 0x038C ),
  25. ]
  26. def random_utf8_string(length):
  27. alphabet = [
  28. chr(code_point) for current_range in include_ranges
  29. for code_point in range(current_range[0], current_range[1] + 1)
  30. ]
  31. return ''.join(random.choice(alphabet) for i in range(length))
  32. def random_image(name, ts=8, ims=256):
  33. imarray = numpy.random.rand(ts, ts, 3) * 255
  34. im = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
  35. im = im.resize((ims, ims), Image.NEAREST)
  36. imf = io.BytesIO()
  37. im.save(imf, format='png')
  38. imf.seek(0)
  39. hash = sha1(imf.read()).hexdigest()
  40. return {
  41. 'content' : imf,
  42. 'hash' : hash,
  43. 'filename' : random_utf8_string(32) + '.png'
  44. }
  45. nsm = NamespaceManager(Graph())
  46. nsc = {
  47. 'extp': Namespace('http://ex.org/exturi_p#'),
  48. 'intp': Namespace('http://ex.org/inturi_p#'),
  49. 'litp': Namespace('http://ex.org/lit_p#'),
  50. }
  51. for pfx, ns in nsc.items():
  52. nsm.bind(pfx, ns)
  53. def random_graph(size, ref):
  54. '''
  55. Generate a synthetic graph.
  56. @param size (int) size Size of the graph. It will be rounded by a
  57. multiplier of 4.
  58. '''
  59. gr = Graph()
  60. gr.namespace_manager = nsm
  61. for ii in range(floor(size / 4)):
  62. gr.add((
  63. URIRef(''),
  64. nsc['intp'][str(ii % size)],
  65. URIRef(ref)
  66. ))
  67. gr.add((
  68. URIRef(''),
  69. nsc['litp'][str(ii % size)],
  70. Literal(random_utf8_string(64))
  71. ))
  72. gr.add((
  73. URIRef(''),
  74. nsc['litp'][str(ii % size)],
  75. Literal(random_utf8_string(64))
  76. ))
  77. gr.add((
  78. URIRef(''),
  79. nsc['extp'][str(ii % size)],
  80. URIRef('http://example.edu/res/{}'.format(ii // 10))
  81. ))
  82. #print('Graph: {}'.format(gr.serialize(format='turtle').decode('utf-8')))
  83. return gr