Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/ops/gen_string_ops.py: 10%

1397 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

1"""Python wrappers around TensorFlow ops. 

2 

3This file is MACHINE GENERATED! Do not edit. 

4""" 

5 

6import collections 

7 

8from tensorflow.python import pywrap_tfe as pywrap_tfe 

9from tensorflow.python.eager import context as _context 

10from tensorflow.python.eager import core as _core 

11from tensorflow.python.eager import execute as _execute 

12from tensorflow.python.framework import dtypes as _dtypes 

13from tensorflow.security.fuzzing.py import annotation_types as _atypes 

14 

15from tensorflow.python.framework import op_def_registry as _op_def_registry 

16from tensorflow.python.framework import ops as _ops 

17from tensorflow.python.framework import op_def_library as _op_def_library 

18from tensorflow.python.util.deprecation import deprecated_endpoints 

19from tensorflow.python.util import dispatch as _dispatch 

20from tensorflow.python.util.tf_export import tf_export 

21 

22from typing import TypeVar 

23 

24@_dispatch.add_fallback_dispatch_list 

25@_dispatch.add_type_based_api_dispatcher 

26@tf_export('strings.as_string', 'as_string', v1=['dtypes.as_string', 'strings.as_string', 'as_string']) 

27@deprecated_endpoints('dtypes.as_string') 

28def as_string(input, precision=-1, scientific=False, shortest=False, width=-1, fill="", name=None): 

29 r"""Converts each entry in the given tensor to strings. 

30 

31 Supports many numeric types and boolean. 

32 

33 For Unicode, see the 

34 [https://www.tensorflow.org/tutorials/representation/unicode](Working with Unicode text) 

35 tutorial. 

36 

37 Examples: 

38 

39 >>> tf.strings.as_string([3, 2]) 

40 <tf.Tensor: shape=(2,), dtype=string, numpy=array([b'3', b'2'], dtype=object)> 

41 >>> tf.strings.as_string([3.1415926, 2.71828], precision=2).numpy() 

42 array([b'3.14', b'2.72'], dtype=object) 

43 

44 Args: 

45 input: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `uint8`, `int16`, `int8`, `int64`, `bfloat16`, `uint16`, `half`, `uint32`, `uint64`, `complex64`, `complex128`, `bool`, `variant`, `string`. 

46 precision: An optional `int`. Defaults to `-1`. 

47 The post-decimal precision to use for floating point numbers. 

48 Only used if precision > -1. 

49 scientific: An optional `bool`. Defaults to `False`. 

50 Use scientific notation for floating point numbers. 

51 shortest: An optional `bool`. Defaults to `False`. 

52 Use shortest representation (either scientific or standard) for 

53 floating point numbers. 

54 width: An optional `int`. Defaults to `-1`. 

55 Pad pre-decimal numbers to this width. 

56 Applies to both floating point and integer numbers. 

57 Only used if width > -1. 

58 fill: An optional `string`. Defaults to `""`. 

59 The value to pad if width > -1. If empty, pads with spaces. 

60 Another typical value is '0'. String cannot be longer than 1 character. 

61 name: A name for the operation (optional). 

62 

63 Returns: 

64 A `Tensor` of type `string`. 

65 """ 

66 _ctx = _context._context or _context.context() 

67 tld = _ctx._thread_local_data 

68 if tld.is_eager: 

69 try: 

70 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

71 _ctx, "AsString", name, input, "precision", precision, "scientific", 

72 scientific, "shortest", shortest, "width", width, "fill", fill) 

73 return _result 

74 except _core._NotOkStatusException as e: 

75 _ops.raise_from_not_ok_status(e, name) 

76 except _core._FallbackException: 

77 pass 

78 try: 

79 _result = _dispatcher_for_as_string( 

80 (input, precision, scientific, shortest, width, fill, name,), None) 

81 if _result is not NotImplemented: 

82 return _result 

83 return as_string_eager_fallback( 

84 input, precision=precision, scientific=scientific, 

85 shortest=shortest, width=width, fill=fill, name=name, ctx=_ctx) 

86 except _core._SymbolicException: 

87 pass # Add nodes to the TensorFlow graph. 

88 except (TypeError, ValueError): 

89 _result = _dispatch.dispatch( 

90 as_string, (), dict(input=input, precision=precision, 

91 scientific=scientific, shortest=shortest, 

92 width=width, fill=fill, name=name) 

93 ) 

94 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

95 return _result 

96 raise 

97 else: 

98 _result = _dispatcher_for_as_string( 

99 (input, precision, scientific, shortest, width, fill, name,), None) 

100 if _result is not NotImplemented: 

101 return _result 

102 # Add nodes to the TensorFlow graph. 

103 if precision is None: 

104 precision = -1 

105 precision = _execute.make_int(precision, "precision") 

106 if scientific is None: 

107 scientific = False 

108 scientific = _execute.make_bool(scientific, "scientific") 

109 if shortest is None: 

110 shortest = False 

111 shortest = _execute.make_bool(shortest, "shortest") 

112 if width is None: 

113 width = -1 

114 width = _execute.make_int(width, "width") 

115 if fill is None: 

116 fill = "" 

117 fill = _execute.make_str(fill, "fill") 

118 try: 

119 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

120 "AsString", input=input, precision=precision, scientific=scientific, 

121 shortest=shortest, width=width, fill=fill, name=name) 

122 except (TypeError, ValueError): 

123 _result = _dispatch.dispatch( 

124 as_string, (), dict(input=input, precision=precision, 

125 scientific=scientific, shortest=shortest, 

126 width=width, fill=fill, name=name) 

127 ) 

128 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

129 return _result 

130 raise 

131 _result = _outputs[:] 

132 if _execute.must_record_gradient(): 

133 _attrs = ("T", _op._get_attr_type("T"), "precision", 

134 _op._get_attr_int("precision"), "scientific", 

135 _op._get_attr_bool("scientific"), "shortest", 

136 _op._get_attr_bool("shortest"), "width", 

137 _op._get_attr_int("width"), "fill", _op.get_attr("fill")) 

138 _inputs_flat = _op.inputs 

139 _execute.record_gradient( 

140 "AsString", _inputs_flat, _attrs, _result) 

141 _result, = _result 

142 return _result 

143 

144AsString = tf_export("raw_ops.AsString")(_ops.to_raw_op(as_string)) 

145_dispatcher_for_as_string = as_string._tf_type_based_dispatcher.Dispatch 

146 

147 

148def as_string_eager_fallback(input, precision, scientific, shortest, width, fill, name, ctx): 

149 if precision is None: 

150 precision = -1 

151 precision = _execute.make_int(precision, "precision") 

152 if scientific is None: 

153 scientific = False 

154 scientific = _execute.make_bool(scientific, "scientific") 

155 if shortest is None: 

156 shortest = False 

157 shortest = _execute.make_bool(shortest, "shortest") 

158 if width is None: 

159 width = -1 

160 width = _execute.make_int(width, "width") 

161 if fill is None: 

162 fill = "" 

163 fill = _execute.make_str(fill, "fill") 

164 _attr_T, (input,) = _execute.args_to_matching_eager([input], ctx, [_dtypes.float32, _dtypes.float64, _dtypes.int32, _dtypes.uint8, _dtypes.int16, _dtypes.int8, _dtypes.int64, _dtypes.bfloat16, _dtypes.uint16, _dtypes.half, _dtypes.uint32, _dtypes.uint64, _dtypes.complex64, _dtypes.complex128, _dtypes.bool, _dtypes.variant, _dtypes.string, ]) 

165 _inputs_flat = [input] 

166 _attrs = ("T", _attr_T, "precision", precision, "scientific", scientific, 

167 "shortest", shortest, "width", width, "fill", fill) 

168 _result = _execute.execute(b"AsString", 1, inputs=_inputs_flat, 

169 attrs=_attrs, ctx=ctx, name=name) 

170 if _execute.must_record_gradient(): 

171 _execute.record_gradient( 

172 "AsString", _inputs_flat, _attrs, _result) 

173 _result, = _result 

174 return _result 

175 

176 

177@_dispatch.add_fallback_dispatch_list 

178@_dispatch.add_type_based_api_dispatcher 

179@tf_export('io.decode_base64', v1=['io.decode_base64', 'decode_base64']) 

180@deprecated_endpoints('decode_base64') 

181def decode_base64(input, name=None): 

182 r"""Decode web-safe base64-encoded strings. 

183 

184 Input may or may not have padding at the end. See 

185 [EncodeBase64](https://www.tensorflow.org/api_docs/python/tf/io/encode_base64) 

186 for padding. Web-safe means that input must use - and _ instead of + and /. 

187 

188 Args: 

189 input: A `Tensor` of type `string`. Base64 strings to decode. 

190 name: A name for the operation (optional). 

191 

192 Returns: 

193 A `Tensor` of type `string`. 

194 """ 

195 _ctx = _context._context or _context.context() 

196 tld = _ctx._thread_local_data 

197 if tld.is_eager: 

198 try: 

199 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

200 _ctx, "DecodeBase64", name, input) 

201 return _result 

202 except _core._NotOkStatusException as e: 

203 _ops.raise_from_not_ok_status(e, name) 

204 except _core._FallbackException: 

205 pass 

206 try: 

207 _result = _dispatcher_for_decode_base64( 

208 (input, name,), None) 

209 if _result is not NotImplemented: 

210 return _result 

211 return decode_base64_eager_fallback( 

212 input, name=name, ctx=_ctx) 

213 except _core._SymbolicException: 

214 pass # Add nodes to the TensorFlow graph. 

215 except (TypeError, ValueError): 

216 _result = _dispatch.dispatch( 

217 decode_base64, (), dict(input=input, name=name) 

218 ) 

219 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

220 return _result 

221 raise 

222 else: 

223 _result = _dispatcher_for_decode_base64( 

224 (input, name,), None) 

225 if _result is not NotImplemented: 

226 return _result 

227 # Add nodes to the TensorFlow graph. 

228 try: 

229 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

230 "DecodeBase64", input=input, name=name) 

231 except (TypeError, ValueError): 

232 _result = _dispatch.dispatch( 

233 decode_base64, (), dict(input=input, name=name) 

234 ) 

235 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

236 return _result 

237 raise 

238 _result = _outputs[:] 

239 if _execute.must_record_gradient(): 

240 _attrs = () 

241 _inputs_flat = _op.inputs 

242 _execute.record_gradient( 

243 "DecodeBase64", _inputs_flat, _attrs, _result) 

244 _result, = _result 

245 return _result 

246 

247DecodeBase64 = tf_export("raw_ops.DecodeBase64")(_ops.to_raw_op(decode_base64)) 

248_dispatcher_for_decode_base64 = decode_base64._tf_type_based_dispatcher.Dispatch 

249 

250 

251def decode_base64_eager_fallback(input, name, ctx): 

252 input = _ops.convert_to_tensor(input, _dtypes.string) 

253 _inputs_flat = [input] 

254 _attrs = None 

255 _result = _execute.execute(b"DecodeBase64", 1, inputs=_inputs_flat, 

256 attrs=_attrs, ctx=ctx, name=name) 

257 if _execute.must_record_gradient(): 

258 _execute.record_gradient( 

259 "DecodeBase64", _inputs_flat, _attrs, _result) 

260 _result, = _result 

261 return _result 

262 

263 

264@_dispatch.add_fallback_dispatch_list 

265@_dispatch.add_type_based_api_dispatcher 

266@tf_export('io.encode_base64', v1=['io.encode_base64', 'encode_base64']) 

267@deprecated_endpoints('encode_base64') 

268def encode_base64(input, pad=False, name=None): 

269 r"""Encode strings into web-safe base64 format. 

270 

271 Refer to [this article](https://en.wikipedia.org/wiki/Base64) for more information on 

272 base64 format. Base64 strings may have padding with '=' at the 

273 end so that the encoded has length multiple of 4. See Padding section of the 

274 link above. 

275 

276 Web-safe means that the encoder uses - and _ instead of + and /. 

277 

278 Args: 

279 input: A `Tensor` of type `string`. Strings to be encoded. 

280 pad: An optional `bool`. Defaults to `False`. 

281 Bool whether padding is applied at the ends. 

282 name: A name for the operation (optional). 

283 

284 Returns: 

285 A `Tensor` of type `string`. 

286 """ 

287 _ctx = _context._context or _context.context() 

288 tld = _ctx._thread_local_data 

289 if tld.is_eager: 

290 try: 

291 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

292 _ctx, "EncodeBase64", name, input, "pad", pad) 

293 return _result 

294 except _core._NotOkStatusException as e: 

295 _ops.raise_from_not_ok_status(e, name) 

296 except _core._FallbackException: 

297 pass 

298 try: 

299 _result = _dispatcher_for_encode_base64( 

300 (input, pad, name,), None) 

301 if _result is not NotImplemented: 

302 return _result 

303 return encode_base64_eager_fallback( 

304 input, pad=pad, name=name, ctx=_ctx) 

305 except _core._SymbolicException: 

306 pass # Add nodes to the TensorFlow graph. 

307 except (TypeError, ValueError): 

308 _result = _dispatch.dispatch( 

309 encode_base64, (), dict(input=input, pad=pad, name=name) 

310 ) 

311 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

312 return _result 

313 raise 

314 else: 

315 _result = _dispatcher_for_encode_base64( 

316 (input, pad, name,), None) 

317 if _result is not NotImplemented: 

318 return _result 

319 # Add nodes to the TensorFlow graph. 

320 if pad is None: 

321 pad = False 

322 pad = _execute.make_bool(pad, "pad") 

323 try: 

324 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

325 "EncodeBase64", input=input, pad=pad, name=name) 

326 except (TypeError, ValueError): 

327 _result = _dispatch.dispatch( 

328 encode_base64, (), dict(input=input, pad=pad, name=name) 

329 ) 

330 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

331 return _result 

332 raise 

333 _result = _outputs[:] 

334 if _execute.must_record_gradient(): 

335 _attrs = ("pad", _op._get_attr_bool("pad")) 

336 _inputs_flat = _op.inputs 

337 _execute.record_gradient( 

338 "EncodeBase64", _inputs_flat, _attrs, _result) 

339 _result, = _result 

340 return _result 

341 

342EncodeBase64 = tf_export("raw_ops.EncodeBase64")(_ops.to_raw_op(encode_base64)) 

343_dispatcher_for_encode_base64 = encode_base64._tf_type_based_dispatcher.Dispatch 

344 

345 

346def encode_base64_eager_fallback(input, pad, name, ctx): 

347 if pad is None: 

348 pad = False 

349 pad = _execute.make_bool(pad, "pad") 

350 input = _ops.convert_to_tensor(input, _dtypes.string) 

351 _inputs_flat = [input] 

352 _attrs = ("pad", pad) 

353 _result = _execute.execute(b"EncodeBase64", 1, inputs=_inputs_flat, 

354 attrs=_attrs, ctx=ctx, name=name) 

355 if _execute.must_record_gradient(): 

356 _execute.record_gradient( 

357 "EncodeBase64", _inputs_flat, _attrs, _result) 

358 _result, = _result 

359 return _result 

360 

361 

362def reduce_join(inputs, reduction_indices, keep_dims=False, separator="", name=None): 

363 r"""Joins a string Tensor across the given dimensions. 

364 

365 Computes the string join across dimensions in the given string Tensor of shape 

366 `[\\(d_0, d_1, ..., d_{n-1}\\)]`. Returns a new Tensor created by joining the input 

367 strings with the given separator (default: empty string). Negative indices are 

368 counted backwards from the end, with `-1` being equivalent to `n - 1`. If 

369 indices are not specified, joins across all dimensions beginning from `n - 1` 

370 through `0`. 

371 

372 For example: 

373 

374 ```python 

375 # tensor `a` is [["a", "b"], ["c", "d"]] 

376 tf.reduce_join(a, 0) ==> ["ac", "bd"] 

377 tf.reduce_join(a, 1) ==> ["ab", "cd"] 

378 tf.reduce_join(a, -2) = tf.reduce_join(a, 0) ==> ["ac", "bd"] 

379 tf.reduce_join(a, -1) = tf.reduce_join(a, 1) ==> ["ab", "cd"] 

380 tf.reduce_join(a, 0, keep_dims=True) ==> [["ac", "bd"]] 

381 tf.reduce_join(a, 1, keep_dims=True) ==> [["ab"], ["cd"]] 

382 tf.reduce_join(a, 0, separator=".") ==> ["a.c", "b.d"] 

383 tf.reduce_join(a, [0, 1]) ==> "acbd" 

384 tf.reduce_join(a, [1, 0]) ==> "abcd" 

385 tf.reduce_join(a, []) ==> [["a", "b"], ["c", "d"]] 

386 tf.reduce_join(a) = tf.reduce_join(a, [1, 0]) ==> "abcd" 

387 ``` 

388 

389 Args: 

390 inputs: A `Tensor` of type `string`. 

391 The input to be joined. All reduced indices must have non-zero size. 

392 reduction_indices: A `Tensor` of type `int32`. 

393 The dimensions to reduce over. Dimensions are reduced in the 

394 order specified. Omitting `reduction_indices` is equivalent to passing 

395 `[n-1, n-2, ..., 0]`. Negative indices from `-n` to `-1` are supported. 

396 keep_dims: An optional `bool`. Defaults to `False`. 

397 If `True`, retain reduced dimensions with length `1`. 

398 separator: An optional `string`. Defaults to `""`. 

399 The separator to use when joining. 

400 name: A name for the operation (optional). 

401 

402 Returns: 

403 A `Tensor` of type `string`. 

404 """ 

405 _ctx = _context._context or _context.context() 

406 tld = _ctx._thread_local_data 

407 if tld.is_eager: 

408 try: 

409 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

410 _ctx, "ReduceJoin", name, inputs, reduction_indices, "keep_dims", 

411 keep_dims, "separator", separator) 

412 return _result 

413 except _core._NotOkStatusException as e: 

414 _ops.raise_from_not_ok_status(e, name) 

415 except _core._FallbackException: 

416 pass 

417 try: 

418 return reduce_join_eager_fallback( 

419 inputs, reduction_indices, keep_dims=keep_dims, separator=separator, 

420 name=name, ctx=_ctx) 

421 except _core._SymbolicException: 

422 pass # Add nodes to the TensorFlow graph. 

423 # Add nodes to the TensorFlow graph. 

424 if keep_dims is None: 

425 keep_dims = False 

426 keep_dims = _execute.make_bool(keep_dims, "keep_dims") 

427 if separator is None: 

428 separator = "" 

429 separator = _execute.make_str(separator, "separator") 

430 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

431 "ReduceJoin", inputs=inputs, reduction_indices=reduction_indices, 

432 keep_dims=keep_dims, separator=separator, name=name) 

433 _result = _outputs[:] 

434 if _execute.must_record_gradient(): 

435 _attrs = ("keep_dims", _op._get_attr_bool("keep_dims"), "separator", 

436 _op.get_attr("separator")) 

437 _inputs_flat = _op.inputs 

438 _execute.record_gradient( 

439 "ReduceJoin", _inputs_flat, _attrs, _result) 

440 _result, = _result 

441 return _result 

442 

443ReduceJoin = tf_export("raw_ops.ReduceJoin")(_ops.to_raw_op(reduce_join)) 

444 

445 

446def reduce_join_eager_fallback(inputs, reduction_indices, keep_dims, separator, name, ctx): 

447 if keep_dims is None: 

448 keep_dims = False 

449 keep_dims = _execute.make_bool(keep_dims, "keep_dims") 

450 if separator is None: 

451 separator = "" 

452 separator = _execute.make_str(separator, "separator") 

453 inputs = _ops.convert_to_tensor(inputs, _dtypes.string) 

454 reduction_indices = _ops.convert_to_tensor(reduction_indices, _dtypes.int32) 

455 _inputs_flat = [inputs, reduction_indices] 

456 _attrs = ("keep_dims", keep_dims, "separator", separator) 

457 _result = _execute.execute(b"ReduceJoin", 1, inputs=_inputs_flat, 

458 attrs=_attrs, ctx=ctx, name=name) 

459 if _execute.must_record_gradient(): 

460 _execute.record_gradient( 

461 "ReduceJoin", _inputs_flat, _attrs, _result) 

462 _result, = _result 

463 return _result 

464 

465 

466def regex_full_match(input, pattern, name=None): 

467 r"""Check if the input matches the regex pattern. 

468 

469 The input is a string tensor of any shape. The pattern is a scalar 

470 string tensor which is applied to every element of the input tensor. 

471 The boolean values (True or False) of the output tensor indicate 

472 if the input matches the regex pattern provided. 

473 

474 The pattern follows the re2 syntax (https://github.com/google/re2/wiki/Syntax) 

475 

476 Examples: 

477 

478 >>> tf.strings.regex_full_match(["TF lib", "lib TF"], ".*lib$") 

479 <tf.Tensor: shape=(2,), dtype=bool, numpy=array([ True, False])> 

480 >>> tf.strings.regex_full_match(["TF lib", "lib TF"], ".*TF$") 

481 <tf.Tensor: shape=(2,), dtype=bool, numpy=array([False, True])> 

482 

483 Args: 

484 input: A `Tensor` of type `string`. 

485 A string tensor of the text to be processed. 

486 pattern: A `Tensor` of type `string`. 

487 A scalar string tensor containing the regular expression to match the input. 

488 name: A name for the operation (optional). 

489 

490 Returns: 

491 A `Tensor` of type `bool`. 

492 """ 

493 _ctx = _context._context or _context.context() 

494 tld = _ctx._thread_local_data 

495 if tld.is_eager: 

496 try: 

497 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

498 _ctx, "RegexFullMatch", name, input, pattern) 

499 return _result 

500 except _core._NotOkStatusException as e: 

501 _ops.raise_from_not_ok_status(e, name) 

502 except _core._FallbackException: 

503 pass 

504 try: 

505 return regex_full_match_eager_fallback( 

506 input, pattern, name=name, ctx=_ctx) 

507 except _core._SymbolicException: 

508 pass # Add nodes to the TensorFlow graph. 

509 # Add nodes to the TensorFlow graph. 

510 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

511 "RegexFullMatch", input=input, pattern=pattern, name=name) 

512 _result = _outputs[:] 

513 if _execute.must_record_gradient(): 

514 _attrs = () 

515 _inputs_flat = _op.inputs 

516 _execute.record_gradient( 

517 "RegexFullMatch", _inputs_flat, _attrs, _result) 

518 _result, = _result 

519 return _result 

520 

521RegexFullMatch = tf_export("raw_ops.RegexFullMatch")(_ops.to_raw_op(regex_full_match)) 

522 

523 

524def regex_full_match_eager_fallback(input, pattern, name, ctx): 

525 input = _ops.convert_to_tensor(input, _dtypes.string) 

526 pattern = _ops.convert_to_tensor(pattern, _dtypes.string) 

527 _inputs_flat = [input, pattern] 

528 _attrs = None 

529 _result = _execute.execute(b"RegexFullMatch", 1, inputs=_inputs_flat, 

530 attrs=_attrs, ctx=ctx, name=name) 

531 if _execute.must_record_gradient(): 

532 _execute.record_gradient( 

533 "RegexFullMatch", _inputs_flat, _attrs, _result) 

534 _result, = _result 

535 return _result 

536 

537 

538def regex_replace(input, pattern, rewrite, replace_global=True, name=None): 

539 r"""Replaces matches of the `pattern` regular expression in `input` with the 

540replacement string provided in `rewrite`. 

541 

542 It follows the re2 syntax (https://github.com/google/re2/wiki/Syntax) 

543 

544 Args: 

545 input: A `Tensor` of type `string`. The text to be processed. 

546 pattern: A `Tensor` of type `string`. 

547 The regular expression to be matched in the `input` strings. 

548 rewrite: A `Tensor` of type `string`. 

549 The rewrite string to be substituted for the `pattern` expression where it is 

550 matched in the `input` strings. 

551 replace_global: An optional `bool`. Defaults to `True`. 

552 If True, the replacement is global (that is, all matches of the `pattern` regular 

553 expression in each input string are rewritten), otherwise the `rewrite` 

554 substitution is only made for the first `pattern` match. 

555 name: A name for the operation (optional). 

556 

557 Returns: 

558 A `Tensor` of type `string`. 

559 """ 

560 _ctx = _context._context or _context.context() 

561 tld = _ctx._thread_local_data 

562 if tld.is_eager: 

563 try: 

564 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

565 _ctx, "RegexReplace", name, input, pattern, rewrite, "replace_global", 

566 replace_global) 

567 return _result 

568 except _core._NotOkStatusException as e: 

569 _ops.raise_from_not_ok_status(e, name) 

570 except _core._FallbackException: 

571 pass 

572 try: 

573 return regex_replace_eager_fallback( 

574 input, pattern, rewrite, replace_global=replace_global, name=name, 

575 ctx=_ctx) 

576 except _core._SymbolicException: 

577 pass # Add nodes to the TensorFlow graph. 

578 # Add nodes to the TensorFlow graph. 

579 if replace_global is None: 

580 replace_global = True 

581 replace_global = _execute.make_bool(replace_global, "replace_global") 

582 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

583 "RegexReplace", input=input, pattern=pattern, rewrite=rewrite, 

584 replace_global=replace_global, name=name) 

585 _result = _outputs[:] 

586 if _execute.must_record_gradient(): 

587 _attrs = ("replace_global", _op._get_attr_bool("replace_global")) 

588 _inputs_flat = _op.inputs 

589 _execute.record_gradient( 

590 "RegexReplace", _inputs_flat, _attrs, _result) 

591 _result, = _result 

592 return _result 

593 

594RegexReplace = tf_export("raw_ops.RegexReplace")(_ops.to_raw_op(regex_replace)) 

595 

596 

597def regex_replace_eager_fallback(input, pattern, rewrite, replace_global, name, ctx): 

598 if replace_global is None: 

599 replace_global = True 

600 replace_global = _execute.make_bool(replace_global, "replace_global") 

601 input = _ops.convert_to_tensor(input, _dtypes.string) 

602 pattern = _ops.convert_to_tensor(pattern, _dtypes.string) 

603 rewrite = _ops.convert_to_tensor(rewrite, _dtypes.string) 

604 _inputs_flat = [input, pattern, rewrite] 

605 _attrs = ("replace_global", replace_global) 

606 _result = _execute.execute(b"RegexReplace", 1, inputs=_inputs_flat, 

607 attrs=_attrs, ctx=ctx, name=name) 

608 if _execute.must_record_gradient(): 

609 _execute.record_gradient( 

610 "RegexReplace", _inputs_flat, _attrs, _result) 

611 _result, = _result 

612 return _result 

613 

614 

615def static_regex_full_match(input, pattern, name=None): 

616 r"""Check if the input matches the regex pattern. 

617 

618 The input is a string tensor of any shape. The pattern is the 

619 regular expression to be matched with every element of the input tensor. 

620 The boolean values (True or False) of the output tensor indicate 

621 if the input matches the regex pattern provided. 

622 

623 The pattern follows the re2 syntax (https://github.com/google/re2/wiki/Syntax) 

624 

625 Args: 

626 input: A `Tensor` of type `string`. 

627 A string tensor of the text to be processed. 

628 pattern: A `string`. The regular expression to match the input. 

629 name: A name for the operation (optional). 

630 

631 Returns: 

632 A `Tensor` of type `bool`. 

633 """ 

634 _ctx = _context._context or _context.context() 

635 tld = _ctx._thread_local_data 

636 if tld.is_eager: 

637 try: 

638 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

639 _ctx, "StaticRegexFullMatch", name, input, "pattern", pattern) 

640 return _result 

641 except _core._NotOkStatusException as e: 

642 _ops.raise_from_not_ok_status(e, name) 

643 except _core._FallbackException: 

644 pass 

645 try: 

646 return static_regex_full_match_eager_fallback( 

647 input, pattern=pattern, name=name, ctx=_ctx) 

648 except _core._SymbolicException: 

649 pass # Add nodes to the TensorFlow graph. 

650 # Add nodes to the TensorFlow graph. 

651 pattern = _execute.make_str(pattern, "pattern") 

652 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

653 "StaticRegexFullMatch", input=input, pattern=pattern, name=name) 

654 _result = _outputs[:] 

655 if _execute.must_record_gradient(): 

656 _attrs = ("pattern", _op.get_attr("pattern")) 

657 _inputs_flat = _op.inputs 

658 _execute.record_gradient( 

659 "StaticRegexFullMatch", _inputs_flat, _attrs, _result) 

660 _result, = _result 

661 return _result 

662 

663StaticRegexFullMatch = tf_export("raw_ops.StaticRegexFullMatch")(_ops.to_raw_op(static_regex_full_match)) 

664 

665 

666def static_regex_full_match_eager_fallback(input, pattern, name, ctx): 

667 pattern = _execute.make_str(pattern, "pattern") 

668 input = _ops.convert_to_tensor(input, _dtypes.string) 

669 _inputs_flat = [input] 

670 _attrs = ("pattern", pattern) 

671 _result = _execute.execute(b"StaticRegexFullMatch", 1, inputs=_inputs_flat, 

672 attrs=_attrs, ctx=ctx, name=name) 

673 if _execute.must_record_gradient(): 

674 _execute.record_gradient( 

675 "StaticRegexFullMatch", _inputs_flat, _attrs, _result) 

676 _result, = _result 

677 return _result 

678 

679 

680def static_regex_replace(input, pattern, rewrite, replace_global=True, name=None): 

681 r"""Replaces the match of pattern in input with rewrite. 

682 

683 It follows the re2 syntax (https://github.com/google/re2/wiki/Syntax) 

684 

685 Args: 

686 input: A `Tensor` of type `string`. The text to be processed. 

687 pattern: A `string`. The regular expression to match the input. 

688 rewrite: A `string`. The rewrite to be applied to the matched expression. 

689 replace_global: An optional `bool`. Defaults to `True`. 

690 If True, the replacement is global, otherwise the replacement 

691 is done only on the first match. 

692 name: A name for the operation (optional). 

693 

694 Returns: 

695 A `Tensor` of type `string`. 

696 """ 

697 _ctx = _context._context or _context.context() 

698 tld = _ctx._thread_local_data 

699 if tld.is_eager: 

700 try: 

701 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

702 _ctx, "StaticRegexReplace", name, input, "pattern", pattern, 

703 "rewrite", rewrite, "replace_global", replace_global) 

704 return _result 

705 except _core._NotOkStatusException as e: 

706 _ops.raise_from_not_ok_status(e, name) 

707 except _core._FallbackException: 

708 pass 

709 try: 

710 return static_regex_replace_eager_fallback( 

711 input, pattern=pattern, rewrite=rewrite, 

712 replace_global=replace_global, name=name, ctx=_ctx) 

713 except _core._SymbolicException: 

714 pass # Add nodes to the TensorFlow graph. 

715 # Add nodes to the TensorFlow graph. 

716 pattern = _execute.make_str(pattern, "pattern") 

717 rewrite = _execute.make_str(rewrite, "rewrite") 

718 if replace_global is None: 

719 replace_global = True 

720 replace_global = _execute.make_bool(replace_global, "replace_global") 

721 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

722 "StaticRegexReplace", input=input, pattern=pattern, rewrite=rewrite, 

723 replace_global=replace_global, name=name) 

724 _result = _outputs[:] 

725 if _execute.must_record_gradient(): 

726 _attrs = ("pattern", _op.get_attr("pattern"), "rewrite", 

727 _op.get_attr("rewrite"), "replace_global", 

728 _op._get_attr_bool("replace_global")) 

729 _inputs_flat = _op.inputs 

730 _execute.record_gradient( 

731 "StaticRegexReplace", _inputs_flat, _attrs, _result) 

732 _result, = _result 

733 return _result 

734 

735StaticRegexReplace = tf_export("raw_ops.StaticRegexReplace")(_ops.to_raw_op(static_regex_replace)) 

736 

737 

738def static_regex_replace_eager_fallback(input, pattern, rewrite, replace_global, name, ctx): 

739 pattern = _execute.make_str(pattern, "pattern") 

740 rewrite = _execute.make_str(rewrite, "rewrite") 

741 if replace_global is None: 

742 replace_global = True 

743 replace_global = _execute.make_bool(replace_global, "replace_global") 

744 input = _ops.convert_to_tensor(input, _dtypes.string) 

745 _inputs_flat = [input] 

746 _attrs = ("pattern", pattern, "rewrite", rewrite, "replace_global", 

747 replace_global) 

