Advertisement
Guest User

Untitled

a guest
Nov 1st, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. """
  2. PQ Gram Implementation
  3. """
  4.  
  5. mock = [
  6. {'from': 'a', 'to': 'b'},
  7. {'from': 'b', 'to': 'c'},
  8. {'from': 'b', 'to': 'd'},
  9. {'from': 'a', 'to': 'e'},
  10. {'from': 'a', 'to': 'f'}
  11. ] # children must be from left to right
  12.  
  13.  
  14. def shift(reg, el):
  15. reg.pop(0)
  16. reg.append(el)
  17. return reg
  18.  
  19.  
  20. def pq_profile(T, P, p, q, r):
  21. anc = ['*'] * p
  22. P = profile(T, p, q, P, r, anc)
  23. return P
  24.  
  25.  
  26. def is_leaf(r, T):
  27. for node in T:
  28. if node['from'] == r:
  29. return False
  30. return True
  31.  
  32.  
  33. def get_children(r, T):
  34. children = []
  35. for node in T:
  36. if node['from'] == r:
  37. children.append(node['to'])
  38. return children
  39.  
  40.  
  41. def profile(T, p, q, P, r, anc): # *, *
  42. anc = shift(anc, r)
  43. sib = ['*'] * q
  44.  
  45. if is_leaf(r, T):
  46. P.append(anc + sib)
  47. else:
  48. children = get_children(r, T)
  49. for c in children:
  50. sib = shift(sib, c)
  51. P.append(anc + sib)
  52. P = profile(T, p, q, P, c, anc)
  53. for k in range(1, q):
  54. sib = shift(sib, '*')
  55. P.append(anc + sib)
  56. return P
  57.  
  58.  
  59. gramProfile = pq_profile(mock, [], 2, 3, mock[0]['from'])
  60.  
  61. for line in gramProfile:
  62. print(line)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement