Advertisement
Void-voiD

Untitled

Apr 12th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. import re
  2. import numpy as np
  3. from scipy import spatial as sp
  4.  
  5. amount_of_words = 0
  6. amount_of_sentences = 0
  7. freq = {}
  8. sentences = []
  9. text = open("2.3.1.txt", 'r')
  10.  
  11. for line in text:
  12. amount_of_sentences += 1
  13. current = re.split('[^a-z]', line[: -2].lower())
  14. result = []
  15. for cur in current:
  16. if cur != '':
  17. result.append(cur)
  18. if cur not in freq.values():
  19. freq[amount_of_words] = cur
  20. amount_of_words += 1
  21. sentences.append(result)
  22. text.close()
  23.  
  24. matrix_freq = np.zeros((amount_of_sentences, amount_of_words))
  25. i = 0
  26.  
  27. for i in range(amount_of_sentences):
  28. for j in range(amount_of_words):
  29. matrix_freq[i][j] = sentences[i].count(freq[j])
  30.  
  31. results = []
  32. comp = matrix_freq[0]
  33. min1 = 1
  34. min2 = 1
  35. min1_index = 0
  36. min2_index = 0
  37. for i in range(1, amount_of_sentences):
  38. x = sp.distance.cosine(comp, matrix_freq[i])
  39. results.insert(i, x)
  40. if x <= min1:
  41. min2 = min1
  42. min2_index = min1_index
  43. min1 = x
  44. min1_index = i
  45. elif x <= min2:
  46. min2 = x
  47. min2_index = i
  48.  
  49. # for i in matrix_freq:
  50. # print(i)
  51. print(results)
  52. results.sort()
  53. print(results)
  54. print(min1, min2)
  55. print(min1_index, min2_index)
  56.  
  57. res = open('2.3.1.result.txt', 'w')
  58. res.write(str(min1_index) + ' ' + str(min2_index))
  59. res.close()
  60.  
  61.  
  62. -----------------------------------------------------------------------------
  63.  
  64.  
  65. import re
  66. import numpy as np
  67. from math import pi, sin, exp
  68.  
  69.  
  70. def f(x):
  71. return sin(x / 5) * exp(x / 10) + 5 * exp(-1 * x / 2)
  72.  
  73.  
  74. f_1 = f(1)
  75. f_4 = f(4)
  76. f_8 = f(8)
  77. f_10 = f(10)
  78. f_15 = f(15)
  79.  
  80. matrix_1 = np.array([[1, 1],
  81. [1, 15]])
  82. b_1 = np.array([f_1, f_15])
  83. x_1 = np.linalg.solve(matrix_1, b_1)
  84.  
  85. # .....
  86.  
  87. matrix_3 = np.array([[1, 1, 1, 1],
  88. [1, 4, 16, 64],
  89. [1, 10, 100, 1000],
  90. [1, 15, 225, 3375]])
  91. b_3 = np.array([f_1, f_4, f_10, f_15])
  92. x_3 = np.linalg.solve(matrix_3, b_3)
  93. x = list(map(str, x_3))
  94. print(type(x[0]))
  95. print(x)
  96.  
  97. result = open("2.3.2.result.txt", 'w')
  98. result.write(x[0] + ' ' + x[1] + ' ' + x[2] + ' ' + x[3])
  99. result.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement