|
| 1 | +using System.Collections.Generic; |
| 2 | +using Newtonsoft.Json; |
| 3 | + |
| 4 | +namespace Nest |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// A tokenizer that breaks text into terms whenever it encounters a character which is in a defined set. It is mostly useful |
| 8 | + /// for cases where a simple custom tokenization is desired, and the overhead of use of <see cref="PatternTokenizer"/> is not acceptable. |
| 9 | + /// </summary> |
| 10 | + public interface ICharGroupTokenizer : ITokenizer |
| 11 | + { |
| 12 | + /// <summary> |
| 13 | + /// A list containing a list of characters to tokenize the string on. Whenever a character from this list is encountered, a |
| 14 | + /// new token is started. This accepts either single characters like eg. -, or character groups: whitespace, letter, digit, |
| 15 | + /// punctuation, symbol. |
| 16 | + /// </summary> |
| 17 | + [JsonProperty("tokenize_on_chars")] |
| 18 | + IEnumerable<string> TokenizeOnCharacters { get; set; } |
| 19 | + } |
| 20 | + |
| 21 | + /// <inheritdoc cref="ICharGroupTokenizer"/> |
| 22 | + public class CharGroupTokenizer : TokenizerBase, ICharGroupTokenizer |
| 23 | + { |
| 24 | + internal const string TokenizerType = "char_group"; |
| 25 | + |
| 26 | + public CharGroupTokenizer() => this.Type = TokenizerType; |
| 27 | + |
| 28 | + /// <inheritdoc cref="ICharGroupTokenizer.TokenizeOnCharacters"/> |
| 29 | + public IEnumerable<string> TokenizeOnCharacters { get; set; } |
| 30 | + } |
| 31 | + |
| 32 | + /// <inheritdoc cref="ICharGroupTokenizer"/> |
| 33 | + public class CharGroupTokenizerDescriptor |
| 34 | + : TokenizerDescriptorBase<CharGroupTokenizerDescriptor, ICharGroupTokenizer>, ICharGroupTokenizer |
| 35 | + { |
| 36 | + protected override string Type => CharGroupTokenizer.TokenizerType; |
| 37 | + |
| 38 | + IEnumerable<string> ICharGroupTokenizer.TokenizeOnCharacters { get; set; } |
| 39 | + |
| 40 | + /// <inheritdoc cref="ICharGroupTokenizer.TokenizeOnCharacters"/> |
| 41 | + public CharGroupTokenizerDescriptor TokenizeOnCharacters(params string[] characters) => |
| 42 | + Assign(a => a.TokenizeOnCharacters = characters); |
| 43 | + |
| 44 | + /// <inheritdoc cref="ICharGroupTokenizer.TokenizeOnCharacters"/> |
| 45 | + public CharGroupTokenizerDescriptor TokenizeOnCharacters(IEnumerable<string> characters) => |
| 46 | + Assign(a => a.TokenizeOnCharacters = characters); |
| 47 | + } |
| 48 | +} |
0 commit comments