748 _result = _execute.execute(b"StaticRegexReplace", 1, inputs=_inputs_flat, 

749 attrs=_attrs, ctx=ctx, name=name) 

750 if _execute.must_record_gradient(): 

751 _execute.record_gradient( 

752 "StaticRegexReplace", _inputs_flat, _attrs, _result) 

753 _result, = _result 

754 return _result 

755 

756 

757def string_format(inputs, template="%s", placeholder="%s", summarize=3, name=None): 

758 r"""Formats a string template using a list of tensors. 

759 

760 Formats a string template using a list of tensors, pretty-printing tensor summaries. 

761 

762 Args: 

763 inputs: A list of `Tensor` objects. 

764 The list of tensors to format into the placeholder string. 

765 template: An optional `string`. Defaults to `"%s"`. 

766 A string, the template to format tensor summaries into. 

767 placeholder: An optional `string`. Defaults to `"%s"`. 

768 A string, at each placeholder in the template a subsequent tensor summary will be inserted. 

769 summarize: An optional `int`. Defaults to `3`. 

770 When formatting the tensor summaries print the first and last summarize entries of each tensor dimension. 

771 name: A name for the operation (optional). 

772 

773 Returns: 

774 A `Tensor` of type `string`. 

775 """ 

776 _ctx = _context._context or _context.context() 

777 tld = _ctx._thread_local_data 

778 if tld.is_eager: 

779 try: 

780 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

781 _ctx, "StringFormat", name, inputs, "template", template, 

782 "placeholder", placeholder, "summarize", summarize) 

783 return _result 

784 except _core._NotOkStatusException as e: 

785 _ops.raise_from_not_ok_status(e, name) 

786 except _core._FallbackException: 

787 pass 

788 try: 

789 return string_format_eager_fallback( 

790 inputs, template=template, placeholder=placeholder, 

791 summarize=summarize, name=name, ctx=_ctx) 

792 except _core._SymbolicException: 

793 pass # Add nodes to the TensorFlow graph. 

794 # Add nodes to the TensorFlow graph. 

795 if template is None: 

796 template = "%s" 

797 template = _execute.make_str(template, "template") 

798 if placeholder is None: 

799 placeholder = "%s" 

800 placeholder = _execute.make_str(placeholder, "placeholder") 

801 if summarize is None: 

802 summarize = 3 

803 summarize = _execute.make_int(summarize, "summarize") 

804 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

805 "StringFormat", inputs=inputs, template=template, 

806 placeholder=placeholder, summarize=summarize, 

807 name=name) 

808 _result = _outputs[:] 

809 if _execute.must_record_gradient(): 

810 _attrs = ("T", _op.get_attr("T"), "template", _op.get_attr("template"), 

811 "placeholder", _op.get_attr("placeholder"), "summarize", 

812 _op._get_attr_int("summarize")) 

813 _inputs_flat = _op.inputs 

814 _execute.record_gradient( 

815 "StringFormat", _inputs_flat, _attrs, _result) 

816 _result, = _result 

817 return _result 

818 

819StringFormat = tf_export("raw_ops.StringFormat")(_ops.to_raw_op(string_format)) 

820 

821 

822def string_format_eager_fallback(inputs, template, placeholder, summarize, name, ctx): 

823 if template is None: 

824 template = "%s" 

825 template = _execute.make_str(template, "template") 

826 if placeholder is None: 

827 placeholder = "%s" 

828 placeholder = _execute.make_str(placeholder, "placeholder") 

829 if summarize is None: 

830 summarize = 3 

831 summarize = _execute.make_int(summarize, "summarize") 

832 _attr_T, inputs = _execute.convert_to_mixed_eager_tensors(inputs, ctx) 

833 _inputs_flat = list(inputs) 

834 _attrs = ("T", _attr_T, "template", template, "placeholder", placeholder, 

835 "summarize", summarize) 

836 _result = _execute.execute(b"StringFormat", 1, inputs=_inputs_flat, 

837 attrs=_attrs, ctx=ctx, name=name) 

838 if _execute.must_record_gradient(): 

839 _execute.record_gradient( 

840 "StringFormat", _inputs_flat, _attrs, _result) 

841 _result, = _result 

842 return _result 

843 

844 

845def string_join(inputs, separator="", name=None): 

846 r"""Joins the strings in the given list of string tensors into one tensor; 

847 

848 with the given separator (default is an empty separator). 

849 

850 Examples: 

851 

852 >>> s = ["hello", "world", "tensorflow"] 

853 >>> tf.strings.join(s, " ") 

854 <tf.Tensor: shape=(), dtype=string, numpy=b'hello world tensorflow'> 

855 

856 Args: 

857 inputs: A list of `Tensor` objects with type `string`. 

858 A list of string tensors. The tensors must all have the same shape, 

859 or be scalars. Scalars may be mixed in; these will be broadcast to the shape 

860 of non-scalar inputs. 

861 separator: An optional `string`. Defaults to `""`. 

862 string, an optional join separator. 

863 name: A name for the operation (optional). 

864 

865 Returns: 

866 A `Tensor` of type `string`. 

867 """ 

868 _ctx = _context._context or _context.context() 

869 tld = _ctx._thread_local_data 

870 if tld.is_eager: 

871 try: 

872 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

873 _ctx, "StringJoin", name, inputs, "separator", separator) 

874 return _result 

875 except _core._NotOkStatusException as e: 

876 _ops.raise_from_not_ok_status(e, name) 

877 except _core._FallbackException: 

878 pass 

879 try: 

880 return string_join_eager_fallback( 

881 inputs, separator=separator, name=name, ctx=_ctx) 

882 except _core._SymbolicException: 

883 pass # Add nodes to the TensorFlow graph. 

884 # Add nodes to the TensorFlow graph. 

885 if not isinstance(inputs, (list, tuple)): 

886 raise TypeError( 

887 "Expected list for 'inputs' argument to " 

888 "'string_join' Op, not %r." % inputs) 

889 _attr_N = len(inputs) 

890 if separator is None: 

891 separator = "" 

892 separator = _execute.make_str(separator, "separator") 

893 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

894 "StringJoin", inputs=inputs, separator=separator, name=name) 

895 _result = _outputs[:] 

896 if _execute.must_record_gradient(): 

897 _attrs = ("N", _op._get_attr_int("N"), "separator", 

898 _op.get_attr("separator")) 

899 _inputs_flat = _op.inputs 

900 _execute.record_gradient( 

901 "StringJoin", _inputs_flat, _attrs, _result) 

902 _result, = _result 

903 return _result 

904 

905StringJoin = tf_export("raw_ops.StringJoin")(_ops.to_raw_op(string_join)) 

906 

907 

908def string_join_eager_fallback(inputs, separator, name, ctx): 

909 if not isinstance(inputs, (list, tuple)): 

910 raise TypeError( 

911 "Expected list for 'inputs' argument to " 

912 "'string_join' Op, not %r." % inputs) 

913 _attr_N = len(inputs) 

914 if separator is None: 

915 separator = "" 

916 separator = _execute.make_str(separator, "separator") 

917 inputs = _ops.convert_n_to_tensor(inputs, _dtypes.string) 

918 _inputs_flat = list(inputs) 

919 _attrs = ("N", _attr_N, "separator", separator) 

920 _result = _execute.execute(b"StringJoin", 1, inputs=_inputs_flat, 

921 attrs=_attrs, ctx=ctx, name=name) 

922 if _execute.must_record_gradient(): 

923 _execute.record_gradient( 

924 "StringJoin", _inputs_flat, _attrs, _result) 

925 _result, = _result 

926 return _result 

927 

928 

929def string_length(input, unit="BYTE", name=None): 

930 r"""String lengths of `input`. 

931 

932 Computes the length of each string given in the input tensor. 

933 

934 >>> strings = tf.constant(['Hello','TensorFlow', '\U0001F642']) 

935 >>> tf.strings.length(strings).numpy() # default counts bytes 

936 array([ 5, 10, 4], dtype=int32) 

937 >>> tf.strings.length(strings, unit="UTF8_CHAR").numpy() 

938 array([ 5, 10, 1], dtype=int32) 

939 

940 Args: 

941 input: A `Tensor` of type `string`. 

942 The strings for which to compute the length for each element. 

943 unit: An optional `string` from: `"BYTE", "UTF8_CHAR"`. Defaults to `"BYTE"`. 

944 The unit that is counted to compute string length. One of: `"BYTE"` (for 

945 the number of bytes in each string) or `"UTF8_CHAR"` (for the number of UTF-8 

946 encoded Unicode code points in each string). Results are undefined 

947 if `unit=UTF8_CHAR` and the `input` strings do not contain structurally 

948 valid UTF-8. 

949 name: A name for the operation (optional). 

950 

951 Returns: 

952 A `Tensor` of type `int32`. 

953 """ 

954 _ctx = _context._context or _context.context() 

955 tld = _ctx._thread_local_data 

956 if tld.is_eager: 

957 try: 

958 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

959 _ctx, "StringLength", name, input, "unit", unit) 

960 return _result 

961 except _core._NotOkStatusException as e: 

962 _ops.raise_from_not_ok_status(e, name) 

963 except _core._FallbackException: 

964 pass 

965 try: 

966 return string_length_eager_fallback( 

967 input, unit=unit, name=name, ctx=_ctx) 

968 except _core._SymbolicException: 

969 pass # Add nodes to the TensorFlow graph. 

970 # Add nodes to the TensorFlow graph. 

971 if unit is None: 

972 unit = "BYTE" 

973 unit = _execute.make_str(unit, "unit") 

974 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

975 "StringLength", input=input, unit=unit, name=name) 

976 _result = _outputs[:] 

977 if _execute.must_record_gradient(): 

978 _attrs = ("unit", _op.get_attr("unit")) 

979 _inputs_flat = _op.inputs 

980 _execute.record_gradient( 

981 "StringLength", _inputs_flat, _attrs, _result) 

982 _result, = _result 

983 return _result 

984 

985StringLength = tf_export("raw_ops.StringLength")(_ops.to_raw_op(string_length)) 

986 

987 

988def string_length_eager_fallback(input, unit, name, ctx): 

989 if unit is None: 

990 unit = "BYTE" 

991 unit = _execute.make_str(unit, "unit") 

992 input = _ops.convert_to_tensor(input, _dtypes.string) 

993 _inputs_flat = [input] 

994 _attrs = ("unit", unit) 

995 _result = _execute.execute(b"StringLength", 1, inputs=_inputs_flat, 

996 attrs=_attrs, ctx=ctx, name=name) 

997 if _execute.must_record_gradient(): 

998 _execute.record_gradient( 

999 "StringLength", _inputs_flat, _attrs, _result) 

1000 _result, = _result 

1001 return _result 

1002 

1003 

1004@_dispatch.add_fallback_dispatch_list 

1005@_dispatch.add_type_based_api_dispatcher 

1006@tf_export('strings.lower') 

1007def string_lower(input, encoding="", name=None): 

1008 r"""Converts all uppercase characters into their respective lowercase replacements. 

1009 

1010 Example: 

1011 

1012 >>> tf.strings.lower("CamelCase string and ALL CAPS") 

1013 <tf.Tensor: shape=(), dtype=string, numpy=b'camelcase string and all caps'> 

1014 

1015 Args: 

1016 input: A `Tensor` of type `string`. The input to be lower-cased. 

1017 encoding: An optional `string`. Defaults to `""`. 

1018 Character encoding of `input`. Allowed values are '' and 'utf-8'. 

1019 Value '' is interpreted as ASCII. 

1020 name: A name for the operation (optional). 

1021 

1022 Returns: 

1023 A `Tensor` of type `string`. 

1024 """ 

1025 _ctx = _context._context or _context.context() 

1026 tld = _ctx._thread_local_data 

1027 if tld.is_eager: 

1028 try: 

1029 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

1030 _ctx, "StringLower", name, input, "encoding", encoding) 

1031 return _result 

1032 except _core._NotOkStatusException as e: 

1033 _ops.raise_from_not_ok_status(e, name) 

1034 except _core._FallbackException: 

1035 pass 

1036 try: 

1037 _result = _dispatcher_for_string_lower( 

1038 (input, encoding, name,), None) 

1039 if _result is not NotImplemented: 

1040 return _result 

1041 return string_lower_eager_fallback( 

1042 input, encoding=encoding, name=name, ctx=_ctx) 

1043 except _core._SymbolicException: 

1044 pass # Add nodes to the TensorFlow graph. 

1045 except (TypeError, ValueError): 

1046 _result = _dispatch.dispatch( 

1047 string_lower, (), dict(input=input, encoding=encoding, name=name) 

1048 ) 

1049 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

1050 return _result 

1051 raise 

1052 else: 

1053 _result = _dispatcher_for_string_lower( 

1054 (input, encoding, name,), None) 

1055 if _result is not NotImplemented: 

1056 return _result 

1057 # Add nodes to the TensorFlow graph. 

1058 if encoding is None: 

1059 encoding = "" 

1060 encoding = _execute.make_str(encoding, "encoding") 

1061 try: 

1062 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

1063 "StringLower", input=input, encoding=encoding, name=name) 

1064 except (TypeError, ValueError): 

1065 _result = _dispatch.dispatch( 

1066 string_lower, (), dict(input=input, encoding=encoding, name=name) 

1067 ) 

1068 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

1069 return _result 

1070 raise 

1071 _result = _outputs[:] 

1072 if _execute.must_record_gradient(): 

1073 _attrs = ("encoding", _op.get_attr("encoding")) 

1074 _inputs_flat = _op.inputs 

1075 _execute.record_gradient( 

1076 "StringLower", _inputs_flat, _attrs, _result) 

1077 _result, = _result 

1078 return _result 

1079 

1080StringLower = tf_export("raw_ops.StringLower")(_ops.to_raw_op(string_lower)) 

1081_dispatcher_for_string_lower = string_lower._tf_type_based_dispatcher.Dispatch 

1082 

1083 

1084def string_lower_eager_fallback(input, encoding, name, ctx): 

1085 if encoding is None: 

1086 encoding = "" 

1087 encoding = _execute.make_str(encoding, "encoding") 

1088 input = _ops.convert_to_tensor(input, _dtypes.string) 

1089 _inputs_flat = [input] 

1090 _attrs = ("encoding", encoding) 

1091 _result = _execute.execute(b"StringLower", 1, inputs=_inputs_flat, 

1092 attrs=_attrs, ctx=ctx, name=name) 

1093 if _execute.must_record_gradient(): 

1094 _execute.record_gradient( 

1095 "StringLower", _inputs_flat, _attrs, _result) 

1096 _result, = _result 

1097 return _result 

1098 

1099_StringNGramsOutput = collections.namedtuple( 

1100 "StringNGrams", 

1101 ["ngrams", "ngrams_splits"]) 

1102 

1103 

1104def string_n_grams(data, data_splits, separator, ngram_widths, left_pad, right_pad, pad_width, preserve_short_sequences, name=None): 

1105 r"""Creates ngrams from ragged string data. 

1106 

1107 This op accepts a ragged tensor with 1 ragged dimension containing only 

1108 strings and outputs a ragged tensor with 1 ragged dimension containing ngrams 

1109 of that string, joined along the innermost axis. 

1110 

1111 Args: 

1112 data: A `Tensor` of type `string`. 

1113 The values tensor of the ragged string tensor to make ngrams out of. Must be a 

1114 1D string tensor. 

1115 data_splits: A `Tensor`. Must be one of the following types: `int32`, `int64`. 

1116 The splits tensor of the ragged string tensor to make ngrams out of. 

1117 separator: A `string`. 

1118 The string to append between elements of the token. Use "" for no separator. 

1119 ngram_widths: A list of `ints`. The sizes of the ngrams to create. 

1120 left_pad: A `string`. 

1121 The string to use to pad the left side of the ngram sequence. Only used if 

1122 pad_width != 0. 

1123 right_pad: A `string`. 

1124 The string to use to pad the right side of the ngram sequence. Only used if 

1125 pad_width != 0. 

1126 pad_width: An `int`. 

1127 The number of padding elements to add to each side of each 

1128 sequence. Note that padding will never be greater than 'ngram_widths'-1 

1129 regardless of this value. If `pad_width=-1`, then add `max(ngram_widths)-1` 

1130 elements. 

1131 preserve_short_sequences: A `bool`. 

1132 name: A name for the operation (optional). 

1133 

1134 Returns: 

1135 A tuple of `Tensor` objects (ngrams, ngrams_splits). 

1136 

1137 ngrams: A `Tensor` of type `string`. 

1138 ngrams_splits: A `Tensor`. Has the same type as `data_splits`. 

1139 """ 

