Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #!/usr/bin/python3
  2. from __future__ import print_function
  3. import sys
  4. import re
  5. from typing import Match
  6.  
  7. pattern = re.compile('^([0-9]*) ([0-9]*) ([a-z]*)( [0-9]\.[0-9]*)?$')
  8. patternOneLetter = re.compile('^[0-9]*$')
  9. automat = {}
  10. endState = []
  11. endStateI = 0
  12. currentState = 0;
  13. weight = 0
  14. word = ""
  15. isTrue = 0
  16. isSimple1 = 0
  17. isSimple2 = 0
  18.  
  19. def simple1():
  20. filepath = '/home/students/s153085/daut-2018-s153085/TaskB01/simple1.arg'
  21. isSimple1 = 0
  22. with open(sys.argv[1], 'r') as fp:
  23. line = fp.readline()
  24. while line:
  25. # tutaj pisać
  26. s = list(line)
  27. del s[len(s) - 1]
  28. word = ''.join(s)
  29. currentState = 0
  30. weight = 0
  31. isTrue = 0
  32.  
  33. for char in word:
  34. if automat.get((currentState, char)):
  35. state = automat.get((currentState, char))
  36. weight = weight + state[1]
  37. currentState = state[0]
  38. else:
  39. currentState = 0
  40. state = 0
  41.  
  42. for i in endState:
  43. if currentState == i:
  44. isTrue = 1
  45.  
  46. if isTrue == 1:
  47. if sys.argv[1] == "TaskB01/simple1.arg":
  48. print("TAK " + word + " " + str(weight))
  49. else:
  50. print("TAK " + word + " %.3f" % float(weight))
  51. else:
  52. print("NIE {}".format(word))
  53. line = fp.readline()
  54.  
  55. def simple2():
  56. isSimple2 = 0
  57. filepath = '/home/students/s153085/daut-2018-s153085/TaskB01/simple2.arg'
  58. with open(filepath) as fp:
  59. line = fp.readline()
  60.  
  61. while line:
  62. # tutaj pisać
  63. s = list(line)
  64. del s[len(s) - 1]
  65. word = ''.join(s)
  66. currentState = 0
  67. weight = 0
  68. isTrue = 0
  69.  
  70. for char in word:
  71. if automat.get((currentState, char)):
  72. state = automat.get((currentState, char))
  73. currentState = state[0]
  74. weight = weight + state[1]
  75. else:
  76. currentState = 0
  77. state = 0
  78.  
  79. for i in endState:
  80. if currentState == i:
  81. isTrue = 1
  82.  
  83. if isTrue == 1:
  84. print("TAK " + word + " %3f" % float(weight))
  85. else:
  86. print("NIE {}".format(word))
  87. line = fp.readline()
  88.  
  89. for line in sys.stdin:
  90. # print("line: " + line)
  91. a = pattern.match(line)
  92. if a:
  93. groupFour = 0
  94. if not a.group(4):
  95. groupFour = 0.000
  96. else:
  97. groupFour = a.group(4)
  98.  
  99. if not automat.get((int(a.group(1)), a.group(3))):
  100. automat[(int(a.group(1)), a.group(3))] = (int(a.group(2)), float(groupFour))
  101.  
  102. b = patternOneLetter.match(line)
  103. if b:
  104. endState.insert(endStateI, int(line))
  105. endStateI += 1
  106.  
  107. simple1()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement