Advertisement
Guest User

lab3

a guest
Oct 14th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. import decimal as dec
  2. class Symbolfr:
  3. def __init__(self, symbol, frequency):
  4. self.symbol = symbol
  5. self.frequency = frequency
  6.  
  7. class Symbolbr:
  8. def __init__(self, symbol, low_board, high_board):
  9. self.symbol = symbol
  10. self.low_board = low_board
  11. self.high_board = high_board
  12. def _get_frequency(element):
  13. return element.frequency
  14. def get_symbols_boards(symbols_dict, symbol):
  15. for sym in symbols_dict:
  16. if sym.symbol == symbol:
  17. return sym.low_board, sym.high_board
  18. def get_symbols_frequency(user_string, precision=250):
  19. if precision <= 0:
  20. raise ValueError("Precision of encoding can not be less than zero")
  21. if len(user_string) == 0:
  22. raise ValueError("Received empty string")
  23. symbols_array = []
  24. for ch in list(user_string):
  25. symbols_array.append(ch)
  26. symbols_array.sort()
  27. symbols_frequency = []
  28. count = dec.Decimal(0)
  29. for ch_i in list(symbols_array):
  30. for ch_g in list(symbols_array):
  31. if ch_i == ch_g:
  32. count += dec.Decimal(1)
  33. is_exist = False
  34. for sym in symbols_frequency:
  35. if sym.symbol == ch_i:
  36. is_exist = True
  37. if not is_exist:
  38. symbols_frequency.append(Symbolfr(ch_i, count))
  39. count = dec.Decimal(0)
  40. symbols_frequency.sort(key=_get_frequency, reverse=True)
  41. dec.getcontext().prec = precision
  42. return symbols_frequency
  43. def get_symbols_intervals(user_string, precision=250):
  44. if precision <= 0:
  45. raise ValueError("Precision of encoding can not be less than zero")
  46. vector_length = dec.Decimal(0)
  47. symbols_frequency = get_symbols_frequency(user_string, precision)
  48. for sym in symbols_frequency:
  49. if sym.frequency <= 0:
  50. raise ValueError("Frequency of symbol less than zero")
  51. vector_length += sym.frequency
  52. symbols_intervals = []
  53. length = dec.Decimal(0)
  54. for element in symbols_frequency:
  55. low_board = length
  56. high_board = low_board + element.frequency / vector_length
  57. length += element.frequency / vector_length
  58. symbols_intervals.append(Symbolbr(element.symbol, low_board, high_board))
  59. return symbols_intervals
  60. def encode(user_string, precision=250):
  61. if precision <= 0:
  62. raise ValueError("Precision of encoding can not be less than zero")
  63. if len(user_string) == 0:
  64. raise ValueError("Received empty string")
  65. dec.getcontext().rounding = dec.ROUND_UP
  66. vector_length = dec.Decimal(0)
  67. symbols_frequency = get_symbols_frequency(user_string, precision)
  68. for sym in symbols_frequency:
  69. vector_length += sym.frequency
  70. symbols_intervals = get_symbols_intervals(user_string, precision)
  71. low_old = dec.Decimal(0)
  72. low_board = dec.Decimal(0)
  73. high_old = dec.Decimal(1)
  74. high_board = dec.Decimal(1)
  75. for ch in list(user_string):
  76. low, high = get_symbols_boards(symbols_intervals, ch)
  77. high_board = low_old + (high_old - low_old) * high
  78. low_board = low_old + (high_old - low_old) * low
  79. high_old = high_board
  80. low_old = low_board
  81. dec.getcontext().prec = precision
  82. return low_board, high_board
  83. def decode(symbols_frequency, code, precision_of_string=10):
  84. if precision_of_string <= 0:
  85. raise ValueError("Precision of string can not be less than zero")
  86. if code < 0 or code >= 1:
  87. raise ValueError("Incorrect code")
  88. vector_length = dec.Decimal(0)
  89. user_string = ""
  90. for sym in symbols_frequency:
  91. vector_length += sym.frequency
  92. count = int(sym.frequency)
  93. while count > 0:
  94. user_string += sym.symbol
  95. count -= 1
  96. dec.getcontext().rounding = dec.ROUND_UP
  97. symbols_intervals = get_symbols_intervals(user_string)
  98. first_char = ""
  99. for sym in symbols_frequency:
  100. low, high = get_symbols_boards(symbols_intervals, sym.symbol)
  101. if low <= code <= high:
  102. first_char = sym.symbol
  103. user_string = [first_char]
  104. for i in range(0, precision_of_string - 1):
  105. low, high = get_symbols_boards(symbols_intervals, user_string[i])
  106. code = (code - low) / (high - low)
  107. for sym in symbols_frequency:
  108. low, high = get_symbols_boards(symbols_intervals, sym.symbol)
  109. if low < code <= high:
  110. user_string.append(sym.symbol)
  111. result_string = ""
  112. for ch in user_string:
  113. result_string += ch
  114. return result_string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement