| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TextNormalizationExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using System.Text; |
| | | 10 | | using MyNet.Primitives; |
| | | 11 | | using MyNet.Text.Normalization; |
| | | 12 | | |
| | | 13 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 14 | | namespace MyNet.Text; |
| | | 15 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 16 | | |
| | | 17 | | public static class TextNormalizationExtensions |
| | | 18 | | { |
| | | 19 | | extension(TextPipeline pipeline) |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Applies a normalization transform to the pipeline. |
| | | 23 | | /// </summary> |
| | | 24 | | public TextPipeline Normalize(ITextNormalizationTransform transform) |
| | | 25 | | { |
| | 6 | 26 | | ArgumentNullException.ThrowIfNull(transform); |
| | 6 | 27 | | return pipeline.Apply(transform); |
| | | 28 | | } |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | extension(string input) |
| | | 32 | | { |
| | | 33 | | /// <summary> |
| | | 34 | | /// Applies a normalization transform. |
| | | 35 | | /// </summary> |
| | | 36 | | public string Normalize(ITextNormalizationTransform transform, CultureInfo? culture = null) |
| | | 37 | | { |
| | 9 | 38 | | ArgumentNullException.ThrowIfNull(transform); |
| | 9 | 39 | | return transform.Apply(input, culture.OrCurrent()); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Normalizes Unicode representation. |
| | | 44 | | /// </summary> |
| | | 45 | | public string NormalizeUnicode(NormalizationForm form, CultureInfo? culture = null) |
| | 3 | 46 | | => input.Normalize(new UnicodeNormalizationTransform(form), culture); |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Removes diacritics from text. |
| | | 50 | | /// </summary> |
| | | 51 | | public string RemoveDiacritics(CultureInfo? culture = null) |
| | 3 | 52 | | => input.Normalize(TextNormalization.RemoveDiacritics, culture); |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Trims and collapses whitespace to single spaces. |
| | | 56 | | /// </summary> |
| | | 57 | | public string NormalizeWhitespace(CultureInfo? culture = null) |
| | 3 | 58 | | => input.Normalize(TextNormalization.CleanWhitespace, culture); |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | |