Skip to content

Commit

Permalink
Adding a parametric matching timeout to all Regex instances (#3045)
Browse files Browse the repository at this point in the history
* adding parameterized regex timeout

* removing net462

* adding the missing Swedish type

* - using default when timeout is not assigned to

* refactoring and cleaning up

* more clean ups

* - rebase from the master
- adding net462 back
  • Loading branch information
FARHAD SHAKERIN authored Mar 6, 2023
1 parent 48c71fd commit 760e836
Show file tree
Hide file tree
Showing 467 changed files with 5,399 additions and 6,524 deletions.
Original file line number Diff line number Diff line change
@@ -1,47 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Text.RegularExpressions;

using Microsoft.Recognizers.Definitions.Arabic;
using Microsoft.Recognizers.Text.Choice.Extractors;

namespace Microsoft.Recognizers.Text.Choice.Arabic
{
public class ArabicBooleanExtractorConfiguration : IBooleanExtractorConfiguration
public class ArabicBooleanExtractorConfiguration : BaseBooleanExtractorConfiguration
{
public static readonly Regex TrueRegex =
new Regex(ChoiceDefinitions.TrueRegex, RegexOptions.Singleline | RegexOptions.RightToLeft);

public static readonly Regex FalseRegex =
new Regex(ChoiceDefinitions.FalseRegex, RegexOptions.Singleline | RegexOptions.RightToLeft);

public static readonly Regex TokenRegex =
new Regex(ChoiceDefinitions.TokenizerRegex, RegexOptions.Singleline | RegexOptions.RightToLeft);

public static readonly IDictionary<Regex, string> MapRegexes = new Dictionary<Regex, string>()
{
{ TrueRegex, Constants.SYS_BOOLEAN_TRUE },
{ FalseRegex, Constants.SYS_BOOLEAN_FALSE },
};

public ArabicBooleanExtractorConfiguration(bool onlyTopMatch = true)
: base(
trueRegex: ChoiceDefinitions.TrueRegex,
falseRegex: ChoiceDefinitions.FalseRegex,
tokenRegex: ChoiceDefinitions.TokenizerRegex,
options: RegexOptions.Singleline | RegexOptions.RightToLeft,
allowPartialMatch: false,
maxDistance: 2,
onlyTopMatch)
{
this.OnlyTopMatch = onlyTopMatch;
}

Regex IBooleanExtractorConfiguration.TrueRegex => TrueRegex;

Regex IBooleanExtractorConfiguration.FalseRegex => FalseRegex;

IDictionary<Regex, string> IChoiceExtractorConfiguration.MapRegexes => MapRegexes;

Regex IChoiceExtractorConfiguration.TokenRegex => TokenRegex;

public bool AllowPartialMatch => false;

public int MaxDistance => 2;

public bool OnlyTopMatch { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Text.RegularExpressions;

using Microsoft.Recognizers.Definitions.Bulgarian;
using Microsoft.Recognizers.Text.Choice.Extractors;

namespace Microsoft.Recognizers.Text.Choice.Bulgarian
{
public class BulgarianBooleanExtractorConfiguration : IBooleanExtractorConfiguration
public class BulgarianBooleanExtractorConfiguration : BaseBooleanExtractorConfiguration
{
public static readonly Regex TrueRegex =
new Regex(ChoiceDefinitions.TrueRegex, RegexOptions.Singleline);

public static readonly Regex FalseRegex =
new Regex(ChoiceDefinitions.FalseRegex, RegexOptions.Singleline);

public static readonly Regex TokenRegex =
new Regex(ChoiceDefinitions.TokenizerRegex, RegexOptions.Singleline);

public static readonly IDictionary<Regex, string> MapRegexes = new Dictionary<Regex, string>()
{
{ TrueRegex, Constants.SYS_BOOLEAN_TRUE },
{ FalseRegex, Constants.SYS_BOOLEAN_FALSE },
};

public BulgarianBooleanExtractorConfiguration(bool onlyTopMatch = true)
: base(
trueRegex: ChoiceDefinitions.TrueRegex,
falseRegex: ChoiceDefinitions.FalseRegex,
tokenRegex: ChoiceDefinitions.TokenizerRegex,
options: RegexOptions.Singleline,
allowPartialMatch: false,
maxDistance: 2,
onlyTopMatch)
{
this.OnlyTopMatch = onlyTopMatch;
}

Regex IBooleanExtractorConfiguration.TrueRegex => TrueRegex;

Regex IBooleanExtractorConfiguration.FalseRegex => FalseRegex;

IDictionary<Regex, string> IChoiceExtractorConfiguration.MapRegexes => MapRegexes;

Regex IChoiceExtractorConfiguration.TokenRegex => TokenRegex;

public bool AllowPartialMatch => false;

public int MaxDistance => 2;

public bool OnlyTopMatch { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Text.RegularExpressions;

using Microsoft.Recognizers.Definitions.Chinese;
using Microsoft.Recognizers.Text.Choice.Extractors;

namespace Microsoft.Recognizers.Text.Choice.Chinese
{
public class ChineseBooleanExtractorConfiguration : IBooleanExtractorConfiguration
public class ChineseBooleanExtractorConfiguration : BaseBooleanExtractorConfiguration
{
public static readonly Regex TrueRegex =
new Regex(ChoiceDefinitions.TrueRegex, RegexOptions.Singleline);

public static readonly Regex FalseRegex =
new Regex(ChoiceDefinitions.FalseRegex, RegexOptions.Singleline);

public static readonly Regex TokenRegex =
new Regex(ChoiceDefinitions.TokenizerRegex, RegexOptions.Singleline);

public static readonly IDictionary<Regex, string> MapRegexes = new Dictionary<Regex, string>()
{
{ TrueRegex, Constants.SYS_BOOLEAN_TRUE },
{ FalseRegex, Constants.SYS_BOOLEAN_FALSE },
};

public ChineseBooleanExtractorConfiguration(bool onlyTopMatch = true)
: base(
trueRegex: ChoiceDefinitions.TrueRegex,
falseRegex: ChoiceDefinitions.FalseRegex,
tokenRegex: ChoiceDefinitions.TokenizerRegex,
options: RegexOptions.Singleline,
allowPartialMatch: false,
maxDistance: 2,
onlyTopMatch)
{
this.OnlyTopMatch = onlyTopMatch;
}

Regex IBooleanExtractorConfiguration.TrueRegex => TrueRegex;

Regex IBooleanExtractorConfiguration.FalseRegex => FalseRegex;

IDictionary<Regex, string> IChoiceExtractorConfiguration.MapRegexes => MapRegexes;

Regex IChoiceExtractorConfiguration.TokenRegex => TokenRegex;

public bool AllowPartialMatch => false;

public int MaxDistance => 2;

public bool OnlyTopMatch { get; }
}
}
26 changes: 18 additions & 8 deletions .NET/Microsoft.Recognizers.Text.Choice/ChoiceRecognizer.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using Microsoft.Recognizers.Text.Choice.Arabic;
using Microsoft.Recognizers.Text.Choice.Bulgarian;
using Microsoft.Recognizers.Text.Choice.Chinese;
using Microsoft.Recognizers.Text.Choice.Dutch;
using Microsoft.Recognizers.Text.Choice.English;
using Microsoft.Recognizers.Text.Choice.Extractors;
using Microsoft.Recognizers.Text.Choice.French;
using Microsoft.Recognizers.Text.Choice.German;
using Microsoft.Recognizers.Text.Choice.Hindi;
Expand All @@ -21,23 +23,23 @@ namespace Microsoft.Recognizers.Text.Choice
{
public class ChoiceRecognizer : Recognizer<ChoiceOptions>
{
public ChoiceRecognizer(string targetCulture, ChoiceOptions options = ChoiceOptions.None, bool lazyInitialization = false)
: base(targetCulture, options, lazyInitialization)
public ChoiceRecognizer(string targetCulture, ChoiceOptions options = ChoiceOptions.None, bool lazyInitialization = false, int timeoutInSeconds = 0)
: base(targetCulture, options, lazyInitialization, timeoutInSeconds)
{
}

public ChoiceRecognizer(string targetCulture, int options, bool lazyInitialization = false)
: this(targetCulture, GetOptions(options), lazyInitialization)
public ChoiceRecognizer(string targetCulture, int options, bool lazyInitialization = false, int timeoutInSeconds = 0)
: this(targetCulture, GetOptions(options), lazyInitialization, timeoutInSeconds)
{
}

public ChoiceRecognizer(ChoiceOptions options = ChoiceOptions.None, bool lazyInitialization = true)
: base(null, options, lazyInitialization)
public ChoiceRecognizer(ChoiceOptions options = ChoiceOptions.None, bool lazyInitialization = true, int timeoutInSeconds = 0)
: base(null, options, lazyInitialization, timeoutInSeconds)
{
}

public ChoiceRecognizer(int options, bool lazyInitialization = true)
: this(null, GetOptions(options), lazyInitialization)
public ChoiceRecognizer(int options, bool lazyInitialization = true, int timeoutInSeconds = 0)
: this(null, GetOptions(options), lazyInitialization, timeoutInSeconds)
{
}

Expand All @@ -53,6 +55,14 @@ public IModel GetBooleanModel(string culture = null, bool fallbackToDefaultCultu
return GetModel<BooleanModel>(culture, fallbackToDefaultCulture);
}

protected override List<Type> GetRelatedTypes()
{
return new List<Type>()
{
typeof(BaseBooleanExtractorConfiguration),
};
}

protected override void InitializeConfiguration()
{
RegisterModel<BooleanModel>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Text.RegularExpressions;

using Microsoft.Recognizers.Definitions.Dutch;
using Microsoft.Recognizers.Text.Choice.Extractors;

namespace Microsoft.Recognizers.Text.Choice.Dutch
{
public class DutchBooleanExtractorConfiguration : IBooleanExtractorConfiguration
public class DutchBooleanExtractorConfiguration : BaseBooleanExtractorConfiguration
{
public static readonly Regex TrueRegex =
new Regex(ChoiceDefinitions.TrueRegex, RegexOptions.Singleline);

public static readonly Regex FalseRegex =
new Regex(ChoiceDefinitions.FalseRegex, RegexOptions.Singleline);

public static readonly Regex TokenRegex =
new Regex(ChoiceDefinitions.TokenizerRegex, RegexOptions.Singleline);

public static readonly IDictionary<Regex, string> MapRegexes = new Dictionary<Regex, string>()
{
{ TrueRegex, Constants.SYS_BOOLEAN_TRUE },
{ FalseRegex, Constants.SYS_BOOLEAN_FALSE },
};

public DutchBooleanExtractorConfiguration(bool onlyTopMatch = true)
: base(
trueRegex: ChoiceDefinitions.TrueRegex,
falseRegex: ChoiceDefinitions.FalseRegex,
tokenRegex: ChoiceDefinitions.TokenizerRegex,
options: RegexOptions.Singleline,
allowPartialMatch: false,
maxDistance: 2,
onlyTopMatch)
{
this.OnlyTopMatch = onlyTopMatch;
}

Regex IBooleanExtractorConfiguration.TrueRegex => TrueRegex;

Regex IBooleanExtractorConfiguration.FalseRegex => FalseRegex;

IDictionary<Regex, string> IChoiceExtractorConfiguration.MapRegexes => MapRegexes;

Regex IChoiceExtractorConfiguration.TokenRegex => TokenRegex;

public bool AllowPartialMatch => false;

public int MaxDistance => 2;

public bool OnlyTopMatch { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Text.RegularExpressions;

using Microsoft.Recognizers.Definitions.English;
using Microsoft.Recognizers.Text.Choice.Extractors;

namespace Microsoft.Recognizers.Text.Choice.English
{
public class EnglishBooleanExtractorConfiguration : IBooleanExtractorConfiguration
public class EnglishBooleanExtractorConfiguration : BaseBooleanExtractorConfiguration
{
public static readonly Regex TrueRegex =
new Regex(ChoiceDefinitions.TrueRegex, RegexOptions.Singleline);

public static readonly Regex FalseRegex =
new Regex(ChoiceDefinitions.FalseRegex, RegexOptions.Singleline);

public static readonly Regex TokenRegex =
new Regex(ChoiceDefinitions.TokenizerRegex, RegexOptions.Singleline);

public static readonly IDictionary<Regex, string> MapRegexes = new Dictionary<Regex, string>()
{
{ TrueRegex, Constants.SYS_BOOLEAN_TRUE },
{ FalseRegex, Constants.SYS_BOOLEAN_FALSE },
};

public EnglishBooleanExtractorConfiguration(bool onlyTopMatch = true)
: base(
trueRegex: ChoiceDefinitions.TrueRegex,
falseRegex: ChoiceDefinitions.FalseRegex,
tokenRegex: ChoiceDefinitions.TokenizerRegex,
options: RegexOptions.Singleline,
allowPartialMatch: false,
maxDistance: 2,
onlyTopMatch)
{
this.OnlyTopMatch = onlyTopMatch;
}

Regex IBooleanExtractorConfiguration.TrueRegex => TrueRegex;

Regex IBooleanExtractorConfiguration.FalseRegex => FalseRegex;

IDictionary<Regex, string> IChoiceExtractorConfiguration.MapRegexes => MapRegexes;

Regex IChoiceExtractorConfiguration.TokenRegex => TokenRegex;

public bool AllowPartialMatch => false;

public int MaxDistance => 2;

public bool OnlyTopMatch { get; }
}
}
Loading

0 comments on commit 760e836

Please sign in to comment.