Skip to content

Commit 1367126

Browse files
author
Yanan Wang
committed
refactor: remove namespace wrapper
1 parent fab6e18 commit 1367126

File tree

16 files changed

+512
-537
lines changed

16 files changed

+512
-537
lines changed

src/Plugins/BotSharp.Plugin.FuzzySharp/BotSharp.Plugin.FuzzySharp.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,4 @@
1818
<ItemGroup>
1919
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
2020
</ItemGroup>
21-
22-
<ItemGroup>
23-
<Folder Include="data\" />
24-
</ItemGroup>
2521
</Project>
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11

2-
namespace BotSharp.Plugin.FuzzySharp.Constants
2+
namespace BotSharp.Plugin.FuzzySharp.Constants;
3+
4+
public static class MatchReason
35
{
4-
public static class MatchReason
5-
{
6-
/// <summary>
7-
/// Token matched a domain term mapping (e.g., HVAC -> Air Conditioning/Heating)
8-
/// </summary>
9-
public const string DomainTermMapping = "domain_term_mapping";
6+
/// <summary>
7+
/// Token matched a domain term mapping (e.g., HVAC -> Air Conditioning/Heating)
8+
/// </summary>
9+
public const string DomainTermMapping = "domain_term_mapping";
1010

11-
/// <summary>
12-
/// Token exactly matched a vocabulary entry
13-
/// </summary>
14-
public const string ExactMatch = "exact_match";
11+
/// <summary>
12+
/// Token exactly matched a vocabulary entry
13+
/// </summary>
14+
public const string ExactMatch = "exact_match";
1515

16-
/// <summary>
17-
/// Token was flagged as a potential typo and a correction was suggested
18-
/// </summary>
19-
public const string TypoCorrection = "typo_correction";
20-
}
16+
/// <summary>
17+
/// Token was flagged as a potential typo and a correction was suggested
18+
/// </summary>
19+
public const string TypoCorrection = "typo_correction";
2120
}
Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11

2-
namespace BotSharp.Plugin.FuzzySharp.Constants
2+
namespace BotSharp.Plugin.FuzzySharp.Constants;
3+
4+
public static class TextConstants
35
{
4-
public static class TextConstants
6+
/// <summary>
7+
/// Characters that need to be separated during tokenization (by adding spaces before and after)
8+
/// Includes: parentheses, brackets, braces, punctuation marks, special symbols, etc.
9+
/// This ensures "(IH)" is split into "(", "IH", ")"
10+
/// </summary>
11+
public static readonly char[] SeparatorChars =
512
{
6-
/// <summary>
7-
/// Characters that need to be separated during tokenization (by adding spaces before and after)
8-
/// Includes: parentheses, brackets, braces, punctuation marks, special symbols, etc.
9-
/// This ensures "(IH)" is split into "(", "IH", ")"
10-
/// </summary>
11-
public static readonly char[] SeparatorChars =
12-
{
13-
// Parentheses and brackets
14-
'(', ')', '[', ']', '{', '}',
15-
// Punctuation marks
16-
',', '.', ';', ':', '!', '?',
17-
// Special symbols
18-
'=', '@', '#', '$', '%', '^', '&', '*', '+', '-', '\\', '|', '<', '>', '~', '`'
19-
};
13+
// Parentheses and brackets
14+
'(', ')', '[', ']', '{', '}',
15+
// Punctuation marks
16+
',', '.', ';', ':', '!', '?',
17+
// Special symbols
18+
'=', '@', '#', '$', '%', '^', '&', '*', '+', '-', '\\', '|', '<', '>', '~', '`'
19+
};
2020

21-
/// <summary>
22-
/// Whitespace characters used as token separators during tokenization.
23-
/// Includes: space, tab, newline, and carriage return.
24-
/// </summary>
25-
public static readonly char[] TokenSeparators =
26-
{
27-
' ', '\t', '\n', '\r'
28-
};
29-
}
21+
/// <summary>
22+
/// Whitespace characters used as token separators during tokenization.
23+
/// Includes: space, tab, newline, and carriage return.
24+
/// </summary>
25+
public static readonly char[] TokenSeparators =
26+
{
27+
' ', '\t', '\n', '\r'
28+
};
3029
}

src/Plugins/BotSharp.Plugin.FuzzySharp/Controllers/FuzzySharpController.cs

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,56 @@
44
using Microsoft.AspNetCore.Mvc;
55
using Microsoft.Extensions.Logging;
66

