Create a tensor of n-grams based on `data`. Creates a tensor of n-grams based on `data`. The n-grams are created by joining windows of `width` adjacent strings from the inner axis of `data` using `separator`. The input data can be padded on both the start and end of the sequence, if desi
(data,
ngram_width,
separator=" ",
pad_values=None,
padding_width=None,
preserve_short_sequences=False,
name=None)
| 655 | |
| 656 | @tf_export("strings.ngrams") |
| 657 | def ngrams(data, |
| 658 | ngram_width, |
| 659 | separator=" ", |
| 660 | pad_values=None, |
| 661 | padding_width=None, |
| 662 | preserve_short_sequences=False, |
| 663 | name=None): |
| 664 | """Create a tensor of n-grams based on `data`. |
| 665 | |
| 666 | Creates a tensor of n-grams based on `data`. The n-grams are created by |
| 667 | joining windows of `width` adjacent strings from the inner axis of `data` |
| 668 | using `separator`. |
| 669 | |
| 670 | The input data can be padded on both the start and end of the sequence, if |
| 671 | desired, using the `pad_values` argument. If set, `pad_values` should contain |
| 672 | either a tuple of strings or a single string; the 0th element of the tuple |
| 673 | will be used to pad the left side of the sequence and the 1st element of the |
| 674 | tuple will be used to pad the right side of the sequence. The `padding_width` |
| 675 | arg controls how many padding values are added to each side; it defaults to |
| 676 | `ngram_width-1`. |
| 677 | |
| 678 | If this op is configured to not have padding, or if it is configured to add |
| 679 | padding with `padding_width` set to less than ngram_width-1, it is possible |
| 680 | that a sequence, or a sequence plus padding, is smaller than the ngram |
| 681 | width. In that case, no ngrams will be generated for that sequence. This can |
| 682 | be prevented by setting `preserve_short_sequences`, which will cause the op |
| 683 | to always generate at least one ngram per non-empty sequence. |
| 684 | |
| 685 | Args: |
| 686 | data: A Tensor or RaggedTensor containing the source data for the ngrams. |
| 687 | ngram_width: The width(s) of the ngrams to create. If this is a list or |
| 688 | tuple, the op will return ngrams of all specified arities in list order. |
| 689 | Values must be non-Tensor integers greater than 0. |
| 690 | separator: The separator string used between ngram elements. Must be a |
| 691 | string constant, not a Tensor. |
| 692 | pad_values: A tuple of (left_pad_value, right_pad_value), a single string, |
| 693 | or None. If None, no padding will be added; if a single string, then that |
| 694 | string will be used for both left and right padding. Values must be Python |
| 695 | strings. |
| 696 | padding_width: If set, `padding_width` pad values will be added to both |
| 697 | sides of each sequence. Defaults to `ngram_width`-1. Must be greater than |
| 698 | 0. (Note that 1-grams are never padded, regardless of this value.) |
| 699 | preserve_short_sequences: If true, then ensure that at least one ngram is |
| 700 | generated for each input sequence. In particular, if an input sequence is |
| 701 | shorter than `min(ngram_width) + 2*pad_width`, then generate a single |
| 702 | ngram containing the entire sequence. If false, then no ngrams are |
| 703 | generated for these short input sequences. |
| 704 | name: The op name. |
| 705 | |
| 706 | Returns: |
| 707 | A RaggedTensor of ngrams. If `data.shape=[D1...DN, S]`, then |
| 708 | `output.shape=[D1...DN, NUM_NGRAMS]`, where |
| 709 | `NUM_NGRAMS=S-ngram_width+1+2*padding_width`. |
| 710 | |
| 711 | Raises: |
| 712 | TypeError: if `pad_values` is set to an invalid type. |
| 713 | ValueError: if `pad_values`, `padding_width`, or `ngram_width` is set to an |
| 714 | invalid value. |
nothing calls this directly
no test coverage detected