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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 | #1 -- get dialogues
finals = []
dialogues = cands.dialogue_num.drop_duplicates()
#dialogues = [97]
for d in tqdm(dialogues):
#assume each target cannot be used more than once
target_memo = []
# 2 -- get segment list
seg_list = get_seg_list(cands[cands.dialogue_num == d])
# 3 -- create seg pairs list
seg_pairs = []
for i, s in enumerate(seg_list):
for n in [j for j in reversed(range((i+1)-20, i+1)) if j>=0]:
try:
seg_pairs.append((seg_list[n], seg_list[i+1]))
except IndexError:
pass
# 4 -- for each pair, pull row and append rules
for s in seg_pairs:
row = cands[(cands.source_id == s[0]) & (cands.target_id == s[1])]
if row.empty:
#check for backwards links
backwards = cands[(cands.source_id == s[1]) & (cands.target_id == s[0])]
if backwards.shape[0] > 0:
b_index = backwards.index[0]
b_row = backwards.iloc[0]
if (b_row.source_turn_id == b_row.target_turn_id) and LF_Com_L_L_case1(b_row):
if b_row.target_id in target_memo:
finals.append((b_index, 0))
else:
finals.append((b_index, 1))
target_memo.append(b_row.target_id)
else:
finals.append((b_index, 0))
continue
else:
r_index = row.index[0]
row = row.iloc[0]
backwards = cands[(cands.source_id == s[1]) & (cands.target_id == s[0])]
#we only look at ling --> ling cases
if row.source_type != 'Segment' or row.target_type != 'Segment':
finals.append((r_index, 0))
if backwards.shape[0] > 0:
b_index = backwards.index[0]
finals.append((b_index, -1))
elif LF_Com_L_L_case1(row):
if row.target_id in target_memo:
finals.append((r_index, 0))
else:
finals.append((r_index, 1))
target_memo.append(row.target_id)
#then no backwards link
if backwards.shape[0] > 0:
b_index = backwards.index[0]
finals.append((b_index, -1))
elif backwards.shape[0] > 0:
b_row = backwards.iloc[0]
b_index = backwards.index[0]
if (b_row.source_turn_id == b_row.target_turn_id) and LF_Com_L_L_case1(b_row):
if row.target_id in target_memo:
finals.append((b_index, 0))
finals.append((r_index, 0))
else:
finals.append((b_index, 1))
finals.append((r_index, -1))
target_memo.append(row.target_id)
else:
finals.append((b_index, 0))
finals.append((r_index, 0))
else:
finals.append((r_index, 0))
|