feat: 完成屏蔽词功能。

解决问题(#77)(https://github.com/real-zony/ZonyLrcToolsX/issues/77)
This commit is contained in:
real-zony 2021-06-04 23:15:51 +08:00
parent a7130fd478
commit 0c889272dc
8 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,3 @@
{
"fuckking": "***kking"
}

View File

@ -32,5 +32,10 @@ namespace ZonyLrcTools.Cli.Config
/// 歌词下载器相关的配置属性。
/// </summary>
public IEnumerable<LyricDownloaderOption> LyricDownloaderOptions { get; set; }
/// <summary>
/// 屏蔽词功能相关配置。
/// </summary>
public BlockWordOption BlockWordOptions { get; set; }
}
}

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using ZonyLrcTools.Cli.Config;
using ZonyLrcTools.Cli.Infrastructure.DependencyInject;
namespace ZonyLrcTools.Cli.Infrastructure.Tag
{
public class BlockWordDictionary : IBlockWordDictionary, ISingletonDependency
{
private readonly ToolOptions _options;
private readonly Lazy<Dictionary<string, string>> _wordsDictionary;
public BlockWordDictionary(IOptions<ToolOptions> options)
{
_options = options.Value;
_wordsDictionary = new Lazy<Dictionary<string, string>>(() =>
{
var jsonData = File.ReadAllText(_options.BlockWordOptions.BlockWordDictionaryFile);
return JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonData);
});
}
public string GetValue(string key)
{
if (_wordsDictionary.Value.TryGetValue(key, out var value))
{
return value;
}
return null;
}
}
}

View File

@ -0,0 +1,15 @@
namespace ZonyLrcTools.Cli.Infrastructure.Tag
{
public class BlockWordOption
{
/// <summary>
/// 是否启用本功能。
/// </summary>
public bool IsEnable { get; set; }
/// <summary>
/// 屏蔽词字典文件,用于替换歌曲名或者歌手名称。
/// </summary>
public string BlockWordDictionaryFile { get; set; }
}
}

View File

@ -0,0 +1,7 @@
namespace ZonyLrcTools.Cli.Infrastructure.Tag
{
public interface IBlockWordDictionary
{
string GetValue(string key);
}
}

View File

@ -30,6 +30,10 @@
<Content Include="Resources\error_msg.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Remove="BlockWords.json" />
<Content Include="BlockWords.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

View File

@ -33,6 +33,10 @@
"LyricOption": {
"IsOneLine": true,
"LineBreak": "\n"
},
"BlockWordOptions": {
"IsEnable": false,
"BlockWordDictionaryFile": "BlockWords.json"
}
}
}

View File

@ -0,0 +1,18 @@
using Shouldly;
using Xunit;
using ZonyLrcTools.Cli.Infrastructure.Tag;
namespace ZonyLrcTools.Tests.Infrastructure.Tag
{
public class BlockWordDictionaryTests : TestBase
{
[Fact]
public void GetValue_Test()
{
var dictionary = GetService<IBlockWordDictionary>();
var result = dictionary.GetValue("fuckking");
result.ShouldBe("***kking");
}
}
}