Spaces:
Sleeping
Sleeping
File size: 10,106 Bytes
05d3571 |
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
#!/usr/bin/python
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
#
# LASER Language-Agnostic SEntence Representations
# is a toolkit to calculate multilingual sentence embeddings
# and to use them for document classification, bitext filtering
# and mining
#
# --------------------------------------------------------
#
# Python tool to search for paraphrases in FAISS index
import re
import sys
import os.path
import tempfile
import argparse
import faiss
import time
import pdb
import numpy as np
from collections import namedtuple
# get environment
assert os.environ.get('LASER'), 'Please set the enviornment variable LASER'
LASER = os.environ['LASER']
sys.path.append(LASER + '/source/lib')
from indexing import IndexLoad, IndexTextOpen, IndexTextQuery, SplitOpen, SplitAccess
from embed import SentenceEncoder, EncodeLoad, EncodeFile, EncodeTime
from text_processing import Token, BPEfastApply
SPACE_NORMALIZER = re.compile("\s+")
Batch = namedtuple('Batch', 'srcs tokens lengths')
# calculate L2 distance between [x]
# and the vectors referenced in idxs
# x should be already normalized
def IndexDistL2(X, E, D, I, thresh=1.0, dtype=np.float32, sort=True):
nb, nK = I.shape
dim = X.shape[1]
dist_l2 = np.empty((nb, nK), dtype=np.float32)
y = np.empty((1, dim), dtype=dtype)
for i in range(nb):
for k in range(nK):
if D[i, k] <= thresh:
# get embedding from disk
np.copyto(y, SplitAccess(E, I[i, k]))
faiss.normalize_L2(y)
dist_l2[i, k] = 1.0 - np.dot(X[i], y[0])
else:
# exclude sentences which already have a huge FAISS distance
# (getting embeddings from disk is very time consumming)
dist_l2[i, k] = 1.0
if sort:
# re-sort according to L2
idxs = np.argsort(dist_l2[i], axis=0)
dist_l2[i] = dist_l2[i][idxs]
I[i] = I[i][idxs]
return dist_l2, I
###############################################################################
#
# Apply an absolute threshold on the distance
#
###############################################################################
def MarginAbs(em, ofp, params, args, stats):
D, I = params.idx.search(em, args.kmax)
thresh = args.threshold_faiss
if args.embed:
D, I = IndexDistL2(em, params.E, D, I, args.threshold_faiss)
thresh = args.threshold_L2
for n in range(D.shape[0]):
prev = {} # for deduplication
for i in range(args.kmax):
txt = IndexTextQuery(params.T, params.R, I[n, i])
if (args.dedup and txt not in prev) and D[n, i] <= thresh:
prev[txt] = 1
ofp.write('{:d}\t{:7.5f}\t{}\n'
.format(stats.nbs, D[n, i], txt))
stats.nbp += 1
# display source sentece if requested
if (args.include_source == 'matches' and len(prev) > 0):
ofp.write('{:d}\t{:6.1f}\t{}\n'
.format(stats.nbs, 0.0, sentences[n].replace('@@ ', '')))
if args.include_source == 'always':
ofp.write('{:d}\t{:6.1f}\t{}\n'
.format(stats.nbs, 0.0, sentences[n].replace('@@ ', '')))
stats.nbs += 1
###############################################################################
#
# Apply an threshold on the ratio between distance and average
#
###############################################################################
def MarginRatio(em, ofp, params, args, stats):
D, I = params.idx.search(em, args.margin_k)
thresh = args.threshold
if args.embed:
D, I = IndexDistL2(em, params.E, D, I, args.threshold_faiss)
thresh = args.threshold_L2
Mean = D.mean(axis=1)
for n in range(D.shape[0]):
if D[n, 0] / Mean[n] <= args.threshold:
if args.include_source == 'matches':
ofp.write('{:d}\t{:6.1f}\t{}\n'
.format(stats.nbs, 0.0, sentences[n].replace('@@ ', '')))
txt = IndexTextQuery(params.T, params.R, I[n, 0])
ofp.write('{:d}\t{:7.5f}\t{}\n'.format(stats.nbs, D[n, 0], txt))
stats.nbp += 1
stats.nbs += 1
if args.include_source == 'always':
ofp.write('{:d}\t{:6.1f}\t{}\n'
.format(stats.nbs, 0.0, sentences[n].replace('@@ ', '')))
###############################################################################
def MarginDist(em, ofp, params, args, stats):
print('ERROR: MarginAbs not implemented')
sys.exit(1)
###############################################################################
def buffered_read(fp, buffer_size):
buffer = []
for src_str in fp:
buffer.append(src_str.strip())
if len(buffer) >= buffer_size:
yield buffer
buffer = []
if len(buffer) > 0:
yield buffer
###############################################################################
parser = argparse.ArgumentParser('LASER: paraphrase tool')
parser.add_argument('--encoder', type=str, required=True,
help='encoder to be used')
parser.add_argument('--encoding', default='utf-8',
help='Character encoding for input/output')
parser.add_argument('--token-lang', type=str, default='--',
help="Language of tokenizer ('--' for no tokenization)")
parser.add_argument('--bpe-codes', type=str, default=None, required=True,
help='BPE codes')
parser.add_argument('--buffer-size', type=int, default=100,
help='Buffer size (sentences)')
parser.add_argument('--max-tokens', type=int, default=12000,
help='Maximum number of tokens to process in a batch')
parser.add_argument('--max-sentences', type=int, default=None,
help='Maximum number of sentences to process in a batch')
parser.add_argument('--cpu', action='store_true',
help='Use CPU instead of GPU')
parser.add_argument('--index', type=str, required=True,
help='FAISS index')
parser.add_argument('--nprobe', type=int, default=128,
help='FAISS: value of nprobe')
parser.add_argument('--text', type=str, required=True,
help='File with indexed texts')
parser.add_argument(
'--dim', type=int, default=1024,
help='Dimension of specified sentence embeddings')
parser.add_argument(
'--embed', type=str, default=None,
help='Sentence embeddings, true L2 distance will be calculated when specified')
parser.add_argument('-i', '--input', type=str, required=True,
help='Input text file')
parser.add_argument('-p', '--output', type=str, default='--',
help='Output paraphrases')
parser.add_argument('--kmax', type=int, default=10,
help='Max value of distance or margin of each paraphrase')
parser.add_argument('--dedup', type=int, default=1,
help='Deduplicate list of paraphrases')
parser.add_argument('--include-source', default='never',
choices=['never', 'matches', 'always'],
help='Include source sentence in the list of paraphrases')
parser.add_argument('--margin',
choices=['absolute', 'distance', 'ratio'],
default='ratio', help='Margin function')
parser.add_argument('-T', '--threshold-margin', type=float, default=0.9,
help='Threshold on margin')
parser.add_argument('--threshold-faiss', type=float, default=0.4,
help='Threshold on FAISS distance')
parser.add_argument('--threshold-L2', type=float, default=0.2,
help='Threshold on L2 distance')
parser.add_argument('--margin-k', type=int, default=4,
help='Number of nearest neighbors for margin calculation')
parser.add_argument('--verbose', action='store_true',
help='Detailed output')
print('\nLASER: paraphrase tool')
args = parser.parse_args()
# index,
# memory mapped texts, references and word counts
# encoder
params = namedtuple('params', 'idx T R W M E enc')
# open text and reference file
params.T, params.R, params.W, params.M = IndexTextOpen(args.text)
# Open on-disk embeddings for L2 distances
if args.embed:
params.E = SplitOpen(args.embed, ['en'],
args.dim, np.float32, verbose=False)
# load FAISS index
params.idx = IndexLoad(args.index, args.nprobe)
# load sentence encoder
params.enc = EncodeLoad(args)
margin_methods = {'absolute': MarginAbs,
'distance': MarginDist,
'ratio': MarginRatio}
with tempfile.TemporaryDirectory() as tmpdir:
ifile = args.input
if args.token_lang != '--':
ifile = os.path.join(tmpdir, 'tok')
Token(args.input,
ifile,
lang=args.token_lang,
romanize=True if args.token_lang == 'el' else False,
lower_case=True, gzip=False,
verbose=args.verbose, over_write=False)
if args.bpe_codes:
bpe_file = os.path.join(tmpdir, 'bpe')
BPEfastApply(ifile,
bpe_file,
args.bpe_codes,
verbose=args.verbose, over_write=False)
ifile = bpe_file
print(' - processing (batch size is {:d})'.format(args.buffer_size))
ifp = open(ifile, 'r', encoding=args.encoding, errors='surrogateescape')
if args.output == '--':
ofp = sys.stdout
else:
ofp = open(args.output, 'w', encoding=args.encoding, errors='surrogateescape')
stats = namedtuple('stats', 'ns np')
stats.nbs = 0
stats.nbp = 0
t = time.time()
for sentences in buffered_read(ifp, args.buffer_size):
embed = params.enc.encode_sentences(sentences)
faiss.normalize_L2(embed)
# call function for selected margin method
margin_methods.get(args.margin)(embed, ofp, params, args, stats)
if stats.nbs % 1000 == 0:
print('\r - {:d} sentences {:d} paraphrases'
.format(stats.nbs, stats.nbp), end='')
ifp.close()
if args.output != '--':
ofp.close()
print('\r - {:d} sentences {:d} paraphrases'
.format(stats.nbs, stats.nbp), end='')
EncodeTime(t)
|