1140 _ctx = _context._context or _context.context() 

1141 tld = _ctx._thread_local_data 

1142 if tld.is_eager: 

1143 try: 

1144 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

1145 _ctx, "StringNGrams", name, data, data_splits, "separator", separator, 

1146 "ngram_widths", ngram_widths, "left_pad", left_pad, "right_pad", 

1147 right_pad, "pad_width", pad_width, "preserve_short_sequences", 

1148 preserve_short_sequences) 

1149 _result = _StringNGramsOutput._make(_result) 

1150 return _result 

1151 except _core._NotOkStatusException as e: 

1152 _ops.raise_from_not_ok_status(e, name) 

1153 except _core._FallbackException: 

1154 pass 

1155 try: 

1156 return string_n_grams_eager_fallback( 

1157 data, data_splits, separator=separator, ngram_widths=ngram_widths, 

1158 left_pad=left_pad, right_pad=right_pad, pad_width=pad_width, 

1159 preserve_short_sequences=preserve_short_sequences, name=name, 

1160 ctx=_ctx) 

1161 except _core._SymbolicException: 

1162 pass # Add nodes to the TensorFlow graph. 

1163 # Add nodes to the TensorFlow graph. 

1164 separator = _execute.make_str(separator, "separator") 

1165 if not isinstance(ngram_widths, (list, tuple)): 

1166 raise TypeError( 

1167 "Expected list for 'ngram_widths' argument to " 

1168 "'string_n_grams' Op, not %r." % ngram_widths) 

1169 ngram_widths = [_execute.make_int(_i, "ngram_widths") for _i in ngram_widths] 

1170 left_pad = _execute.make_str(left_pad, "left_pad") 

1171 right_pad = _execute.make_str(right_pad, "right_pad") 

1172 pad_width = _execute.make_int(pad_width, "pad_width") 

1173 preserve_short_sequences = _execute.make_bool(preserve_short_sequences, "preserve_short_sequences") 

1174 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

1175 "StringNGrams", data=data, data_splits=data_splits, 

1176 separator=separator, ngram_widths=ngram_widths, 

1177 left_pad=left_pad, right_pad=right_pad, 

1178 pad_width=pad_width, 

1179 preserve_short_sequences=preserve_short_sequences, 

1180 name=name) 

1181 _result = _outputs[:] 

1182 if _execute.must_record_gradient(): 

1183 _attrs = ("separator", _op.get_attr("separator"), "ngram_widths", 

1184 _op.get_attr("ngram_widths"), "left_pad", 

1185 _op.get_attr("left_pad"), "right_pad", 

1186 _op.get_attr("right_pad"), "pad_width", 

1187 _op._get_attr_int("pad_width"), "preserve_short_sequences", 

1188 _op._get_attr_bool("preserve_short_sequences"), "Tsplits", 

1189 _op._get_attr_type("Tsplits")) 

1190 _inputs_flat = _op.inputs 

1191 _execute.record_gradient( 

1192 "StringNGrams", _inputs_flat, _attrs, _result) 

1193 _result = _StringNGramsOutput._make(_result) 

1194 return _result 

1195 

1196StringNGrams = tf_export("raw_ops.StringNGrams")(_ops.to_raw_op(string_n_grams)) 

1197 

1198 

1199def string_n_grams_eager_fallback(data, data_splits, separator, ngram_widths, left_pad, right_pad, pad_width, preserve_short_sequences, name, ctx): 

1200 separator = _execute.make_str(separator, "separator") 

1201 if not isinstance(ngram_widths, (list, tuple)): 

1202 raise TypeError( 

1203 "Expected list for 'ngram_widths' argument to " 

1204 "'string_n_grams' Op, not %r." % ngram_widths) 

1205 ngram_widths = [_execute.make_int(_i, "ngram_widths") for _i in ngram_widths] 

1206 left_pad = _execute.make_str(left_pad, "left_pad") 

1207 right_pad = _execute.make_str(right_pad, "right_pad") 

1208 pad_width = _execute.make_int(pad_width, "pad_width") 

1209 preserve_short_sequences = _execute.make_bool(preserve_short_sequences, "preserve_short_sequences") 

1210 _attr_Tsplits, (data_splits,) = _execute.args_to_matching_eager([data_splits], ctx, [_dtypes.int32, _dtypes.int64, ], _dtypes.int64) 

1211 data = _ops.convert_to_tensor(data, _dtypes.string) 

1212 _inputs_flat = [data, data_splits] 

1213 _attrs = ("separator", separator, "ngram_widths", ngram_widths, "left_pad", 

1214 left_pad, "right_pad", right_pad, "pad_width", pad_width, 

1215 "preserve_short_sequences", preserve_short_sequences, "Tsplits", 

1216 _attr_Tsplits) 

1217 _result = _execute.execute(b"StringNGrams", 2, inputs=_inputs_flat, 

1218 attrs=_attrs, ctx=ctx, name=name) 

1219 if _execute.must_record_gradient(): 

1220 _execute.record_gradient( 

1221 "StringNGrams", _inputs_flat, _attrs, _result) 

1222 _result = _StringNGramsOutput._make(_result) 

1223 return _result 

1224 

1225_StringSplitOutput = collections.namedtuple( 

1226 "StringSplit", 

1227 ["indices", "values", "shape"]) 

1228 

1229 

1230def string_split(input, delimiter, skip_empty=True, name=None): 

1231 r"""Split elements of `input` based on `delimiter` into a `SparseTensor`. 

1232 

1233 Let N be the size of source (typically N will be the batch size). Split each 

1234 element of `input` based on `delimiter` and return a `SparseTensor` 

1235 containing the splitted tokens. Empty tokens are ignored. 

1236 

1237 `delimiter` can be empty, or a string of split characters. If `delimiter` is an 

1238 empty string, each element of `input` is split into individual single-byte 

1239 character strings, including splitting of UTF-8 multibyte sequences. Otherwise 

1240 every character of `delimiter` is a potential split point. 

1241 

1242 For example: 

1243 N = 2, input[0] is 'hello world' and input[1] is 'a b c', then the output 

1244 will be 

1245 

1246 indices = [0, 0; 

1247 0, 1; 

1248 1, 0; 

1249 1, 1; 

1250 1, 2] 

1251 shape = [2, 3] 

1252 values = ['hello', 'world', 'a', 'b', 'c'] 

1253 

1254 Args: 

1255 input: A `Tensor` of type `string`. 1-D. Strings to split. 

1256 delimiter: A `Tensor` of type `string`. 

1257 0-D. Delimiter characters (bytes), or empty string. 

1258 skip_empty: An optional `bool`. Defaults to `True`. 

1259 A `bool`. If `True`, skip the empty strings from the result. 

1260 name: A name for the operation (optional). 

1261 

1262 Returns: 

1263 A tuple of `Tensor` objects (indices, values, shape). 

1264 

1265 indices: A `Tensor` of type `int64`. 

1266 values: A `Tensor` of type `string`. 

1267 shape: A `Tensor` of type `int64`. 

1268 """ 

1269 _ctx = _context._context or _context.context() 

1270 tld = _ctx._thread_local_data 

1271 if tld.is_eager: 

1272 try: 

1273 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

1274 _ctx, "StringSplit", name, input, delimiter, "skip_empty", skip_empty) 

1275 _result = _StringSplitOutput._make(_result) 

1276 return _result 

1277 except _core._NotOkStatusException as e: 

1278 _ops.raise_from_not_ok_status(e, name) 

1279 except _core._FallbackException: 

1280 pass 

1281 try: 

1282 return string_split_eager_fallback( 

1283 input, delimiter, skip_empty=skip_empty, name=name, ctx=_ctx) 

1284 except _core._SymbolicException: 

1285 pass # Add nodes to the TensorFlow graph. 

1286 # Add nodes to the TensorFlow graph. 

1287 if skip_empty is None: 

1288 skip_empty = True 

1289 skip_empty = _execute.make_bool(skip_empty, "skip_empty") 

1290 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

1291 "StringSplit", input=input, delimiter=delimiter, 

1292 skip_empty=skip_empty, name=name) 

1293 _result = _outputs[:] 

1294 if _execute.must_record_gradient(): 

1295 _attrs = ("skip_empty", _op._get_attr_bool("skip_empty")) 

1296 _inputs_flat = _op.inputs 

1297 _execute.record_gradient( 

1298 "StringSplit", _inputs_flat, _attrs, _result) 

1299 _result = _StringSplitOutput._make(_result) 

1300 return _result 

1301 

1302StringSplit = tf_export("raw_ops.StringSplit")(_ops.to_raw_op(string_split)) 

1303 

1304 

1305def string_split_eager_fallback(input, delimiter, skip_empty, name, ctx): 

1306 if skip_empty is None: 

1307 skip_empty = True 

1308 skip_empty = _execute.make_bool(skip_empty, "skip_empty") 

1309 input = _ops.convert_to_tensor(input, _dtypes.string) 

1310 delimiter = _ops.convert_to_tensor(delimiter, _dtypes.string) 

1311 _inputs_flat = [input, delimiter] 

1312 _attrs = ("skip_empty", skip_empty) 

1313 _result = _execute.execute(b"StringSplit", 3, inputs=_inputs_flat, 

1314 attrs=_attrs, ctx=ctx, name=name) 

1315 if _execute.must_record_gradient(): 

1316 _execute.record_gradient( 

1317 "StringSplit", _inputs_flat, _attrs, _result) 

1318 _result = _StringSplitOutput._make(_result) 

1319 return _result 

1320 

1321_StringSplitV2Output = collections.namedtuple( 

1322 "StringSplitV2", 

1323 ["indices", "values", "shape"]) 

1324 

1325 

1326def string_split_v2(input, sep, maxsplit=-1, name=None): 

1327 r"""Split elements of `source` based on `sep` into a `SparseTensor`. 

1328 

1329 Let N be the size of source (typically N will be the batch size). Split each 

1330 element of `source` based on `sep` and return a `SparseTensor` 

1331 containing the split tokens. Empty tokens are ignored. 

1332 

1333 For example, N = 2, source[0] is 'hello world' and source[1] is 'a b c', 

1334 then the output will be 

1335 ``` 

1336 st.indices = [0, 0; 

1337 0, 1; 

1338 1, 0; 

1339 1, 1; 

1340 1, 2] 

1341 st.shape = [2, 3] 

1342 st.values = ['hello', 'world', 'a', 'b', 'c'] 

1343 ``` 

1344 

1345 If `sep` is given, consecutive delimiters are not grouped together and are 

1346 deemed to delimit empty strings. For example, source of `"1<>2<><>3"` and 

1347 sep of `"<>"` returns `["1", "2", "", "3"]`. If `sep` is None or an empty 

1348 string, consecutive whitespace are regarded as a single separator, and the 

1349 result will contain no empty strings at the startor end if the string has 

1350 leading or trailing whitespace. 

1351 

1352 Note that the above mentioned behavior matches python's str.split. 

1353 

1354 Args: 

1355 input: A `Tensor` of type `string`. 

1356 `1-D` string `Tensor`, the strings to split. 

1357 sep: A `Tensor` of type `string`. 

1358 `0-D` string `Tensor`, the delimiter character. 

1359 maxsplit: An optional `int`. Defaults to `-1`. 

1360 An `int`. If `maxsplit > 0`, limit of the split of the result. 

1361 name: A name for the operation (optional). 

1362 

1363 Returns: 

1364 A tuple of `Tensor` objects (indices, values, shape). 

1365 

1366 indices: A `Tensor` of type `int64`. 

1367 values: A `Tensor` of type `string`. 

1368 shape: A `Tensor` of type `int64`. 

1369 """ 

1370 _ctx = _context._context or _context.context() 

1371 tld = _ctx._thread_local_data 

1372 if tld.is_eager: 

1373 try: 

1374 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

1375 _ctx, "StringSplitV2", name, input, sep, "maxsplit", maxsplit) 

1376 _result = _StringSplitV2Output._make(_result) 

1377 return _result 

1378 except _core._NotOkStatusException as e: 

1379 _ops.raise_from_not_ok_status(e, name) 

1380 except _core._FallbackException: 

1381 pass 

1382 try: 

1383 return string_split_v2_eager_fallback( 

1384 input, sep, maxsplit=maxsplit, name=name, ctx=_ctx) 

1385 except _core._SymbolicException: 

1386 pass # Add nodes to the TensorFlow graph. 

1387 # Add nodes to the TensorFlow graph. 

1388 if maxsplit is None: 

1389 maxsplit = -1 

1390 maxsplit = _execute.make_int(maxsplit, "maxsplit") 

1391 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

1392 "StringSplitV2", input=input, sep=sep, maxsplit=maxsplit, name=name) 

1393 _result = _outputs[:] 

1394 if _execute.must_record_gradient(): 

1395 _attrs = ("maxsplit", _op._get_attr_int("maxsplit")) 

1396 _inputs_flat = _op.inputs 

1397 _execute.record_gradient( 

1398 "StringSplitV2", _inputs_flat, _attrs, _result) 

1399 _result = _StringSplitV2Output._make(_result) 

1400 return _result 

1401 

1402StringSplitV2 = tf_export("raw_ops.StringSplitV2")(_ops.to_raw_op(string_split_v2)) 

1403 

1404 

1405def string_split_v2_eager_fallback(input, sep, maxsplit, name, ctx): 

1406 if maxsplit is None: 

1407 maxsplit = -1 

1408 maxsplit = _execute.make_int(maxsplit, "maxsplit") 

1409 input = _ops.convert_to_tensor(input, _dtypes.string) 

1410 sep = _ops.convert_to_tensor(sep, _dtypes.string) 

1411 _inputs_flat = [input, sep] 

1412 _attrs = ("maxsplit", maxsplit) 

1413 _result = _execute.execute(b"StringSplitV2", 3, inputs=_inputs_flat, 

1414 attrs=_attrs, ctx=ctx, name=name) 

1415 if _execute.must_record_gradient(): 

1416 _execute.record_gradient( 

1417 "StringSplitV2", _inputs_flat, _attrs, _result) 

1418 _result = _StringSplitV2Output._make(_result) 

1419 return _result 

1420 

1421 

1422@_dispatch.add_fallback_dispatch_list 

1423@_dispatch.add_type_based_api_dispatcher 

1424@tf_export('strings.strip', v1=['strings.strip', 'string_strip']) 

1425@deprecated_endpoints('string_strip') 

1426def string_strip(input, name=None): 

1427 r"""Strip leading and trailing whitespaces from the Tensor. 

1428 

1429 Examples: 

1430 

1431 >>> tf.strings.strip(["\nTensorFlow", " The python library "]).numpy() 

1432 array([b'TensorFlow', b'The python library'], dtype=object) 

1433 

1434 Args: 

1435 input: A `Tensor` of type `string`. A string `Tensor` of any shape. 

1436 name: A name for the operation (optional). 

1437 

1438 Returns: 

1439 A `Tensor` of type `string`. 

1440 """ 

1441 _ctx = _context._context or _context.context() 

1442 tld = _ctx._thread_local_data 

1443 if tld.is_eager: 

1444 try: 

1445 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

1446 _ctx, "StringStrip", name, input) 

1447 return _result 

1448 except _core._NotOkStatusException as e: 

1449 _ops.raise_from_not_ok_status(e, name) 

1450 except _core._FallbackException: 

1451 pass 

1452 try: 

1453 _result = _dispatcher_for_string_strip( 

1454 (input, name,), None) 

1455 if _result is not NotImplemented: 

1456 return _result 

1457 return string_strip_eager_fallback( 

1458 input, name=name, ctx=_ctx) 

1459 except _core._SymbolicException: 

1460 pass # Add nodes to the TensorFlow graph. 

1461 except (TypeError, ValueError): 

1462 _result = _dispatch.dispatch( 

1463 string_strip, (), dict(input=input, name=name) 

1464 ) 

1465 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

1466 return _result 

1467 raise 

1468 else: 

1469 _result = _dispatcher_for_string_strip( 

1470 (input, name,), None) 

1471 if _result is not NotImplemented: 

1472 return _result 

1473 # Add nodes to the TensorFlow graph. 

1474 try: 

1475 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

1476 "StringStrip", input=input, name=name) 

