-
Notifications
You must be signed in to change notification settings - Fork 3
/
bucket.py
154 lines (121 loc) · 4.73 KB
/
bucket.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import numpy as np
twitter_most_frequent_words = ['the','i', 'to', 'a', 'and', 'is', 'in', 'it', 'you', 'of']
def softmax(x):
b = x.max()
y = np.exp(x - b)
return y / y.sum()
def total_variation(p, q):
'''Returns the total variation of two distributions over a finite set.'''
# We use 1-norm to compute total variation.
# d_TV(p, q) := sup_{A \in sigma} |p(A) - q(A)|
# = 1/2 * sum_{x \in X} |p(x) - q(x)| = 1/2 * ||p - q||_1
return 0.5 * np.sum(np.abs(p - q))
def bitblock_to_tokens(vocab, N_blocks_length):
'''
Args:
vocab: vocabulary in a list [token1, token2, ..., tokenx]
N_blocks: number of bit blocks
Return:
bits2tokens: a dictionary {bit_block: token_list}
token2bits: a dictionary {token: it's bit_block}
'''
assert N_blocks_length < 8 # 2^10 is too large, each block contains too few tokens
N_blocks = 2 ** N_blocks_length
batch_size = len(vocab) // N_blocks
bits2tokens = {}
token2bits = {}
for i in range(N_blocks-1):
format_str = '0'+str(N_blocks_length)+'b'
bits = format(i, format_str)
bits2tokens[bits] = vocab[i*batch_size: (i+1)*batch_size]
last_format_str = '0'+str(N_blocks_length)+'b'
last_bits = format(N_blocks - 1, last_format_str)
bits2tokens[last_bits] = vocab[(N_blocks-1)*batch_size:]
for key, tokens in bits2tokens.items():
for token in tokens:
token2bits[token] = key
return bits2tokens, token2bits
def encode(bits2tokens, N_blocks_length, bitstring, p):
'''
Args:
bits2tokens_common: a dictionary {bits: token_ls}
bitstring: the bit string
p: probability distribution over vocab
Return:
stega_text
'''
blocks = 2**N_blocks_length
blocks_num = len(bitstring) // N_blocks_length
# get bits block sequence
bits_blocks = [bitstring[i*N_blocks_length: (i+1)*N_blocks_length] for i in range(blocks_num)]
# get the decimal representation of those bits
decimal_rep = [int(bit_block, 2) for bit_block in bits_blocks]
# get block size on p distribution
p_blocks_size = p.shape[0] // blocks
# divide p into blocks
block_p = []
for i in range(blocks-1):
block_p.append(p[i*p_blocks_size: (i+1)*p_blocks_size])
block_p.append(p[(blocks-1)*p_blocks_size: ])
# get the p blocks corresponding to the bits blocks
correspond_p_blocks = [block_p[i] for i in decimal_rep]
# re-normalize the p block
normed_block_p = [softmax(p) for p in correspond_p_blocks]
tokens = []
for i in range(blocks_num):
block_tokens = bits2tokens[bits_blocks[i]]
token = np.random.choice(block_tokens, 1, replace=False, p=normed_block_p[i])
tokens.append(token[0])
while token in twitter_most_frequent_words:
token = np.random.choice(block_tokens, 1, replace=False, p=normed_block_p[i])
tokens.append(token[0])
stega_text = " ".join(tokens)
return stega_text
def decode(token2bits, N_blocks_length, string):
'''
Args:
token2bits: a dictionary {token: bit_block}
string: a sentence(assume can be tokenized by space)
Return:
bitstring
'''
tokens = string.split(" ")
encoded_ls = []
for i in range(len(tokens)):
if tokens[i] in twitter_most_frequent_words:
continue
else:
bit_block = token2bits[tokens[i].lower()]
encoded_ls.append(bit_block)
bitstring = "".join(encoded_ls)
return bitstring
def common_token_adder(bits2tokens, common_tokens):
"""
Args:
bits2tokens: dictionary {bits_block: [tokens]}
common_tokens: a list of the most frequently used tokens
Return:
bits2tokens_common: a dictionary with each block been added the common_tokens
"""
bits2tokens_common = {}
for bits, tokens in bits2tokens.items():
bits2tokens_common[bits] = tokens+common_tokens
return bits2tokens_common
if __name__ == '__main__':
vocabulary = ['chocolate', 'love', 'apple', 'blueberry', 'muffin', 'yogurt', 'banana', 'cheesecake', 'ice', 'cream']
p = np.array([1.0/len(vocabulary) for _ in range(len(vocabulary))])
test_bits = "000010111"
N_bits = 3
bits2tokens, token2bits = bitblock_to_tokens(vocabulary, N_bits)
# print("-"*30+"dictionaries: "+"-"*30)
# print(bits2tokens)
# print(token2bits)
# bits2tokens_common = common_token_adder(bits2tokens, twitter_most_frequent_words)
stega_text = encode(bits2tokens, N_bits, test_bits, p)
print("-"*30+"stega_text"+"-"*30)
print(stega_text)
print("-"*64)
origin_bits = decode(token2bits, N_bits, stega_text)
print("-"*30+"origin bits"+"-"*30)
print(origin_bits)
print("-"*71)