diff --git a/src/OpenCCNET/ZhConverter.cs b/src/OpenCCNET/ZhConverter.cs
index 4659280..c4ce35d 100644
--- a/src/OpenCCNET/ZhConverter.cs
+++ b/src/OpenCCNET/ZhConverter.cs
@@ -6,15 +6,6 @@ namespace OpenCCNET
{
public static partial class ZhConverter
{
- ///
- /// 初始化
- ///
- public static void Initialize()
- {
- ZhSegment.Initialize();
- ZhDictionary.Initialize();
- }
-
#region 简体中文
///
diff --git a/src/OpenCCNET/ZhDictionary.cs b/src/OpenCCNET/ZhDictionary.cs
index 72f1b53..91a1850 100644
--- a/src/OpenCCNET/ZhDictionary.cs
+++ b/src/OpenCCNET/ZhDictionary.cs
@@ -13,114 +13,111 @@ public static class ZhDictionary
///
/// 简体中文=>繁体中文(OpenCC标准)单字转换字典
///
- public static IDictionary STCharacters { get; set; }
+ public static IDictionary STCharacters => _STCharacters.Value;
///
/// 简体中文=>繁体中文(OpenCC标准)词汇转换字典
///
- public static IDictionary STPhrases { get; set; }
+ public static IDictionary STPhrases => _STPhrases.Value;
///
/// 繁体中文(OpenCC标准)=>简体中文单字转换字典
///
- public static IDictionary TSCharacters { get; set; }
+ public static IDictionary TSCharacters => _TSCharacters.Value;
///
/// 繁体中文(OpenCC标准)=>简体中文词汇转换字典
///
- public static IDictionary TSPhrases { get; set; }
+ public static IDictionary TSPhrases => _TSPhrases.Value;
///
/// 繁体中文(OpenCC标准)=>繁体中文(台湾)单字转换字典
///
- public static IDictionary TWVariants { get; set; }
+ public static IDictionary TWVariants => _TWVariants.Value;
///
/// 繁体中文(OpenCC标准)=>繁体中文(台湾)词汇转换字典
///
- public static IDictionary TWPhrases { get; set; }
+ public static IDictionary TWPhrases => _TWPhrases.Value;
///
/// 繁体中文(台湾)=>繁体中文(OpenCC标准)单字转换字典
///
- public static IDictionary TWVariantsRev { get; set; }
+ public static IDictionary TWVariantsRev => _TWVariantsRev.Value;
///
/// 繁体中文(台湾)=>繁体中文(OpenCC标准)异体字词汇转换字典
///
- public static IDictionary TWVariantsRevPhrases { get; set; }
+ public static IDictionary TWVariantsRevPhrases => _TWVariantsRevPhrases.Value;
///
/// 繁体中文(台湾)=>繁体中文(OpenCC标准)词汇转换字典
///
- public static IDictionary TWPhrasesRev { get; set; }
+ public static IDictionary TWPhrasesRev => _TWPhrasesRev.Value;
///
/// 繁体中文(OpenCC标准)=>繁体中文(香港)单字转换字典
///
- public static IDictionary HKVariants { get; set; }
+ public static IDictionary HKVariants => _HKVariants.Value;
///
/// 繁体中文(香港)=>繁体中文(OpenCC标准)单字转换字典
///
- public static IDictionary HKVariantsRev { get; set; }
+ public static IDictionary HKVariantsRev => _HKVariantsRev.Value;
///
/// 繁体中文(香港)=>繁体中文(OpenCC标准)异体字词汇转换字典
///
- public static IDictionary HKVariantsRevPhrases { get; set; }
+ public static IDictionary HKVariantsRevPhrases => _HKVariantsRevPhrases.Value;
///
/// 日语(旧字体)=>日语(新字体)单字转换字典
///
- public static IDictionary JPVariants { get; set; }
+ public static IDictionary JPVariants => _JPVariants.Value;
///
/// 日语(新字体)=>日语(旧字体)单字转换字典
///
- public static IDictionary JPVariantsRev { get; set; }
+ public static IDictionary JPVariantsRev => _JPVariantsRev.Value;
///
/// 日语(新字体)=>日语(旧字体)异体字单字转换字典
///
- public static IDictionary JPShinjitaiCharacters { get; set; }
+ public static IDictionary JPShinjitaiCharacters => _JPShinjitaiCharacters.Value;
///
/// 日语(新字体)=>日语(旧字体)异体字词汇转换字典
///
- public static IDictionary JPShinjitaiPhrases { get; set; }
+ public static IDictionary JPShinjitaiPhrases => _JPShinjitaiPhrases.Value;
- ///
- /// 加载所有字典文件
- ///
- public static void Initialize()
+ static ZhDictionary()
{
- using var zipArchive = new ZipArchive(typeof(ZhDictionary).Assembly.GetManifestResourceStream("Dictionary.zip") ?? throw new InvalidOperationException("Missing dictionary zip file."));
- STCharacters = LoadDictionary(zipArchive, "STCharacters");
- STPhrases = LoadDictionary(zipArchive, "STPhrases");
- TSCharacters = LoadDictionary(zipArchive, "TSCharacters");
- TSPhrases = LoadDictionary(zipArchive, "TSPhrases");
- TWVariants = LoadDictionary(zipArchive, "TWVariants");
- TWPhrases = LoadDictionary(zipArchive, "TWPhrasesIT", "TWPhrasesName", "TWPhrasesOther");
- TWVariantsRev = LoadDictionaryReversed(zipArchive, "TWVariants");
- TWVariantsRevPhrases = LoadDictionary(zipArchive, "TWVariantsRevPhrases");
- TWPhrasesRev = LoadDictionaryReversed(zipArchive, "TWPhrasesIT", "TWPhrasesName", "TWPhrasesOther");
- HKVariants = LoadDictionary(zipArchive, "HKVariants");
- HKVariantsRev = LoadDictionaryReversed(zipArchive, "HKVariants");
- HKVariantsRevPhrases = LoadDictionary(zipArchive, "HKVariantsRevPhrases");
- JPVariants = LoadDictionary(zipArchive, "JPVariants");
- JPVariantsRev = LoadDictionaryReversed(zipArchive, "JPVariants");
- JPShinjitaiCharacters = LoadDictionary(zipArchive, "JPShinjitaiCharacters");
- JPShinjitaiPhrases = LoadDictionary(zipArchive, "JPShinjitaiPhrases");
+ _STCharacters = new(() => LoadDictionary("STCharacters"));
+ _STPhrases = new(() => LoadDictionary("STPhrases"));
+ _TSCharacters = new(() => LoadDictionary("TSCharacters"));
+ _TSPhrases = new(() => LoadDictionary("TSPhrases"));
+ _TWVariants = new(() => LoadDictionary("TWVariants"));
+ _TWPhrases = new(() => LoadDictionary("TWPhrasesIT", "TWPhrasesName", "TWPhrasesOther"));
+ _TWVariantsRev = new(() => LoadDictionaryReversed("TWVariants"));
+ _TWVariantsRevPhrases = new(() => LoadDictionary("TWVariantsRevPhrases"));
+ _TWPhrasesRev = new(() => LoadDictionaryReversed("TWPhrasesIT", "TWPhrasesName", "TWPhrasesOther"));
+ _HKVariants = new(() => LoadDictionary("HKVariants"));
+ _HKVariantsRev = new(() => LoadDictionaryReversed("HKVariants"));
+ _HKVariantsRevPhrases = new(() => LoadDictionary("HKVariantsRevPhrases"));
+ _JPVariants = new(() => LoadDictionary("JPVariants"));
+ _JPVariantsRev = new(() => LoadDictionaryReversed("JPVariants"));
+ _JPShinjitaiCharacters = new(() => LoadDictionary("JPShinjitaiCharacters"));
+ _JPShinjitaiPhrases = new(() => LoadDictionary("JPShinjitaiPhrases"));
}
///
/// 加载字典文件
///
/// 字典名称
- private static IDictionary LoadDictionary(ZipArchive zipArchive, params string[] dictionaryNames)
+ private static IDictionary LoadDictionary(params string[] dictionaryNames)
{
+ using var zipArchive = new ZipArchive(typeof(ZhDictionary).Assembly.GetManifestResourceStream("Dictionary.zip") ?? throw new InvalidOperationException("Missing dictionary zip file."));
var dictionaryPaths = dictionaryNames.Select(name => $"{name}.txt")
.ToList();
var dictionary = new Dictionary();
@@ -144,8 +141,9 @@ private static IDictionary LoadDictionary(ZipArchive zipArchive,
/// 反向加载字典文件
///
/// 字典名称
- private static IDictionary LoadDictionaryReversed(ZipArchive zipArchive, params string[] dictionaryNames)
+ private static IDictionary LoadDictionaryReversed(params string[] dictionaryNames)
{
+ using var zipArchive = new ZipArchive(typeof(ZhDictionary).Assembly.GetManifestResourceStream("Dictionary.zip") ?? throw new InvalidOperationException("Missing dictionary zip file."));
var dictionaryPaths = dictionaryNames.Select(name => $"{name}.txt");
var dictionary = new Dictionary();
foreach (var path in dictionaryPaths)
@@ -166,6 +164,23 @@ private static IDictionary LoadDictionaryReversed(ZipArchive zip
return dictionary;
}
+
+ private static readonly Lazy> _STCharacters;
+ private static readonly Lazy> _STPhrases;
+ private static readonly Lazy> _TSCharacters;
+ private static readonly Lazy> _TSPhrases;
+ private static readonly Lazy> _TWVariants;
+ private static readonly Lazy> _TWPhrases;
+ private static readonly Lazy> _TWVariantsRev;
+ private static readonly Lazy> _TWVariantsRevPhrases;
+ private static readonly Lazy> _TWPhrasesRev;
+ private static readonly Lazy> _HKVariants;
+ private static readonly Lazy> _HKVariantsRev;
+ private static readonly Lazy> _HKVariantsRevPhrases;
+ private static readonly Lazy> _JPVariants;
+ private static readonly Lazy> _JPVariantsRev;
+ private static readonly Lazy> _JPShinjitaiCharacters;
+ private static readonly Lazy> _JPShinjitaiPhrases;
}
}
}
\ No newline at end of file
diff --git a/src/OpenCCNET/ZhSegment.cs b/src/OpenCCNET/ZhSegment.cs
index f72557d..8808925 100644
--- a/src/OpenCCNET/ZhSegment.cs
+++ b/src/OpenCCNET/ZhSegment.cs
@@ -18,12 +18,6 @@ public static class ZhSegment
///
public static JiebaSegmenter Jieba = new JiebaSegmenter();
- public static void Initialize()
- {
- // 通过调用一次jieba分词来提前加载所需资源
- Jieba.Cut(string.Empty);
- }
-
///
/// 利用jieba.NET分词
///
diff --git a/tests/OpenCCNET.Tests/ZhConverterTests.cs b/tests/OpenCCNET.Tests/ZhConverterTests.cs
index c88a5fa..04a8d0e 100644
--- a/tests/OpenCCNET.Tests/ZhConverterTests.cs
+++ b/tests/OpenCCNET.Tests/ZhConverterTests.cs
@@ -1,16 +1,10 @@
-using NUnit.Framework;
+using NUnit.Framework;
namespace OpenCCNET.Tests
{
[TestFixture]
internal class ZhConverterTests
{
- [OneTimeSetUp]
- public void SetUp()
- {
- ZhConverter.Initialize();
- }
-
[Test]
public void HansToFromTw()
{