1477 except (TypeError, ValueError): 

1478 _result = _dispatch.dispatch( 

1479 string_strip, (), dict(input=input, name=name) 

1480 ) 

1481 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

1482 return _result 

1483 raise 

1484 _result = _outputs[:] 

1485 if _execute.must_record_gradient(): 

1486 _attrs = () 

1487 _inputs_flat = _op.inputs 

1488 _execute.record_gradient( 

1489 "StringStrip", _inputs_flat, _attrs, _result) 

1490 _result, = _result 

1491 return _result 

1492 

1493StringStrip = tf_export("raw_ops.StringStrip")(_ops.to_raw_op(string_strip)) 

1494_dispatcher_for_string_strip = string_strip._tf_type_based_dispatcher.Dispatch 

1495 

1496 

1497def string_strip_eager_fallback(input, name, ctx): 

1498 input = _ops.convert_to_tensor(input, _dtypes.string) 

1499 _inputs_flat = [input] 

1500 _attrs = None 

1501 _result = _execute.execute(b"StringStrip", 1, inputs=_inputs_flat, 

1502 attrs=_attrs, ctx=ctx, name=name) 

1503 if _execute.must_record_gradient(): 

1504 _execute.record_gradient( 

1505 "StringStrip", _inputs_flat, _attrs, _result) 

1506 _result, = _result 

1507 return _result 

1508 

1509 

1510def string_to_hash_bucket(string_tensor, num_buckets, name=None): 

1511 r"""Converts each string in the input Tensor to its hash mod by a number of buckets. 

1512 

1513 The hash function is deterministic on the content of the string within the 

1514 process. 

1515 

1516 Note that the hash function may change from time to time. 

1517 This functionality will be deprecated and it's recommended to use 

1518 `tf.string_to_hash_bucket_fast()` or `tf.string_to_hash_bucket_strong()`. 

1519 

1520 Args: 

1521 string_tensor: A `Tensor` of type `string`. 

1522 num_buckets: An `int` that is `>= 1`. The number of buckets. 

1523 name: A name for the operation (optional). 

1524 

1525 Returns: 

1526 A `Tensor` of type `int64`. 

1527 """ 

1528 _ctx = _context._context or _context.context() 

1529 tld = _ctx._thread_local_data 

1530 if tld.is_eager: 

1531 try: 

1532 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

1533 _ctx, "StringToHashBucket", name, string_tensor, "num_buckets", 

1534 num_buckets) 

1535 return _result 

1536 except _core._NotOkStatusException as e: 

1537 _ops.raise_from_not_ok_status(e, name) 

1538 except _core._FallbackException: 

1539 pass 

1540 try: 

1541 return string_to_hash_bucket_eager_fallback( 

1542 string_tensor, num_buckets=num_buckets, name=name, ctx=_ctx) 

1543 except _core._SymbolicException: 

1544 pass # Add nodes to the TensorFlow graph. 

1545 # Add nodes to the TensorFlow graph. 

1546 num_buckets = _execute.make_int(num_buckets, "num_buckets") 

1547 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

1548 "StringToHashBucket", string_tensor=string_tensor, 

1549 num_buckets=num_buckets, name=name) 

1550 _result = _outputs[:] 

1551 if _execute.must_record_gradient(): 

1552 _attrs = ("num_buckets", _op._get_attr_int("num_buckets")) 

1553 _inputs_flat = _op.inputs 

1554 _execute.record_gradient( 

1555 "StringToHashBucket", _inputs_flat, _attrs, _result) 

1556 _result, = _result 

1557 return _result 

1558 

1559StringToHashBucket = tf_export("raw_ops.StringToHashBucket")(_ops.to_raw_op(string_to_hash_bucket)) 

1560 

1561 

1562def string_to_hash_bucket_eager_fallback(string_tensor, num_buckets, name, ctx): 

1563 num_buckets = _execute.make_int(num_buckets, "num_buckets") 

1564 string_tensor = _ops.convert_to_tensor(string_tensor, _dtypes.string) 

1565 _inputs_flat = [string_tensor] 

1566 _attrs = ("num_buckets", num_buckets) 

1567 _result = _execute.execute(b"StringToHashBucket", 1, inputs=_inputs_flat, 

1568 attrs=_attrs, ctx=ctx, name=name) 

1569 if _execute.must_record_gradient(): 

1570 _execute.record_gradient( 

1571 "StringToHashBucket", _inputs_flat, _attrs, _result) 

1572 _result, = _result 

1573 return _result 

1574 

1575 

1576@_dispatch.add_fallback_dispatch_list 

1577@_dispatch.add_type_based_api_dispatcher 

1578@tf_export('strings.to_hash_bucket_fast', v1=['strings.to_hash_bucket_fast', 'string_to_hash_bucket_fast']) 

1579@deprecated_endpoints('string_to_hash_bucket_fast') 

1580def string_to_hash_bucket_fast(input, num_buckets, name=None): 

1581 r"""Converts each string in the input Tensor to its hash mod by a number of buckets. 

1582 

1583 The hash function is deterministic on the content of the string within the 

1584 process and will never change. However, it is not suitable for cryptography. 

1585 This function may be used when CPU time is scarce and inputs are trusted or 

1586 unimportant. There is a risk of adversaries constructing inputs that all hash 

1587 to the same bucket. To prevent this problem, use a strong hash function with 

1588 `tf.string_to_hash_bucket_strong`. 

1589 

1590 Examples: 

1591 

1592 >>> tf.strings.to_hash_bucket_fast(["Hello", "TensorFlow", "2.x"], 3).numpy() 

1593 array([0, 2, 2]) 

1594 

1595 Args: 

1596 input: A `Tensor` of type `string`. The strings to assign a hash bucket. 

1597 num_buckets: An `int` that is `>= 1`. The number of buckets. 

1598 name: A name for the operation (optional). 

1599 

1600 Returns: 

1601 A `Tensor` of type `int64`. 

1602 """ 

1603 _ctx = _context._context or _context.context() 

1604 tld = _ctx._thread_local_data 

1605 if tld.is_eager: 

1606 try: 

1607 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

1608 _ctx, "StringToHashBucketFast", name, input, "num_buckets", 

1609 num_buckets) 

1610 return _result 

1611 except _core._NotOkStatusException as e: 

1612 _ops.raise_from_not_ok_status(e, name) 

1613 except _core._FallbackException: 

1614 pass 

1615 try: 

1616 _result = _dispatcher_for_string_to_hash_bucket_fast( 

1617 (input, num_buckets, name,), None) 

1618 if _result is not NotImplemented: 

1619 return _result 

1620 return string_to_hash_bucket_fast_eager_fallback( 

1621 input, num_buckets=num_buckets, name=name, ctx=_ctx) 

1622 except _core._SymbolicException: 

1623 pass # Add nodes to the TensorFlow graph. 

1624 except (TypeError, ValueError): 

1625 _result = _dispatch.dispatch( 

1626 string_to_hash_bucket_fast, (), dict(input=input, 

1627 num_buckets=num_buckets, 

1628 name=name) 

1629 ) 

1630 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

1631 return _result 

1632 raise 

1633 else: 

1634 _result = _dispatcher_for_string_to_hash_bucket_fast( 

1635 (input, num_buckets, name,), None) 

1636 if _result is not NotImplemented: 

1637 return _result 

1638 # Add nodes to the TensorFlow graph. 

1639 num_buckets = _execute.make_int(num_buckets, "num_buckets") 

1640 try: 

1641 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

1642 "StringToHashBucketFast", input=input, num_buckets=num_buckets, 

1643 name=name) 

1644 except (TypeError, ValueError): 

1645 _result = _dispatch.dispatch( 

1646 string_to_hash_bucket_fast, (), dict(input=input, 

1647 num_buckets=num_buckets, 

1648 name=name) 

1649 ) 

1650 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

1651 return _result 

1652 raise 

1653 _result = _outputs[:] 

1654 if _execute.must_record_gradient(): 

1655 _attrs = ("num_buckets", _op._get_attr_int("num_buckets")) 

1656 _inputs_flat = _op.inputs 

1657 _execute.record_gradient( 

1658 "StringToHashBucketFast", _inputs_flat, _attrs, _result) 

1659 _result, = _result 

1660 return _result 

1661 

1662StringToHashBucketFast = tf_export("raw_ops.StringToHashBucketFast")(_ops.to_raw_op(string_to_hash_bucket_fast)) 

1663_dispatcher_for_string_to_hash_bucket_fast = string_to_hash_bucket_fast._tf_type_based_dispatcher.Dispatch 

1664 

1665 

1666def string_to_hash_bucket_fast_eager_fallback(input, num_buckets, name, ctx): 

1667 num_buckets = _execute.make_int(num_buckets, "num_buckets") 

1668 input = _ops.convert_to_tensor(input, _dtypes.string) 

1669 _inputs_flat = [input] 

1670 _attrs = ("num_buckets", num_buckets) 

1671 _result = _execute.execute(b"StringToHashBucketFast", 1, 

1672 inputs=_inputs_flat, attrs=_attrs, ctx=ctx, 

1673 name=name) 

1674 if _execute.must_record_gradient(): 

1675 _execute.record_gradient( 

1676 "StringToHashBucketFast", _inputs_flat, _attrs, _result) 

1677 _result, = _result 

1678 return _result 

1679 

1680 

1681@_dispatch.add_fallback_dispatch_list 

1682@_dispatch.add_type_based_api_dispatcher 

1683@tf_export('strings.to_hash_bucket_strong', v1=['strings.to_hash_bucket_strong', 'string_to_hash_bucket_strong']) 

1684@deprecated_endpoints('string_to_hash_bucket_strong') 

1685def string_to_hash_bucket_strong(input, num_buckets, key, name=None): 

1686 r"""Converts each string in the input Tensor to its hash mod by a number of buckets. 

1687 

1688 The hash function is deterministic on the content of the string within the 

1689 process. The hash function is a keyed hash function, where attribute `key` 

1690 defines the key of the hash function. `key` is an array of 2 elements. 

1691 

1692 A strong hash is important when inputs may be malicious, e.g. URLs with 

1693 additional components. Adversaries could try to make their inputs hash to the 

1694 same bucket for a denial-of-service attack or to skew the results. A strong 

1695 hash can be used to make it difficult to find inputs with a skewed hash value 

1696 distribution over buckets. This requires that the hash function is 

1697 seeded by a high-entropy (random) "key" unknown to the adversary. 

1698 

1699 The additional robustness comes at a cost of roughly 4x higher compute 

1700 time than `tf.string_to_hash_bucket_fast`. 

1701 

1702 Examples: 

1703 

1704 >>> tf.strings.to_hash_bucket_strong(["Hello", "TF"], 3, [1, 2]).numpy() 

1705 array([2, 0]) 

1706 

1707 Args: 

1708 input: A `Tensor` of type `string`. The strings to assign a hash bucket. 

1709 num_buckets: An `int` that is `>= 1`. The number of buckets. 

1710 key: A list of `ints`. 

1711 The key used to seed the hash function, passed as a list of two uint64 

1712 elements. 

1713 name: A name for the operation (optional). 

1714 

1715 Returns: 

1716 A `Tensor` of type `int64`. 

1717 """ 

1718 _ctx = _context._context or _context.context() 

1719 tld = _ctx._thread_local_data 

1720 if tld.is_eager: 

1721 try: 

1722 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

1723 _ctx, "StringToHashBucketStrong", name, input, "num_buckets", 

1724 num_buckets, "key", key) 

1725 return _result 

1726 except _core._NotOkStatusException as e: 

1727 _ops.raise_from_not_ok_status(e, name) 

1728 except _core._FallbackException: 

1729 pass 

1730 try: 

1731 _result = _dispatcher_for_string_to_hash_bucket_strong( 

1732 (input, num_buckets, key, name,), None) 

1733 if _result is not NotImplemented: 

1734 return _result 

1735 return string_to_hash_bucket_strong_eager_fallback( 

1736 input, num_buckets=num_buckets, key=key, name=name, ctx=_ctx) 

1737 except _core._SymbolicException: 

1738 pass # Add nodes to the TensorFlow graph. 

1739 except (TypeError, ValueError): 

1740 _result = _dispatch.dispatch( 

1741 string_to_hash_bucket_strong, (), dict(input=input, 

1742 num_buckets=num_buckets, 

1743 key=key, name=name) 

1744 ) 

1745 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

1746 return _result 

1747 raise 

1748 else: 

1749 _result = _dispatcher_for_string_to_hash_bucket_strong( 

1750 (input, num_buckets, key, name,), None) 

1751 if _result is not NotImplemented: 

1752 return _result 

1753 # Add nodes to the TensorFlow graph. 

1754 num_buckets = _execute.make_int(num_buckets, "num_buckets") 

1755 if not isinstance(key, (list, tuple)): 

1756 raise TypeError( 

1757 "Expected list for 'key' argument to " 

1758 "'string_to_hash_bucket_strong' Op, not %r." % key) 

1759 key = [_execute.make_int(_i, "key") for _i in key] 

1760 try: 

1761 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

1762 "StringToHashBucketStrong", input=input, num_buckets=num_buckets, 

1763 key=key, name=name) 

1764 except (TypeError, ValueError): 

1765 _result = _dispatch.dispatch( 

1766 string_to_hash_bucket_strong, (), dict(input=input, 

1767 num_buckets=num_buckets, 

1768 key=key, name=name) 

1769 ) 

1770 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

1771 return _result 

1772 raise 

1773 _result = _outputs[:] 

1774 if _execute.must_record_gradient(): 

1775 _attrs = ("num_buckets", _op._get_attr_int("num_buckets"), "key", 

1776 _op.get_attr("key")) 

1777 _inputs_flat = _op.inputs 

1778 _execute.record_gradient( 

1779 "StringToHashBucketStrong", _inputs_flat, _attrs, _result) 

1780 _result, = _result 

1781 return _result 

1782 

1783StringToHashBucketStrong = tf_export("raw_ops.StringToHashBucketStrong")(_ops.to_raw_op(string_to_hash_bucket_strong)) 

1784_dispatcher_for_string_to_hash_bucket_strong = string_to_hash_bucket_strong._tf_type_based_dispatcher.Dispatch 

1785 

1786 

1787def string_to_hash_bucket_strong_eager_fallback(input, num_buckets, key, name, ctx): 

1788 num_buckets = _execute.make_int(num_buckets, "num_buckets") 

1789 if not isinstance(key, (list, tuple)): 

1790 raise TypeError( 

1791 "Expected list for 'key' argument to " 

1792 "'string_to_hash_bucket_strong' Op, not %r." % key) 

1793 key = [_execute.make_int(_i, "key") for _i in key] 

1794 input = _ops.convert_to_tensor(input, _dtypes.string) 

1795 _inputs_flat = [input] 

1796 _attrs = ("num_buckets", num_buckets, "key", key) 

1797 _result = _execute.execute(b"StringToHashBucketStrong", 1, 

1798 inputs=_inputs_flat, attrs=_attrs, ctx=ctx, 

1799 name=name) 

1800 if _execute.must_record_gradient(): 

1801 _execute.record_gradient( 

1802 "StringToHashBucketStrong", _inputs_flat, _attrs, _result) 

1803 _result, = _result 

1804 return _result 

1805 

1806 

1807@_dispatch.add_fallback_dispatch_list 

1808@_dispatch.add_type_based_api_dispatcher 

1809@tf_export('strings.upper') 

1810def string_upper(input, encoding="", name=None): 

1811 r"""Converts all lowercase characters into their respective uppercase replacements. 

1812 

1813 Example: 

1814 

1815 >>> tf.strings.upper("CamelCase string and ALL CAPS") 

1816 <tf.Tensor: shape=(), dtype=string, numpy=b'CAMELCASE STRING AND ALL CAPS'> 

1817 

1818 Args: 

1819 input: A `Tensor` of type `string`. The input to be upper-cased. 

1820 encoding: An optional `string`. Defaults to `""`. 

1821 Character encoding of `input`. Allowed values are '' and 'utf-8'. 

1822 Value '' is interpreted as ASCII. 

1823 name: A name for the operation (optional). 

1824 

1825 Returns: 

1826 A `Tensor` of type `string`. 

1827 """ 

1828 _ctx = _context._context or _context.context() 

1829 tld = _ctx._thread_local_data 

1830 if tld.is_eager: 

1831 try: 

1832 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

