-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbeam.py
54 lines (44 loc) · 1.2 KB
/
beam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import heapq
class Beam(object):
"""
A class to represent a Beam data structure
...
Attributes
----------
heap: list
list containing probability, token and setence completetion boolean information
beam_width: int
size of beam
"""
#For comparison of prefixes, the tuple (prefix_probability, complete_sentence) is used.
#This is so that if two prefixes have equal probabilities then a complete sentence is preferred over an incomplete one since (0.5, False) < (0.5, True)
def __init__(self, beam_width):
"""
Initiate the beam
Parameters
----------
beam_width: int
size of beam
"""
self.heap = list()
self.beam_width = beam_width
def add(self, prob, complete, prefix):
"""
Add item to the heap queue. Also pop from heap if beam size is greate than max defined
Parameters
----------
prob: float
Probability value of current sentence in the beam
complete: boolean
if the sentence is completed or not
prefix:
last n-1 token in the current sentence
"""
heapq.heappush(self.heap, (prob, complete, prefix))
if len(self.heap) > self.beam_width:
heapq.heappop(self.heap)
def __iter__(self):
"""
Iterate over the heap structure
"""
return iter(self.heap)