Skip to content

Commit

Permalink
Update packages
Browse files Browse the repository at this point in the history
  • Loading branch information
ZOXEXIVO committed Nov 4, 2024
1 parent 9593f7a commit 39a38b5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/Backend/Geen.Core/Geen.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
</ItemGroup>
</Project>
121 changes: 57 additions & 64 deletions src/Backend/Geen.Core/Services/Text/ContentService.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Geen.Core.Domains.Mentions;
using Geen.Core.Models.Content;
using Geen.Core.Services.Interfaces;
using Geen.Core.Services.Text.Extensions;

namespace Geen.Core.Services.Text;

Expand All @@ -16,7 +14,7 @@ public interface IContentService
string GenerateBasicTitle(MentionModel mention);
}

public class ContentService : IContentService
public partial class ContentService : IContentService
{
private readonly IClubCacheRepository _clubCacheRepository;
private readonly IPlayerCacheRepository _playerCacheRepository;
Expand All @@ -30,126 +28,121 @@ public ContentService(IPlayerCacheRepository playerCacheRepository,

public ContentProcessingResult Process(string text, ContentContext context)
{
var cleanedText = text.Trim().AsText();
var cleanedText = text.AsSpan().Trim().ToString();

var builder = new StringBuilder(cleanedText);

var words = cleanedText
.Split(new[] { ' ', ',', '.', '(', ')', '\n', '-', '/', '\\', '<', '>' },
StringSplitOptions.RemoveEmptyEntries)
var words = cleanedText.Split(' ', ',', '.', '(', ')', '\n', '-', '/', '\\', '<', '>')
.Where(x => x.Length > 3);

var result = new ContentProcessingResult();

var replacedWords = new HashSet<string>();

var playerReplacements = new Dictionary<string, string>();
var clubReplacements = new Dictionary<string, string>();

foreach (var word in words)
{
if (replacedWords.Contains(word))
continue;

var loweredWord = word.ToLower();
var loweredWord = word.ToString().ToLowerInvariant();

var playerInfo = _playerCacheRepository.GetPlayerUrl(loweredWord, context);
if (!string.IsNullOrWhiteSpace(playerInfo.Url))
{
builder.Replace(word, $"<a href=\"/player/{playerInfo.Url}\" target=\"blank\">{word}</a>");
playerReplacements[word.ToString()] = $"<a href=\"/player/{playerInfo.Url}\" target=\"blank\">{word}</a>";
result.PlayerIds.Add(playerInfo.Id);

replacedWords.Add(word);
replacedWords.Add(word.ToString());
}

var clubInfo = _clubCacheRepository.GetClubUrl(loweredWord, context);
if (!string.IsNullOrWhiteSpace(clubInfo.Url))
{
builder.Replace(word, $"<a href=\"/club/{clubInfo.Url}\" target=\"blank\">{word}</a>");
clubReplacements[word.ToString()] = $"<a href=\"/club/{clubInfo.Url}\" target=\"blank\">{word}</a>";
result.ClubIds.Add(clubInfo.Id);

replacedWords.Add(word);
replacedWords.Add(word.ToString());
}
}

result.Text = builder.ToString();
var pattern = string.Join("|", playerReplacements.Keys.Concat(clubReplacements.Keys).Select(Regex.Escape));
result.Text = Regex.Replace(cleanedText, pattern, match =>
{
if (playerReplacements.TryGetValue(match.Value, out var playerReplacement))
return playerReplacement;
if (clubReplacements.TryGetValue(match.Value, out var clubReplacement))
return clubReplacement;
return match.Value;
});

return result;
}

public string GenerateBasicTitle(MentionModel mention)
{
if (mention.Player != null) return GenerateTitleForPlayer(mention);

if (mention.Club != null) return GenerateTitleForClub(mention);
public string GenerateBasicTitle(MentionModel mention) =>
mention.Player != null ? GenerateTitleForPlayer(mention) :
mention.Club != null ? GenerateTitleForClub(mention) : string.Empty;

return string.Empty;
}
private string GenerateTitleForPlayer(MentionModel playerMention) => ExtractPhrase(playerMention.Text);

private string GenerateTitleForPlayer(MentionModel playerMention)
{
return ExtractPhraze(playerMention.Text);
}
private string GenerateTitleForClub(MentionModel clubMention) => ExtractPhrase(clubMention.Text);

private string GenerateTitleForClub(MentionModel clubMention)
{
return ExtractPhraze(clubMention.Text);
}

private string ExtractPhraze(string text)
private string ExtractPhrase(string text)
{
var cleanText = StripTextFromTags(text);
var wordSplittedText = cleanText.Split(' ', StringSplitOptions.RemoveEmptyEntries);

var wordSplittedText = cleanText.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

if (wordSplittedText.Length > 9)
return ClearTailFromNonAplha(string.Join(" ", wordSplittedText.Take(9)));

if (char.IsLower(cleanText.First()))
return MakeFirstLetterUppercase(cleanText);

return ClearTailFromNonAplha(cleanText);
return wordSplittedText.Length > 9 ? ClearTailFromNonAlpha(string.Concat(wordSplittedText.Take(9))) :
char.IsLower(cleanText[0]) ? char.ToUpperInvariant(cleanText[0]) + cleanText[1..] : ClearTailFromNonAlpha(cleanText);
}

private static string MakeFirstLetterUppercase(string text)
private static string ClearTailFromNonAlpha(string text)
{
return text.First().ToString().ToUpper() + text.Substring(1);
}
var span = text.AsSpan();
var lastAlphaIndex = span.Length - 1;

private static string ClearTailFromNonAplha(string text)
{
var lastAlphaIndex = text.Length - 1;

while (lastAlphaIndex >= 1 && !char.IsLetterOrDigit(text[lastAlphaIndex]))
while (lastAlphaIndex >= 1 && !char.IsLetterOrDigit(span[lastAlphaIndex]))
lastAlphaIndex--;

return text.Substring(0, lastAlphaIndex + 1);
return span[..(lastAlphaIndex + 1)].ToString();
}

private static readonly Regex TagRegex = MyTagRegex();
private static readonly Regex WhitespaceRegex = MyWhitespaceRegex();
private static readonly Regex NewlineRegex = MyNewlineRegex();
private static readonly Regex MultiNewlineRegex = MyMultiNewlineRegex();

private string StripTextFromTags(string text)
{
if (string.IsNullOrWhiteSpace(text))
return string.Empty;

var s = Regex.Replace(text, @"<(.|\n)*?>", string.Empty);

s = s.Replace("&nbsp;", " ");
var stripped = TagRegex.Replace(text, string.Empty).Replace("&nbsp;", " ");
stripped = WhitespaceRegex.Replace(stripped, " ");
stripped = NewlineRegex.Replace(stripped, " ");
stripped = MultiNewlineRegex.Replace(stripped, " ");

s = Regex.Replace(s, @"\s+", " ");
s = Regex.Replace(s, @"\r\n", " ");
s = Regex.Replace(s, @"\n+", " ");

return s;
return stripped;
}

[GeneratedRegex(@"<(.|\n)*?>", RegexOptions.Compiled)]
private static partial Regex MyTagRegex();
[GeneratedRegex(@"\s+", RegexOptions.Compiled)]
private static partial Regex MyWhitespaceRegex();
[GeneratedRegex(@"\r\n", RegexOptions.Compiled)]
private static partial Regex MyNewlineRegex();
[GeneratedRegex(@"\n+", RegexOptions.Compiled)]
private static partial Regex MyMultiNewlineRegex();
}

public class ContentProcessingResult
{
public ContentProcessingResult()
{
PlayerIds = new SortedSet<int>();
ClubIds = new SortedSet<int>();
PlayerIds = new HashSet<int>();
ClubIds = new HashSet<int>();
}

public string Text { get; set; }

public SortedSet<int> PlayerIds { get; set; }
public SortedSet<int> ClubIds { get; set; }
public HashSet<int> PlayerIds { get; set; }
public HashSet<int> ClubIds { get; set; }
}
8 changes: 4 additions & 4 deletions src/Backend/Geen.Data/Geen.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Mapster" Version="7.4.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="MongoDB.Driver" Version="2.28.0" />
<PackageReference Include="StackExchange.Redis" Version="2.8.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="MongoDB.Driver" Version="3.0.0" />
<PackageReference Include="StackExchange.Redis" Version="2.8.16" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Geen.Core\Geen.Core.csproj"/>
Expand Down
2 changes: 1 addition & 1 deletion src/Backend/Geen.Jobs/Geen.Jobs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
</ItemGroup>

</Project>
6 changes: 3 additions & 3 deletions src/Backend/Geen.Web/Geen.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.7.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.7.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.7.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.9.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.9.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.9.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Geen.Core\Geen.Core.csproj" />
Expand Down

0 comments on commit 39a38b5

Please sign in to comment.