1833 _ctx, "StringUpper", name, input, "encoding", encoding) 

1834 return _result 

1835 except _core._NotOkStatusException as e: 

1836 _ops.raise_from_not_ok_status(e, name) 

1837 except _core._FallbackException: 

1838 pass 

1839 try: 

1840 _result = _dispatcher_for_string_upper( 

1841 (input, encoding, name,), None) 

1842 if _result is not NotImplemented: 

1843 return _result 

1844 return string_upper_eager_fallback( 

1845 input, encoding=encoding, name=name, ctx=_ctx) 

1846 except _core._SymbolicException: 

1847 pass # Add nodes to the TensorFlow graph. 

1848 except (TypeError, ValueError): 

1849 _result = _dispatch.dispatch( 

1850 string_upper, (), dict(input=input, encoding=encoding, name=name) 

1851 ) 

1852 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

1853 return _result 

1854 raise 

1855 else: 

1856 _result = _dispatcher_for_string_upper( 

1857 (input, encoding, name,), None) 

1858 if _result is not NotImplemented: 

1859 return _result 

1860 # Add nodes to the TensorFlow graph. 

1861 if encoding is None: 

1862 encoding = "" 

1863 encoding = _execute.make_str(encoding, "encoding") 

1864 try: 

1865 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

1866 "StringUpper", input=input, encoding=encoding, name=name) 

1867 except (TypeError, ValueError): 

1868 _result = _dispatch.dispatch( 

1869 string_upper, (), dict(input=input, encoding=encoding, name=name) 

1870 ) 

1871 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

1872 return _result 

1873 raise 

1874 _result = _outputs[:] 

1875 if _execute.must_record_gradient(): 

1876 _attrs = ("encoding", _op.get_attr("encoding")) 

1877 _inputs_flat = _op.inputs 

1878 _execute.record_gradient( 

1879 "StringUpper", _inputs_flat, _attrs, _result) 

1880 _result, = _result 

1881 return _result 

1882 

1883StringUpper = tf_export("raw_ops.StringUpper")(_ops.to_raw_op(string_upper)) 

1884_dispatcher_for_string_upper = string_upper._tf_type_based_dispatcher.Dispatch 

1885 

1886 

1887def string_upper_eager_fallback(input, encoding, name, ctx): 

1888 if encoding is None: 

1889 encoding = "" 

1890 encoding = _execute.make_str(encoding, "encoding") 

1891 input = _ops.convert_to_tensor(input, _dtypes.string) 

1892 _inputs_flat = [input] 

1893 _attrs = ("encoding", encoding) 

1894 _result = _execute.execute(b"StringUpper", 1, inputs=_inputs_flat, 

1895 attrs=_attrs, ctx=ctx, name=name) 

1896 if _execute.must_record_gradient(): 

1897 _execute.record_gradient( 

1898 "StringUpper", _inputs_flat, _attrs, _result) 

1899 _result, = _result 

1900 return _result 

1901 

1902 

1903def substr(input, pos, len, unit="BYTE", name=None): 

1904 r"""Return substrings from `Tensor` of strings. 

1905 

1906 For each string in the input `Tensor`, creates a substring starting at index 

1907 `pos` with a total length of `len`. 

1908 

1909 If `len` defines a substring that would extend beyond the length of the input 

1910 string, or if `len` is negative, then as many characters as possible are used. 

1911 

1912 A negative `pos` indicates distance within the string backwards from the end. 

1913 

1914 If `pos` specifies an index which is out of range for any of the input strings, 

1915 then an `InvalidArgumentError` is thrown. 

1916 

1917 `pos` and `len` must have the same shape, otherwise a `ValueError` is thrown on 

1918 Op creation. 

1919 

1920 *NOTE*: `Substr` supports broadcasting up to two dimensions. More about 

1921 broadcasting 

1922 [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) 

1923 

1924 --- 

1925 

1926 Examples 

1927 

1928 Using scalar `pos` and `len`: 

1929 

1930 ```python 

1931 input = [b'Hello', b'World'] 

1932 position = 1 

1933 length = 3 

1934 

1935 output = [b'ell', b'orl'] 

1936 ``` 

1937 

1938 Using `pos` and `len` with same shape as `input`: 

1939 

1940 ```python 

1941 input = [[b'ten', b'eleven', b'twelve'], 

1942 [b'thirteen', b'fourteen', b'fifteen'], 

1943 [b'sixteen', b'seventeen', b'eighteen']] 

1944 position = [[1, 2, 3], 

1945 [1, 2, 3], 

1946 [1, 2, 3]] 

1947 length = [[2, 3, 4], 

1948 [4, 3, 2], 

1949 [5, 5, 5]] 

1950 

1951 output = [[b'en', b'eve', b'lve'], 

1952 [b'hirt', b'urt', b'te'], 

1953 [b'ixtee', b'vente', b'hteen']] 

1954 ``` 

1955 

1956 Broadcasting `pos` and `len` onto `input`: 

1957 

1958 ``` 

1959 input = [[b'ten', b'eleven', b'twelve'], 

1960 [b'thirteen', b'fourteen', b'fifteen'], 

1961 [b'sixteen', b'seventeen', b'eighteen'], 

1962 [b'nineteen', b'twenty', b'twentyone']] 

1963 position = [1, 2, 3] 

1964 length = [1, 2, 3] 

1965 

1966 output = [[b'e', b'ev', b'lve'], 

1967 [b'h', b'ur', b'tee'], 

1968 [b'i', b've', b'hte'], 

1969 [b'i', b'en', b'nty']] 

1970 ``` 

1971 

1972 Broadcasting `input` onto `pos` and `len`: 

1973 

1974 ``` 

1975 input = b'thirteen' 

1976 position = [1, 5, 7] 

1977 length = [3, 2, 1] 

1978 

1979 output = [b'hir', b'ee', b'n'] 

1980 ``` 

1981 

1982 Raises: 

1983 

1984 * `ValueError`: If the first argument cannot be converted to a 

1985 Tensor of `dtype string`. 

1986 * `InvalidArgumentError`: If indices are out of range. 

1987 * `ValueError`: If `pos` and `len` are not the same shape. 

1988 

1989 Args: 

1990 input: A `Tensor` of type `string`. Tensor of strings 

1991 pos: A `Tensor`. Must be one of the following types: `int32`, `int64`. 

1992 Scalar defining the position of first character in each substring 

1993 len: A `Tensor`. Must have the same type as `pos`. 

1994 Scalar defining the number of characters to include in each substring 

1995 unit: An optional `string` from: `"BYTE", "UTF8_CHAR"`. Defaults to `"BYTE"`. 

1996 The unit that is used to create the substring. One of: `"BYTE"` (for 

1997 defining position and length by bytes) or `"UTF8_CHAR"` (for the UTF-8 

1998 encoded Unicode code points). The default is `"BYTE"`. Results are undefined if 

1999 `unit=UTF8_CHAR` and the `input` strings do not contain structurally valid 

2000 UTF-8. 

2001 name: A name for the operation (optional). 

2002 

2003 Returns: 

2004 A `Tensor` of type `string`. 

2005 """ 

2006 _ctx = _context._context or _context.context() 

2007 tld = _ctx._thread_local_data 

2008 if tld.is_eager: 

2009 try: 

2010 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

2011 _ctx, "Substr", name, input, pos, len, "unit", unit) 

2012 return _result 

2013 except _core._NotOkStatusException as e: 

2014 _ops.raise_from_not_ok_status(e, name) 

2015 except _core._FallbackException: 

2016 pass 

2017 try: 

2018 return substr_eager_fallback( 

2019 input, pos, len, unit=unit, name=name, ctx=_ctx) 

2020 except _core._SymbolicException: 

2021 pass # Add nodes to the TensorFlow graph. 

2022 # Add nodes to the TensorFlow graph. 

2023 if unit is None: 

2024 unit = "BYTE" 

2025 unit = _execute.make_str(unit, "unit") 

2026 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

2027 "Substr", input=input, pos=pos, len=len, unit=unit, name=name) 

2028 _result = _outputs[:] 

2029 if _execute.must_record_gradient(): 

2030 _attrs = ("T", _op._get_attr_type("T"), "unit", _op.get_attr("unit")) 

2031 _inputs_flat = _op.inputs 

2032 _execute.record_gradient( 

2033 "Substr", _inputs_flat, _attrs, _result) 

2034 _result, = _result 

2035 return _result 

2036 

2037Substr = tf_export("raw_ops.Substr")(_ops.to_raw_op(substr)) 

2038 

2039 

2040def substr_eager_fallback(input, pos, len, unit, name, ctx): 

2041 if unit is None: 

2042 unit = "BYTE" 

2043 unit = _execute.make_str(unit, "unit") 

2044 _attr_T, _inputs_T = _execute.args_to_matching_eager([pos, len], ctx, [_dtypes.int32, _dtypes.int64, ]) 

2045 (pos, len) = _inputs_T 

2046 input = _ops.convert_to_tensor(input, _dtypes.string) 

2047 _inputs_flat = [input, pos, len] 

2048 _attrs = ("T", _attr_T, "unit", unit) 

2049 _result = _execute.execute(b"Substr", 1, inputs=_inputs_flat, attrs=_attrs, 

2050 ctx=ctx, name=name) 

2051 if _execute.must_record_gradient(): 

2052 _execute.record_gradient( 

2053 "Substr", _inputs_flat, _attrs, _result) 

2054 _result, = _result 

2055 return _result 

2056 

2057_UnicodeDecodeOutput = collections.namedtuple( 

2058 "UnicodeDecode", 

2059 ["row_splits", "char_values"]) 

2060 

2061 

2062def unicode_decode(input, input_encoding, errors="replace", replacement_char=65533, replace_control_characters=False, Tsplits=_dtypes.int64, name=None): 

2063 r"""Decodes each string in `input` into a sequence of Unicode code points. 

2064 

2065 The character codepoints for all strings are returned using a single vector 

2066 `char_values`, with strings expanded to characters in row-major order. 

2067 

2068 The `row_splits` tensor indicates where the codepoints for 

2069 each input string begin and end within the `char_values` tensor. 

2070 In particular, the values for the `i`th 

2071 string (in row-major order) are stored in the slice 

2072 `[row_splits[i]:row_splits[i+1]]`. Thus: 

2073 

2074 * `char_values[row_splits[i]+j]` is the Unicode codepoint for the `j`th 

2075 character in the `i`th string (in row-major order). 

2076 * `row_splits[i+1] - row_splits[i]` is the number of characters in the `i`th 

2077 string (in row-major order). 

2078 

2079 Args: 

2080 input: A `Tensor` of type `string`. 

2081 The text to be decoded. Can have any shape. Note that the output is flattened 

2082 to a vector of char values. 

2083 input_encoding: A `string`. 

2084 Text encoding of the input strings. This is any of the encodings supported 

2085 by ICU ucnv algorithmic converters. Examples: `"UTF-16", "US ASCII", "UTF-8"`. 

2086 errors: An optional `string` from: `"strict", "replace", "ignore"`. Defaults to `"replace"`. 

2087 Error handling policy when there is invalid formatting found in the input. 

2088 The value of 'strict' will cause the operation to produce a InvalidArgument 

2089 error on any invalid input formatting. A value of 'replace' (the default) will 

2090 cause the operation to replace any invalid formatting in the input with the 

2091 `replacement_char` codepoint. A value of 'ignore' will cause the operation to 

2092 skip any invalid formatting in the input and produce no corresponding output 

2093 character. 

2094 replacement_char: An optional `int`. Defaults to `65533`. 

2095 The replacement character codepoint to be used in place of any invalid 

2096 formatting in the input when `errors='replace'`. Any valid unicode codepoint may 

2097 be used. The default value is the default unicode replacement character is 

2098 0xFFFD or U+65533.) 

2099 replace_control_characters: An optional `bool`. Defaults to `False`. 

2100 Whether to replace the C0 control characters (00-1F) with the 

2101 `replacement_char`. Default is false. 

2102 Tsplits: An optional `tf.DType` from: `tf.int32, tf.int64`. Defaults to `tf.int64`. 

2103 name: A name for the operation (optional). 

2104 

2105 Returns: 

2106 A tuple of `Tensor` objects (row_splits, char_values). 

2107 

2108 row_splits: A `Tensor` of type `Tsplits`. 

2109 char_values: A `Tensor` of type `int32`. 

2110 """ 

2111 _ctx = _context._context or _context.context() 

2112 tld = _ctx._thread_local_data 

2113 if tld.is_eager: 

2114 try: 

2115 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

2116 _ctx, "UnicodeDecode", name, input, "input_encoding", input_encoding, 

2117 "errors", errors, "replacement_char", replacement_char, 

2118 "replace_control_characters", replace_control_characters, "Tsplits", 

2119 Tsplits) 

2120 _result = _UnicodeDecodeOutput._make(_result) 

2121 return _result 

2122 except _core._NotOkStatusException as e: 

2123 _ops.raise_from_not_ok_status(e, name) 

2124 except _core._FallbackException: 

2125 pass 

2126 try: 

2127 return unicode_decode_eager_fallback( 

2128 input, input_encoding=input_encoding, errors=errors, 

2129 replacement_char=replacement_char, 

2130 replace_control_characters=replace_control_characters, 

2131 Tsplits=Tsplits, name=name, ctx=_ctx) 

2132 except _core._SymbolicException: 

2133 pass # Add nodes to the TensorFlow graph. 

2134 # Add nodes to the TensorFlow graph. 

2135 input_encoding = _execute.make_str(input_encoding, "input_encoding") 

2136 if errors is None: 

2137 errors = "replace" 

2138 errors = _execute.make_str(errors, "errors") 

2139 if replacement_char is None: 

2140 replacement_char = 65533 

2141 replacement_char = _execute.make_int(replacement_char, "replacement_char") 

2142 if replace_control_characters is None: 

2143 replace_control_characters = False 

2144 replace_control_characters = _execute.make_bool(replace_control_characters, "replace_control_characters") 

2145 if Tsplits is None: 

2146 Tsplits = _dtypes.int64 

2147 Tsplits = _execute.make_type(Tsplits, "Tsplits") 

2148 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

2149 "UnicodeDecode", input=input, input_encoding=input_encoding, 

2150 errors=errors, replacement_char=replacement_char, 

2151 replace_control_characters=replace_control_characters, 

2152 Tsplits=Tsplits, name=name) 

2153 _result = _outputs[:] 

2154 if _execute.must_record_gradient(): 

2155 _attrs = ("input_encoding", _op.get_attr("input_encoding"), "errors", 

2156 _op.get_attr("errors"), "replacement_char", 

2157 _op._get_attr_int("replacement_char"), 

2158 "replace_control_characters", 

2159 _op._get_attr_bool("replace_control_characters"), "Tsplits", 

2160 _op._get_attr_type("Tsplits")) 

2161 _inputs_flat = _op.inputs 

2162 _execute.record_gradient( 

2163 "UnicodeDecode", _inputs_flat, _attrs, _result) 

2164 _result = _UnicodeDecodeOutput._make(_result) 

2165 return _result 

2166 

2167UnicodeDecode = tf_export("raw_ops.UnicodeDecode")(_ops.to_raw_op(unicode_decode)) 

2168 

2169 

2170def unicode_decode_eager_fallback(input, input_encoding, errors, replacement_char, replace_control_characters, Tsplits, name, ctx): 

2171 input_encoding = _execute.make_str(input_encoding, "input_encoding") 

2172 if errors is None: 

2173 errors = "replace" 

2174 errors = _execute.make_str(errors, "errors") 

2175 if replacement_char is None: 

2176 replacement_char = 65533 

2177 replacement_char = _execute.make_int(replacement_char, "replacement_char") 

2178 if replace_control_characters is None: 

2179 replace_control_characters = False 

2180 replace_control_characters = _execute.make_bool(replace_control_characters, "replace_control_characters") 

2181 if Tsplits is None: 

2182 Tsplits = _dtypes.int64 

2183 Tsplits = _execute.make_type(Tsplits, "Tsplits") 

2184 input = _ops.convert_to_tensor(input, _dtypes.string) 

2185 _inputs_flat = [input] 

2186 _attrs = ("input_encoding", input_encoding, "errors", errors, 

2187 "replacement_char", replacement_char, "replace_control_characters", 

2188 replace_control_characters, "Tsplits", Tsplits) 