7-
namespace BotSharp.Plugin.FuzzySharp.Controllers
7+
namespace BotSharp.Plugin.FuzzySharp.Controllers;
8+
9+
[ApiController]
10+
public class FuzzySharpController : ControllerBase
811
{
9-
[ApiController]
10-
public class FuzzySharpController : ControllerBase
11-
{
12-
private readonly IPhraseService _phraseService;
13-
private readonly ILogger<FuzzySharpController> _logger;
12+
private readonly IPhraseService _phraseService;
13+
private readonly ILogger<FuzzySharpController> _logger;
1414

15-
public FuzzySharpController(
16-
IPhraseService phraseService,
17-
ILogger<FuzzySharpController> logger)
18-
{
19-
_phraseService = phraseService;
20-
_logger = logger;
21-
}
15+
public FuzzySharpController(
16+
IPhraseService phraseService,
17+
ILogger<FuzzySharpController> logger)
18+
{
19+
_phraseService = phraseService;
20+
_logger = logger;
21+
}
2222

23-
/// <summary>
24-
/// Analyze text for typos and entities using domain-specific vocabulary.
25-
///
26-
/// Returns:
27-
/// - `original`: Original input text
28-
/// - `tokens`: Tokenized text (only included if `include_tokens=true`)
29-
/// - `flagged`: List of flagged items (each with `match_type`):
30-
/// - `domain_term_mapping` - Business abbreviations (confidence=1.0)
31-
/// - `exact_match` - Exact vocabulary matches (confidence=1.0)
32-
/// - `typo_correction` - Spelling corrections (confidence less than 1.0)
33-
/// - `processing_time_ms`: Processing time in milliseconds
34-
/// </summary>
35-
/// <param name="request">Text analysis request</param>
36-
/// <returns>Text analysis response</returns>
37-
[HttpPost("fuzzy-sharp/analyze-text")]
38-
[ProducesResponseType(typeof(List<SearchPhrasesResult>), StatusCodes.Status200OK)]
39-
[ProducesResponseType(StatusCodes.Status400BadRequest)]
40-
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
41-
public async Task<IActionResult> AnalyzeText([FromBody] string text)
23+
/// <summary>
24+
/// Analyze text for typos and entities using domain-specific vocabulary.
25+
///
26+
/// Returns:
27+
/// - `original`: Original input text
28+
/// - `tokens`: Tokenized text (only included if `include_tokens=true`)
29+
/// - `flagged`: List of flagged items (each with `match_type`):
30+
/// - `domain_term_mapping` - Business abbreviations (confidence=1.0)
31+
/// - `exact_match` - Exact vocabulary matches (confidence=1.0)
32+
/// - `typo_correction` - Spelling corrections (confidence less than 1.0)
33+
/// - `processing_time_ms`: Processing time in milliseconds
34+
/// </summary>
35+
/// <param name="request">Text analysis request</param>
36+
/// <returns>Text analysis response</returns>
37+
[HttpPost("fuzzy-sharp/analyze-text")]
38+
[ProducesResponseType(typeof(List<SearchPhrasesResult>), StatusCodes.Status200OK)]
39+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
40+
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
41+
public async Task<IActionResult> AnalyzeText([FromBody] string text)
42+
{
43+
try
4244
{
43-
try
45+
if (string.IsNullOrWhiteSpace(text))
4446
{
45-
if (string.IsNullOrWhiteSpace(text))
46-
{
47-
return BadRequest(new { error = "Text is required" });
48-
}
49-
50-
var result = await _phraseService.SearchPhrasesAsync(text);
51-
return Ok(result);
52-
}
53-
catch (Exception ex)
54-
{
55-
_logger.LogError(ex, "Error analyzing and searching entities");
56-
return StatusCode(500, new { error = $"Error analyzing and searching entities: {ex.Message}" });
47+
return BadRequest(new { error = "Text is required" });
5748
}
49+
50+
var result = await _phraseService.SearchPhrasesAsync(text);
51+
return Ok(result);
52+
}
53+
catch (Exception ex)
54+
{
55+
_logger.LogError(ex, "Error analyzing and searching entities");
56+
return StatusCode(500, new { error = $"Error analyzing and searching entities: {ex.Message}" });
5857
}
5958
}
6059
}
Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
using BotSharp.Abstraction.FuzzSharp.Models;
22

3-
namespace BotSharp.Abstraction.FuzzSharp
3+
namespace BotSharp.Abstraction.FuzzSharp;
4+
5+
public interface INgramProcessor
46
{
5-
public interface INgramProcessor
6-
{
7-
/// <summary>
8-
/// Process tokens and generate all possible n-gram match results
9-
/// </summary>
10-
/// <param name="tokens">List of tokens to process</param>
11-
/// <param name="vocabulary">Vocabulary (domain type -> vocabulary set)</param>
12-
/// <param name="domainTermMapping">Domain term mapping</param>
13-
/// <param name="lookup">Lookup table (lowercase vocabulary -> (canonical form, domain type list))</param>
14-
/// <param name="maxNgram">Maximum n-gram length</param>
15-
/// <param name="cutoff">Minimum confidence threshold for fuzzy matching</param>
16-
/// <param name="topK">Maximum number of matches to return</param>
17-
/// <returns>List of flagged items</returns>
18-
List<FlaggedItem> ProcessNgrams(
19-
List<string> tokens,
20-
Dictionary<string, HashSet<string>> vocabulary,
21-
Dictionary<string, (string DbPath, string CanonicalForm)> domainTermMapping,
22-
Dictionary<string, (string CanonicalForm, List<string> DomainTypes)> lookup,
23-
int maxNgram,
24-
double cutoff,
25-
int topK);
26-
}
7+
/// <summary>
8+
/// Process tokens and generate all possible n-gram match results
9+
/// </summary>
10+
/// <param name="tokens">List of tokens to process</param>
11+
/// <param name="vocabulary">Vocabulary (domain type -> vocabulary set)</param>
12+
/// <param name="domainTermMapping">Domain term mapping</param>
13+
/// <param name="lookup">Lookup table (lowercase vocabulary -> (canonical form, domain type list))</param>
14+
/// <param name="maxNgram">Maximum n-gram length</param>
15+
/// <param name="cutoff">Minimum confidence threshold for fuzzy matching</param>
16+
/// <param name="topK">Maximum number of matches to return</param>
17+
/// <returns>List of flagged items</returns>
18+
List<FlaggedItem> ProcessNgrams(
19+
List<string> tokens,
20+
Dictionary<string, HashSet<string>> vocabulary,
21+
Dictionary<string, (string DbPath, string CanonicalForm)> domainTermMapping,
22+
Dictionary<string, (string CanonicalForm, List<string> DomainTypes)> lookup,
23+
int maxNgram,
24+
double cutoff,
25+
int topK);
2726
}
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
using BotSharp.Abstraction.FuzzSharp.Models;
22

3-
namespace BotSharp.Abstraction.FuzzSharp
3+
namespace BotSharp.Abstraction.FuzzSharp;
4+
5+
/// <summary>
6+
/// Result processor interface
7+
/// Responsible for processing match results, including deduplication and sorting
8+
/// </summary>
9+
public interface IResultProcessor
410
{
511
/// <summary>
6-
/// Result processor interface
7-
/// Responsible for processing match results, including deduplication and sorting
12+
/// Process a list of flagged items, removing overlapping duplicates and sorting
813
/// </summary>
9-
public interface IResultProcessor
10-
{
11-
/// <summary>
12-
/// Process a list of flagged items, removing overlapping duplicates and sorting
13-
/// </summary>
14-
/// <param name="flagged">List of flagged items to process</param>
15-
/// <returns>Processed list of flagged items (deduplicated and sorted)</returns>
16-
List<FlaggedItem> ProcessResults(List<FlaggedItem> flagged);
17-
}
14+
/// <param name="flagged">List of flagged items to process</param>
15+
/// <returns>Processed list of flagged items (deduplicated and sorted)</returns>
16+
List<FlaggedItem> ProcessResults(List<FlaggedItem> flagged);
1817
}
Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
1-
namespace BotSharp.Abstraction.FuzzSharp
2-
{
3-
public interface ITokenMatcher
4-
{
5-
/// <summary>
6-
/// Try to match a content span and return a match result
7-
/// </summary>
8-
/// <param name="context">The matching context containing all necessary information</param>
9-
/// <returns>Match result if found, null otherwise</returns>
10-
MatchResult? TryMatch(MatchContext context);
11-
12-
/// <summary>
13-
/// Priority of this matcher (higher priority matchers are tried first)
14-
/// </summary>
15-
int Priority { get; }
16-
}
1+
namespace BotSharp.Abstraction.FuzzSharp;
172

3+
public interface ITokenMatcher
4+
{
185
/// <summary>
19-
/// Context information for token matching
6+
/// Try to match a content span and return a match result
207
/// </summary>
21-
public record MatchContext(
22-
string ContentSpan,
23-
string ContentLow,
24-
int StartIndex,
25-
int NgramLength,
26-
Dictionary<string, HashSet<string>> Vocabulary,
27-
Dictionary<string, (string DbPath, string CanonicalForm)> DomainTermMapping,
28-
Dictionary<string, (string CanonicalForm, List<string> DomainTypes)> Lookup,
29-
double Cutoff,
30-
int TopK);
8+
/// <param name="context">The matching context containing all necessary information</param>
9+
/// <returns>Match result if found, null otherwise</returns>
10+
MatchResult? TryMatch(MatchContext context);
3111

3212
/// <summary>
33-
/// Result of a token match
13+
/// Priority of this matcher (higher priority matchers are tried first)
3414
/// </summary>
35-
public record MatchResult(
36-
string CanonicalForm,
37-
List<string> DomainTypes,
38-
string MatchType,
39-
double Confidence);
15+
int Priority { get; }
4016
}
17+
18+
/// <summary>
19+
/// Context information for token matching
20+
/// </summary>
21+
public record MatchContext(
22+
string ContentSpan,
23+
string ContentLow,
24+
int StartIndex,
25+
int NgramLength,
26+
Dictionary<string, HashSet<string>> Vocabulary,
27+
Dictionary<string, (string DbPath, string CanonicalForm)> DomainTermMapping,
28+
Dictionary<string, (string CanonicalForm, List<string> DomainTypes)> Lookup,
29+
double Cutoff,
30+
int TopK);
31+
32+
/// <summary>
33+
/// Result of a token match
34+
/// </summary>
35+
public record MatchResult(
36+
string CanonicalForm,
37+
List<string> DomainTypes,
38+
string MatchType,
39+
double Confidence);

0 commit comments

Comments
 (0)