2189 _result = _execute.execute(b"UnicodeDecode", 2, inputs=_inputs_flat, 

2190 attrs=_attrs, ctx=ctx, name=name) 

2191 if _execute.must_record_gradient(): 

2192 _execute.record_gradient( 

2193 "UnicodeDecode", _inputs_flat, _attrs, _result) 

2194 _result = _UnicodeDecodeOutput._make(_result) 

2195 return _result 

2196 

2197_UnicodeDecodeWithOffsetsOutput = collections.namedtuple( 

2198 "UnicodeDecodeWithOffsets", 

2199 ["row_splits", "char_values", "char_to_byte_starts"]) 

2200 

2201 

2202def unicode_decode_with_offsets(input, input_encoding, errors="replace", replacement_char=65533, replace_control_characters=False, Tsplits=_dtypes.int64, name=None): 

2203 r"""Decodes each string in `input` into a sequence of Unicode code points. 

2204 

2205 The character codepoints for all strings are returned using a single vector 

2206 `char_values`, with strings expanded to characters in row-major order. 

2207 Similarly, the character start byte offsets are returned using a single vector 

2208 `char_to_byte_starts`, with strings expanded in row-major order. 

2209 

2210 The `row_splits` tensor indicates where the codepoints and start offsets for 

2211 each input string begin and end within the `char_values` and 

2212 `char_to_byte_starts` tensors. In particular, the values for the `i`th 

2213 string (in row-major order) are stored in the slice 

2214 `[row_splits[i]:row_splits[i+1]]`. Thus: 

2215 

2216 * `char_values[row_splits[i]+j]` is the Unicode codepoint for the `j`th 

2217 character in the `i`th string (in row-major order). 

2218 * `char_to_bytes_starts[row_splits[i]+j]` is the start byte offset for the `j`th 

2219 character in the `i`th string (in row-major order). 

2220 * `row_splits[i+1] - row_splits[i]` is the number of characters in the `i`th 

2221 string (in row-major order). 

2222 

2223 Args: 

2224 input: A `Tensor` of type `string`. 

2225 The text to be decoded. Can have any shape. Note that the output is flattened 

2226 to a vector of char values. 

2227 input_encoding: A `string`. 

2228 Text encoding of the input strings. This is any of the encodings supported 

2229 by ICU ucnv algorithmic converters. Examples: `"UTF-16", "US ASCII", "UTF-8"`. 

2230 errors: An optional `string` from: `"strict", "replace", "ignore"`. Defaults to `"replace"`. 

2231 Error handling policy when there is invalid formatting found in the input. 

2232 The value of 'strict' will cause the operation to produce a InvalidArgument 

2233 error on any invalid input formatting. A value of 'replace' (the default) will 

2234 cause the operation to replace any invalid formatting in the input with the 

2235 `replacement_char` codepoint. A value of 'ignore' will cause the operation to 

2236 skip any invalid formatting in the input and produce no corresponding output 

2237 character. 

2238 replacement_char: An optional `int`. Defaults to `65533`. 

2239 The replacement character codepoint to be used in place of any invalid 

2240 formatting in the input when `errors='replace'`. Any valid unicode codepoint may 

2241 be used. The default value is the default unicode replacement character is 

2242 0xFFFD or U+65533.) 

2243 replace_control_characters: An optional `bool`. Defaults to `False`. 

2244 Whether to replace the C0 control characters (00-1F) with the 

2245 `replacement_char`. Default is false. 

2246 Tsplits: An optional `tf.DType` from: `tf.int32, tf.int64`. Defaults to `tf.int64`. 

2247 name: A name for the operation (optional). 

2248 

2249 Returns: 

2250 A tuple of `Tensor` objects (row_splits, char_values, char_to_byte_starts). 

2251 

2252 row_splits: A `Tensor` of type `Tsplits`. 

2253 char_values: A `Tensor` of type `int32`. 

2254 char_to_byte_starts: A `Tensor` of type `int64`. 

2255 """ 

2256 _ctx = _context._context or _context.context() 

2257 tld = _ctx._thread_local_data 

2258 if tld.is_eager: 

2259 try: 

2260 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

2261 _ctx, "UnicodeDecodeWithOffsets", name, input, "input_encoding", 

2262 input_encoding, "errors", errors, "replacement_char", 

2263 replacement_char, "replace_control_characters", 

2264 replace_control_characters, "Tsplits", Tsplits) 

2265 _result = _UnicodeDecodeWithOffsetsOutput._make(_result) 

2266 return _result 

2267 except _core._NotOkStatusException as e: 

2268 _ops.raise_from_not_ok_status(e, name) 

2269 except _core._FallbackException: 

2270 pass 

2271 try: 

2272 return unicode_decode_with_offsets_eager_fallback( 

2273 input, input_encoding=input_encoding, errors=errors, 

2274 replacement_char=replacement_char, 

2275 replace_control_characters=replace_control_characters, 

2276 Tsplits=Tsplits, name=name, ctx=_ctx) 

2277 except _core._SymbolicException: 

2278 pass # Add nodes to the TensorFlow graph. 

2279 # Add nodes to the TensorFlow graph. 

2280 input_encoding = _execute.make_str(input_encoding, "input_encoding") 

2281 if errors is None: 

2282 errors = "replace" 

2283 errors = _execute.make_str(errors, "errors") 

2284 if replacement_char is None: 

2285 replacement_char = 65533 

2286 replacement_char = _execute.make_int(replacement_char, "replacement_char") 

2287 if replace_control_characters is None: 

2288 replace_control_characters = False 

2289 replace_control_characters = _execute.make_bool(replace_control_characters, "replace_control_characters") 

2290 if Tsplits is None: 

2291 Tsplits = _dtypes.int64 

2292 Tsplits = _execute.make_type(Tsplits, "Tsplits") 

2293 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

2294 "UnicodeDecodeWithOffsets", input=input, 

2295 input_encoding=input_encoding, 

2296 errors=errors, 

2297 replacement_char=replacement_char, 

2298 replace_control_characters=replace_control_characters, 

2299 Tsplits=Tsplits, name=name) 

2300 _result = _outputs[:] 

2301 if _execute.must_record_gradient(): 

2302 _attrs = ("input_encoding", _op.get_attr("input_encoding"), "errors", 

2303 _op.get_attr("errors"), "replacement_char", 

2304 _op._get_attr_int("replacement_char"), 

2305 "replace_control_characters", 

2306 _op._get_attr_bool("replace_control_characters"), "Tsplits", 

2307 _op._get_attr_type("Tsplits")) 

2308 _inputs_flat = _op.inputs 

2309 _execute.record_gradient( 

2310 "UnicodeDecodeWithOffsets", _inputs_flat, _attrs, _result) 

2311 _result = _UnicodeDecodeWithOffsetsOutput._make(_result) 

2312 return _result 

2313 

2314UnicodeDecodeWithOffsets = tf_export("raw_ops.UnicodeDecodeWithOffsets")(_ops.to_raw_op(unicode_decode_with_offsets)) 

2315 

2316 

2317def unicode_decode_with_offsets_eager_fallback(input, input_encoding, errors, replacement_char, replace_control_characters, Tsplits, name, ctx): 

2318 input_encoding = _execute.make_str(input_encoding, "input_encoding") 

2319 if errors is None: 

2320 errors = "replace" 

2321 errors = _execute.make_str(errors, "errors") 

2322 if replacement_char is None: 

2323 replacement_char = 65533 

2324 replacement_char = _execute.make_int(replacement_char, "replacement_char") 

2325 if replace_control_characters is None: 

2326 replace_control_characters = False 

2327 replace_control_characters = _execute.make_bool(replace_control_characters, "replace_control_characters") 

2328 if Tsplits is None: 

2329 Tsplits = _dtypes.int64 

2330 Tsplits = _execute.make_type(Tsplits, "Tsplits") 

2331 input = _ops.convert_to_tensor(input, _dtypes.string) 

2332 _inputs_flat = [input] 

2333 _attrs = ("input_encoding", input_encoding, "errors", errors, 

2334 "replacement_char", replacement_char, "replace_control_characters", 

2335 replace_control_characters, "Tsplits", Tsplits) 

2336 _result = _execute.execute(b"UnicodeDecodeWithOffsets", 3, 

2337 inputs=_inputs_flat, attrs=_attrs, ctx=ctx, 

2338 name=name) 

2339 if _execute.must_record_gradient(): 

2340 _execute.record_gradient( 

2341 "UnicodeDecodeWithOffsets", _inputs_flat, _attrs, _result) 

2342 _result = _UnicodeDecodeWithOffsetsOutput._make(_result) 

2343 return _result 

2344 

2345 

2346def unicode_encode(input_values, input_splits, output_encoding, errors="replace", replacement_char=65533, name=None): 

2347 r"""Encode a tensor of ints into unicode strings. 

2348 

2349 Returns a vector of strings, where `output[i]` is constructed by encoding the 

2350 Unicode codepoints in `input_values[input_splits[i]:input_splits[i+1]]` 

2351 using `output_encoding`. 

2352 

2353 --- 

2354 

2355 Example: 

2356 

2357 ``` 

2358 input_values = [72, 101, 108, 108, 111, 87, 111, 114, 108, 100] 

2359 input_splits = [0, 5, 10] 

2360 output_encoding = 'UTF-8' 

2361 

2362 output = ['Hello', 'World'] 

2363 ``` 

2364 

2365 Args: 

2366 input_values: A `Tensor` of type `int32`. 

2367 A 1D tensor containing the unicode codepoints that should be encoded. 

2368 input_splits: A `Tensor`. Must be one of the following types: `int32`, `int64`. 

2369 A 1D tensor specifying how the unicode codepoints should be split into strings. 

2370 In particular, `output[i]` is constructed by encoding the codepoints in the 

2371 slice `input_values[input_splits[i]:input_splits[i+1]]`. 

2372 output_encoding: A `string` from: `"UTF-8", "UTF-16-BE", "UTF-32-BE"`. 

2373 Unicode encoding of the output strings. Valid encodings are: `"UTF-8", 

2374 "UTF-16-BE", and "UTF-32-BE"`. 

2375 errors: An optional `string` from: `"ignore", "replace", "strict"`. Defaults to `"replace"`. 

2376 Error handling policy when there is invalid formatting found in the input. 

2377 The value of 'strict' will cause the operation to produce a InvalidArgument 

2378 error on any invalid input formatting. A value of 'replace' (the default) will 

2379 cause the operation to replace any invalid formatting in the input with the 

2380 `replacement_char` codepoint. A value of 'ignore' will cause the operation to 

2381 skip any invalid formatting in the input and produce no corresponding output 

2382 character. 

2383 replacement_char: An optional `int`. Defaults to `65533`. 

2384 The replacement character codepoint to be used in place of any invalid 

2385 formatting in the input when `errors='replace'`. Any valid unicode codepoint may 

2386 be used. The default value is the default unicode replacement character is 

2387 0xFFFD (U+65533). 

2388 name: A name for the operation (optional). 

2389 

2390 Returns: 

2391 A `Tensor` of type `string`. 

2392 """ 

2393 _ctx = _context._context or _context.context() 

2394 tld = _ctx._thread_local_data 

2395 if tld.is_eager: 

2396 try: 

2397 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

2398 _ctx, "UnicodeEncode", name, input_values, input_splits, "errors", 

2399 errors, "output_encoding", output_encoding, "replacement_char", 

2400 replacement_char) 

2401 return _result 

2402 except _core._NotOkStatusException as e: 

2403 _ops.raise_from_not_ok_status(e, name) 

2404 except _core._FallbackException: 

2405 pass 

2406 try: 

2407 return unicode_encode_eager_fallback( 

2408 input_values, input_splits, errors=errors, 

2409 output_encoding=output_encoding, replacement_char=replacement_char, 

2410 name=name, ctx=_ctx) 

2411 except _core._SymbolicException: 

2412 pass # Add nodes to the TensorFlow graph. 

2413 # Add nodes to the TensorFlow graph. 

2414 output_encoding = _execute.make_str(output_encoding, "output_encoding") 

2415 if errors is None: 

2416 errors = "replace" 

2417 errors = _execute.make_str(errors, "errors") 

2418 if replacement_char is None: 

2419 replacement_char = 65533 

2420 replacement_char = _execute.make_int(replacement_char, "replacement_char") 

2421 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

2422 "UnicodeEncode", input_values=input_values, input_splits=input_splits, 

2423 output_encoding=output_encoding, errors=errors, 

2424 replacement_char=replacement_char, name=name) 

2425 _result = _outputs[:] 

2426 if _execute.must_record_gradient(): 

2427 _attrs = ("errors", _op.get_attr("errors"), "output_encoding", 

2428 _op.get_attr("output_encoding"), "replacement_char", 

2429 _op._get_attr_int("replacement_char"), "Tsplits", 

2430 _op._get_attr_type("Tsplits")) 

2431 _inputs_flat = _op.inputs 

2432 _execute.record_gradient( 

2433 "UnicodeEncode", _inputs_flat, _attrs, _result) 

2434 _result, = _result 

2435 return _result 

2436 

2437UnicodeEncode = tf_export("raw_ops.UnicodeEncode")(_ops.to_raw_op(unicode_encode)) 

2438 

2439 

2440def unicode_encode_eager_fallback(input_values, input_splits, output_encoding, errors, replacement_char, name, ctx): 

2441 output_encoding = _execute.make_str(output_encoding, "output_encoding") 

2442 if errors is None: 

2443 errors = "replace" 

2444 errors = _execute.make_str(errors, "errors") 

2445 if replacement_char is None: 

2446 replacement_char = 65533 

2447 replacement_char = _execute.make_int(replacement_char, "replacement_char") 

2448 _attr_Tsplits, (input_splits,) = _execute.args_to_matching_eager([input_splits], ctx, [_dtypes.int32, _dtypes.int64, ], _dtypes.int64) 

2449 input_values = _ops.convert_to_tensor(input_values, _dtypes.int32) 

2450 _inputs_flat = [input_values, input_splits] 

2451 _attrs = ("errors", errors, "output_encoding", output_encoding, 

2452 "replacement_char", replacement_char, "Tsplits", _attr_Tsplits) 

2453 _result = _execute.execute(b"UnicodeEncode", 1, inputs=_inputs_flat, 

2454 attrs=_attrs, ctx=ctx, name=name) 

2455 if _execute.must_record_gradient(): 

2456 _execute.record_gradient( 

2457 "UnicodeEncode", _inputs_flat, _attrs, _result) 

2458 _result, = _result 

2459 return _result 

2460 

2461 

2462@_dispatch.add_fallback_dispatch_list 

2463@_dispatch.add_type_based_api_dispatcher 

2464@tf_export('strings.unicode_script') 

2465def unicode_script(input, name=None): 

2466 r"""Determine the script codes of a given tensor of Unicode integer code points. 

2467 

2468 This operation converts Unicode code points to script codes corresponding to 

2469 each code point. Script codes correspond to International Components for 

2470 Unicode (ICU) UScriptCode values. 

2471 

2472 See 

2473 [ICU project docs](http://icu-project.org/apiref/icu4c/uscript_8h.html) 

2474 for more details on script codes. 

2475 

2476 For an example, see the unicode strings guide on [unicode scripts] 

2477 (https://www.tensorflow.org/tutorials/load_data/unicode#representing_unicode). 

2478 

2479 Returns -1 (USCRIPT_INVALID_CODE) for invalid codepoints. Output shape will 

2480 match input shape. 

2481 

2482 Examples: 

2483 

2484 >>> tf.strings.unicode_script([1, 31, 38]) 

2485 <tf.Tensor: shape=(3,), dtype=int32, numpy=array([0, 0, 0], dtype=int32)> 

2486 

2487 Args: 

2488 input: A `Tensor` of type `int32`. A Tensor of int32 Unicode code points. 

2489 name: A name for the operation (optional). 

2490 

2491 Returns: 

2492 A `Tensor` of type `int32`. 

2493 """ 

2494 _ctx = _context._context or _context.context() 

2495 tld = _ctx._thread_local_data 

2496 if tld.is_eager: 

2497 try: 

2498 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

2499 _ctx, "UnicodeScript", name, input) 

2500 return _result 

2501 except _core._NotOkStatusException as e: 

2502 _ops.raise_from_not_ok_status(e, name) 

2503 except _core._FallbackException: 

2504 pass 

2505 try: 

2506 _result = _dispatcher_for_unicode_script( 

2507 (input, name,), None) 

2508 if _result is not NotImplemented: 

2509 return _result 

2510 return unicode_script_eager_fallback( 

2511 input, name=name, ctx=_ctx) 

2512 except _core._SymbolicException: 

2513 pass # Add nodes to the TensorFlow graph. 

2514 except (TypeError, ValueError): 

2515 _result = _dispatch.dispatch( 

2516 unicode_script, (), dict(input=input, name=name) 

2517 ) 

2518 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

2519 return _result 

2520 raise 

2521 else: 

2522 _result = _dispatcher_for_unicode_script( 

2523 (input, name,), None) 

2524 if _result is not NotImplemented: 

2525 return _result 

2526 # Add nodes to the TensorFlow graph. 

2527 try: 

2528 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

2529 "UnicodeScript", input=input, name=name) 

2530 except (TypeError, ValueError): 

2531 _result = _dispatch.dispatch( 

2532 unicode_script, (), dict(input=input, name=name) 

2533 ) 

2534 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

2535 return _result 

2536 raise 

2537 _result = _outputs[:] 

2538 if _execute.must_record_gradient(): 

2539 _attrs = () 

2540 _inputs_flat = _op.inputs 

2541 _execute.record_gradient( 

2542 "UnicodeScript", _inputs_flat, _attrs, _result) 

2543 _result, = _result 

2544 return _result 

2545 

2546UnicodeScript = tf_export("raw_ops.UnicodeScript")(_ops.to_raw_op(unicode_script)) 

2547_dispatcher_for_unicode_script = unicode_script._tf_type_based_dispatcher.Dispatch 

2548 

2549 

2550def unicode_script_eager_fallback(input, name, ctx): 

2551 input = _ops.convert_to_tensor(input, _dtypes.int32) 

2552 _inputs_flat = [input] 

2553 _attrs = None 

2554 _result = _execute.execute(b"UnicodeScript", 1, inputs=_inputs_flat, 

2555 attrs=_attrs, ctx=ctx, name=name) 

2556 if _execute.must_record_gradient(): 

2557 _execute.record_gradient( 

2558 "UnicodeScript", _inputs_flat, _attrs, _result) 

2559 _result, = _result 

2560 return _result 

2561 

2562 

2563@_dispatch.add_fallback_dispatch_list 

2564@_dispatch.add_type_based_api_dispatcher 

2565@tf_export('strings.unicode_transcode') 

2566def unicode_transcode(input, input_encoding, output_encoding, errors="replace", replacement_char=65533, replace_control_characters=False, name=None): 

2567 r"""Transcode the input text from a source encoding to a destination encoding. 

2568 

2569 The input is a string tensor of any shape. The output is a string tensor of 

2570 the same shape containing the transcoded strings. Output strings are always 

2571 valid unicode. If the input contains invalid encoding positions, the 

2572 `errors` attribute sets the policy for how to deal with them. If the default 

2573 error-handling policy is used, invalid formatting will be substituted in the 

2574 output by the `replacement_char`. If the errors policy is to `ignore`, any 

2575 invalid encoding positions in the input are skipped and not included in the 

2576 output. If it set to `strict` then any invalid formatting will result in an 

2577 InvalidArgument error. 

2578 

2579 This operation can be used with `output_encoding = input_encoding` to enforce 

2580 correct formatting for inputs even if they are already in the desired encoding. 

2581 

2582 If the input is prefixed by a Byte Order Mark needed to determine encoding 

2583 (e.g. if the encoding is UTF-16 and the BOM indicates big-endian), then that 

2584 BOM will be consumed and not emitted into the output. If the input encoding 

2585 is marked with an explicit endianness (e.g. UTF-16-BE), then the BOM is 

2586 interpreted as a non-breaking-space and is preserved in the output (including 

2587 always for UTF-8). 

2588 

2589 The end result is that if the input is marked as an explicit endianness the 

2590 transcoding is faithful to all codepoints in the source. If it is not marked 

2591 with an explicit endianness, the BOM is not considered part of the string itself 

2592 but as metadata, and so is not preserved in the output. 

2593 

2594 Examples: 

2595 

2596 >>> tf.strings.unicode_transcode(["Hello", "TensorFlow", "2.x"], "UTF-8", "UTF-16-BE") 

2597 <tf.Tensor: shape=(3,), dtype=string, numpy= 

2598 array([b'\x00H\x00e\x00l\x00l\x00o', 

2599 b'\x00T\x00e\x00n\x00s\x00o\x00r\x00F\x00l\x00o\x00w', 

2600 b'\x002\x00.\x00x'], dtype=object)> 

2601 >>> tf.strings.unicode_transcode(["A", "B", "C"], "US ASCII", "UTF-8").numpy() 

2602 array([b'A', b'B', b'C'], dtype=object) 

2603 

2604 Args: 

2605 input: A `Tensor` of type `string`. 

2606 The text to be processed. Can have any shape. 

2607 input_encoding: A `string`. 

2608 Text encoding of the input strings. This is any of the encodings supported 

2609 by ICU ucnv algorithmic converters. Examples: `"UTF-16", "US ASCII", "UTF-8"`. 

2610 output_encoding: A `string` from: `"UTF-8", "UTF-16-BE", "UTF-32-BE"`. 

2611 The unicode encoding to use in the output. Must be one of 

2612 `"UTF-8", "UTF-16-BE", "UTF-32-BE"`. Multi-byte encodings will be big-endian. 

2613 errors: An optional `string` from: `"strict", "replace", "ignore"`. Defaults to `"replace"`. 

2614 Error handling policy when there is invalid formatting found in the input. 

2615 The value of 'strict' will cause the operation to produce a InvalidArgument 

2616 error on any invalid input formatting. A value of 'replace' (the default) will 

2617 cause the operation to replace any invalid formatting in the input with the 

2618 `replacement_char` codepoint. A value of 'ignore' will cause the operation to 

2619 skip any invalid formatting in the input and produce no corresponding output 

2620 character. 

2621 replacement_char: An optional `int`. Defaults to `65533`. 

2622 The replacement character codepoint to be used in place of any invalid 

2623 formatting in the input when `errors='replace'`. Any valid unicode codepoint may 

2624 be used. The default value is the default unicode replacement character is 

2625 0xFFFD or U+65533.) 

2626 

2627 Note that for UTF-8, passing a replacement character expressible in 1 byte, such 

2628 as ' ', will preserve string alignment to the source since invalid bytes will be 

2629 replaced with a 1-byte replacement. For UTF-16-BE and UTF-16-LE, any 1 or 2 byte 

2630 replacement character will preserve byte alignment to the source. 

2631 replace_control_characters: An optional `bool`. Defaults to `False`. 

2632 Whether to replace the C0 control characters (00-1F) with the 

2633 `replacement_char`. Default is false. 

2634 name: A name for the operation (optional). 

2635 

2636 Returns: 

2637 A `Tensor` of type `string`. 

2638 """ 

2639 _ctx = _context._context or _context.context() 

2640 tld = _ctx._thread_local_data 

2641 if tld.is_eager: 

2642 try: 

2643 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

2644 _ctx, "UnicodeTranscode", name, input, "input_encoding", 

2645 input_encoding, "output_encoding", output_encoding, "errors", errors, 

2646 "replacement_char", replacement_char, "replace_control_characters", 

2647 replace_control_characters) 

2648 return _result 

2649 except _core._NotOkStatusException as e: 

2650 _ops.raise_from_not_ok_status(e, name) 

2651 except _core._FallbackException: 

2652 pass 

2653 try: 

2654 _result = _dispatcher_for_unicode_transcode( 

2655 (input, input_encoding, output_encoding, errors, replacement_char, 

2656 replace_control_characters, name,), None) 

2657 if _result is not NotImplemented: 

2658 return _result 

2659 return unicode_transcode_eager_fallback( 

2660 input, input_encoding=input_encoding, 

2661 output_encoding=output_encoding, errors=errors, 

2662 replacement_char=replacement_char, 

2663 replace_control_characters=replace_control_characters, name=name, 

2664 ctx=_ctx) 

2665 except _core._SymbolicException: 

2666 pass # Add nodes to the TensorFlow graph. 

2667 except (TypeError, ValueError): 

2668 _result = _dispatch.dispatch( 

2669 unicode_transcode, (), dict(input=input, 

2670 input_encoding=input_encoding, 

2671 output_encoding=output_encoding, 

2672 errors=errors, 

2673 replacement_char=replacement_char, 

2674 replace_control_characters=replace_control_characters, 

2675 name=name) 

2676 ) 

2677 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

2678 return _result 

2679 raise 

2680 else: 

2681 _result = _dispatcher_for_unicode_transcode( 

2682 (input, input_encoding, output_encoding, errors, replacement_char, 

2683 replace_control_characters, name,), None) 

2684 if _result is not NotImplemented: 

2685 return _result 

2686 # Add nodes to the TensorFlow graph. 

2687 input_encoding = _execute.make_str(input_encoding, "input_encoding") 

2688 output_encoding = _execute.make_str(output_encoding, "output_encoding") 

2689 if errors is None: 

2690 errors = "replace" 

2691 errors = _execute.make_str(errors, "errors") 

2692 if replacement_char is None: 

2693 replacement_char = 65533 

2694 replacement_char = _execute.make_int(replacement_char, "replacement_char") 

2695 if replace_control_characters is None: 

2696 replace_control_characters = False 

2697 replace_control_characters = _execute.make_bool(replace_control_characters, "replace_control_characters") 

2698 try: 

2699 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

2700 "UnicodeTranscode", input=input, input_encoding=input_encoding, 

2701 output_encoding=output_encoding, errors=errors, 

2702 replacement_char=replacement_char, 

2703 replace_control_characters=replace_control_characters, 

2704 name=name) 

2705 except (TypeError, ValueError): 

2706 _result = _dispatch.dispatch( 

2707 unicode_transcode, (), dict(input=input, 

2708 input_encoding=input_encoding, 

2709 output_encoding=output_encoding, 

2710 errors=errors, 

2711 replacement_char=replacement_char, 

2712 replace_control_characters=replace_control_characters, 

2713 name=name) 

2714 ) 

2715 if _result is not _dispatch.OpDispatcher.NOT_SUPPORTED: 

2716 return _result 

2717 raise 

2718 _result = _outputs[:] 

2719 if _execute.must_record_gradient(): 

2720 _attrs = ("input_encoding", _op.get_attr("input_encoding"), 

2721 "output_encoding", _op.get_attr("output_encoding"), "errors", 

2722 _op.get_attr("errors"), "replacement_char", 

2723 _op._get_attr_int("replacement_char"), 

2724 "replace_control_characters", 

2725 _op._get_attr_bool("replace_control_characters")) 

2726 _inputs_flat = _op.inputs 

2727 _execute.record_gradient( 

2728 "UnicodeTranscode", _inputs_flat, _attrs, _result) 

2729 _result, = _result 

2730 return _result 

2731 

2732UnicodeTranscode = tf_export("raw_ops.UnicodeTranscode")(_ops.to_raw_op(unicode_transcode)) 

2733_dispatcher_for_unicode_transcode = unicode_transcode._tf_type_based_dispatcher.Dispatch 

2734 

2735 

2736def unicode_transcode_eager_fallback(input, input_encoding, output_encoding, errors, replacement_char, replace_control_characters, name, ctx): 

2737 input_encoding = _execute.make_str(input_encoding, "input_encoding") 

2738 output_encoding = _execute.make_str(output_encoding, "output_encoding") 

2739 if errors is None: 

2740 errors = "replace" 

2741 errors = _execute.make_str(errors, "errors") 

2742 if replacement_char is None: 

2743 replacement_char = 65533 

2744 replacement_char = _execute.make_int(replacement_char, "replacement_char") 

2745 if replace_control_characters is None: 

2746 replace_control_characters = False 

2747 replace_control_characters = _execute.make_bool(replace_control_characters, "replace_control_characters") 

2748 input = _ops.convert_to_tensor(input, _dtypes.string) 

2749 _inputs_flat = [input] 

2750 _attrs = ("input_encoding", input_encoding, "output_encoding", 

2751 output_encoding, "errors", errors, "replacement_char", replacement_char, 

2752 "replace_control_characters", replace_control_characters) 

2753 _result = _execute.execute(b"UnicodeTranscode", 1, inputs=_inputs_flat, 

2754 attrs=_attrs, ctx=ctx, name=name) 

2755 if _execute.must_record_gradient(): 

2756 _execute.record_gradient( 

2757 "UnicodeTranscode", _inputs_flat, _attrs, _result) 

2758 _result, = _result 

2759 return _result 

2760 

2761 

2762def unsorted_segment_join(inputs, segment_ids, num_segments, separator="", name=None): 

2763 r"""TODO: add doc. 

2764 

2765 Args: 

2766 inputs: A `Tensor` of type `string`. 

2767 segment_ids: A `Tensor`. Must be one of the following types: `int32`, `int64`. 

2768 num_segments: A `Tensor`. Must be one of the following types: `int32`, `int64`. 

2769 separator: An optional `string`. Defaults to `""`. 

2770 name: A name for the operation (optional). 

2771 

2772 Returns: 

2773 A `Tensor` of type `string`. 

2774 """ 

2775 _ctx = _context._context or _context.context() 

2776 tld = _ctx._thread_local_data 

2777 if tld.is_eager: 

2778 try: 

2779 _result = pywrap_tfe.TFE_Py_FastPathExecute( 

2780 _ctx, "UnsortedSegmentJoin", name, inputs, segment_ids, num_segments, 

2781 "separator", separator) 

2782 return _result 

2783 except _core._NotOkStatusException as e: 

2784 _ops.raise_from_not_ok_status(e, name) 

2785 except _core._FallbackException: 

2786 pass 

2787 try: 

2788 return unsorted_segment_join_eager_fallback( 

2789 inputs, segment_ids, num_segments, separator=separator, name=name, 

2790 ctx=_ctx) 

2791 except _core._SymbolicException: 

2792 pass # Add nodes to the TensorFlow graph. 

2793 # Add nodes to the TensorFlow graph. 

2794 if separator is None: 

2795 separator = "" 

2796 separator = _execute.make_str(separator, "separator") 

2797 _, _, _op, _outputs = _op_def_library._apply_op_helper( 

2798 "UnsortedSegmentJoin", inputs=inputs, segment_ids=segment_ids, 

2799 num_segments=num_segments, separator=separator, 

2800 name=name) 

2801 _result = _outputs[:] 

2802 if _execute.must_record_gradient(): 

2803 _attrs = ("separator", _op.get_attr("separator"), "Tindices", 

2804 _op._get_attr_type("Tindices"), "Tnumsegments", 

2805 _op._get_attr_type("Tnumsegments")) 

2806 _inputs_flat = _op.inputs 

2807 _execute.record_gradient( 

2808 "UnsortedSegmentJoin", _inputs_flat, _attrs, _result) 

2809 _result, = _result 

2810 return _result 

2811 

2812UnsortedSegmentJoin = tf_export("raw_ops.UnsortedSegmentJoin")(_ops.to_raw_op(unsorted_segment_join)) 

2813 

2814 

2815def unsorted_segment_join_eager_fallback(inputs, segment_ids, num_segments, separator, name, ctx): 

2816 if separator is None: 

2817 separator = "" 

2818 separator = _execute.make_str(separator, "separator") 

2819 _attr_Tindices, (segment_ids,) = _execute.args_to_matching_eager([segment_ids], ctx, [_dtypes.int32, _dtypes.int64, ]) 

2820 _attr_Tnumsegments, (num_segments,) = _execute.args_to_matching_eager([num_segments], ctx, [_dtypes.int32, _dtypes.int64, ], _dtypes.int32) 

2821 inputs = _ops.convert_to_tensor(inputs, _dtypes.string) 

2822 _inputs_flat = [inputs, segment_ids, num_segments] 

2823 _attrs = ("separator", separator, "Tindices", _attr_Tindices, 

2824 "Tnumsegments", _attr_Tnumsegments) 

2825 _result = _execute.execute(b"UnsortedSegmentJoin", 1, inputs=_inputs_flat, 

2826 attrs=_attrs, ctx=ctx, name=name) 

2827 if _execute.must_record_gradient(): 

2828 _execute.record_gradient( 

2829 "UnsortedSegmentJoin", _inputs_flat, _attrs, _result) 

2830 _result, = _result 

2831 return _result 

2832