Skip to content

Commit aad8934

Browse files
committed
chore: 调整生成逻辑; 调整资源嵌入逻辑;
1 parent 2090f30 commit aad8934

File tree

9 files changed

+60
-223
lines changed

9 files changed

+60
-223
lines changed

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Ignore Visual Studio temporary files, build results, and
1+
## Ignore Visual Studio temporary files, build results, and
22
## files generated by popular Visual Studio add-ons.
33
##
44
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
@@ -360,4 +360,6 @@ MigrationBackup/
360360
.ionide/
361361

362362
# Fody - auto-generated XML schema
363-
FodyWeavers.xsd
363+
FodyWeavers.xsd
364+
365+
/src/LanguageIdentification/Resources/langid-model-data

Directory.Build.props

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<Project>
22
<PropertyGroup>
3-
<TargetFrameworks>net8.0;net9.0;netstandard2.0</TargetFrameworks>
4-
53
<Nullable>enable</Nullable>
64
<LangVersion>latest</LangVersion>
75
<IsPackable>false</IsPackable>

src/LanguageIdentification/LanguageIdentification.csproj

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<VersionPrefix>1.1.2</VersionPrefix>
3+
<TargetFrameworks>net8.0;net9.0;netstandard2.0</TargetFrameworks>
4+
5+
<VersionPrefix>1.2.0</VersionPrefix>
46
<IsPackable>true</IsPackable>
57
<Description>.NET Port of Language Identification Library for langid-java. 移植自langid-java的语言识别库。</Description>
68

@@ -18,26 +20,23 @@
1820
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1921

2022
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
23+
24+
<IsAotCompatible>true</IsAotCompatible>
25+
<InvariantGlobalization>true</InvariantGlobalization>
26+
<NoWarn>$(NoWarn);NETSDK1210</NoWarn>
2127
</PropertyGroup>
2228

2329
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
2430
<PackageReference Include="System.Memory" Version="4.5.5" />
2531
</ItemGroup>
2632

33+
<ItemGroup>
34+
<EmbeddedResource Include="Resources\langid-model-data" />
35+
</ItemGroup>
36+
2737
<ItemGroup>
2838
<None Include="..\..\readme.md" Link="readme.md" Pack="true" PackagePath="/" />
2939

30-
<Compile Update="Resource.Designer.cs">
31-
<DesignTime>True</DesignTime>
32-
<AutoGen>True</AutoGen>
33-
<DependentUpon>Resource.resx</DependentUpon>
34-
</Compile>
35-
36-
<EmbeddedResource Update="Resource.resx">
37-
<Generator>ResXFileCodeGenerator</Generator>
38-
<LastGenOutput>Resource.Designer.cs</LastGenOutput>
39-
</EmbeddedResource>
40-
4140
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
4241
<_Parameter1>LanguageIdentification.Test</_Parameter1>
4342
</AssemblyAttribute>
@@ -56,4 +55,12 @@
5655
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
5756
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
5857
</PropertyGroup>
58+
59+
<!--生成文件-->
60+
<Target Name="_LanguageIdentificationDataGenerate" BeforeTargets="DispatchToInnerBuilds">
61+
<Exec Command="dotnet run --project ../../tool/ModelDataGenerator/ModelDataGenerator.csproj -c Release -e OUTPUT_DIR=&quot;$(MSBuildThisFileDirectory)Resources&quot;" ContinueOnError="true">
62+
<Output TaskParameter="ExitCode" PropertyName="LMDGenerateErrorCode" />
63+
</Exec>
64+
<Error Condition="'$(LMDGenerateErrorCode)' != '0'" Text="langid-model-data generate failed." />
65+
</Target>
5966
</Project>

src/LanguageIdentification/LanguageIdentificationModel.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.IO.Compression;
77
using System.Linq;
8+
using System.Reflection;
89
using System.Runtime.Serialization;
910
using System.Text;
1011

@@ -64,13 +65,15 @@ public static LanguageIdentificationModel Default
6465
{
6566
get
6667
{
67-
if (s_defaultModelReference.TryGetTarget(out var defaultModel))
68+
if (s_defaultModelReference.TryGetTarget(out var defaultModel)
69+
&& defaultModel is not null)
6870
{
6971
return defaultModel;
7072
}
7173
lock (s_defaultModelReference)
7274
{
73-
if (s_defaultModelReference.TryGetTarget(out defaultModel))
75+
if (s_defaultModelReference.TryGetTarget(out defaultModel)
76+
&& defaultModel is not null)
7477
{
7578
return defaultModel;
7679
}
@@ -289,8 +292,13 @@ public bool Equals(T[]? x, T[]? y)
289292
&& Enumerable.SequenceEqual(x, y);
290293
}
291294

292-
public int GetHashCode(T[] obj)
295+
/// <inheritdoc/>
296+
public int GetHashCode(T[]? obj)
293297
{
298+
if (obj is null)
299+
{
300+
return 0;
301+
}
294302
var result = int.MaxValue;
295303
foreach (var item in obj)
296304
{
@@ -314,8 +322,10 @@ public int GetHashCode(T[] obj)
314322
/// <returns></returns>
315323
private static LanguageIdentificationModel InternalLoadDefaultModel()
316324
{
317-
using var stream = new MemoryStream(Resource.ModelData);
318-
using var gzipStream = new GZipStream(stream, CompressionMode.Decompress);
325+
using var modelDataStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("LanguageIdentification.Resources.langid-model-data")
326+
?? throw new InvalidOperationException("Can not load langid-model-data in embedded resource. The referenced library is invalid.");
327+
328+
using var gzipStream = new GZipStream(modelDataStream, CompressionMode.Decompress);
319329
using var bufferedStream = new BufferedStream(gzipStream);
320330

321331
return Deserialize(bufferedStream);

src/LanguageIdentification/Resource.Designer.cs

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/LanguageIdentification/Resource.resx

Lines changed: 0 additions & 124 deletions
This file was deleted.
-1.07 MB
Binary file not shown.

tool/ModelDataGenerator/ModelDataGenerator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net9.0</TargetFrameworks>
5+
<TargetFramework>net9.0</TargetFramework>
66
</PropertyGroup>
77

88
<ItemGroup>

tool/ModelDataGenerator/Program.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using System;
2+
using System.Diagnostics;
23
using System.IO;
34
using System.IO.Compression;
4-
5+
using System.Reflection;
56
using LanguageIdentification;
67

78
#pragma warning disable CS8602 // 解引用可能出现空引用。
@@ -14,7 +15,11 @@ internal class Program
1415

1516
private static void Main(string[] args)
1617
{
17-
using var sourceZipStream = File.OpenRead("langid.zip");
18+
Console.WriteLine("Start Generate langid-model-data");
19+
20+
var stopwatch = Stopwatch.StartNew();
21+
22+
using var sourceZipStream = File.OpenRead(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "langid.zip"));
1823
using var zipArchive = new ZipArchive(sourceZipStream, ZipArchiveMode.Read);
1924
var zipArchiveEntry = zipArchive.GetEntry("langid.json");
2025
using var sourceStream = zipArchiveEntry.Open();
@@ -29,7 +34,17 @@ private static void Main(string[] args)
2934

3035
memoryStream.Seek(0, SeekOrigin.Begin);
3136

32-
using var targetStream = File.OpenWrite("langid-model-data");
37+
var outputDir = Environment.GetEnvironmentVariable("OUTPUT_DIR");
38+
if (!string.IsNullOrEmpty(outputDir))
39+
{
40+
if (!Directory.Exists(outputDir))
41+
{
42+
Directory.CreateDirectory(outputDir);
43+
}
44+
}
45+
46+
using var targetStream = File.OpenWrite(Path.Combine(outputDir ?? string.Empty, "langid-model-data"));
47+
targetStream.SetLength(0);
3348
using var targetGzipStream = new GZipStream(targetStream, CompressionLevel.SmallestSize);
3449

3550
memoryStream.CopyTo(targetGzipStream);
@@ -46,7 +61,9 @@ private static void Main(string[] args)
4661
throw new Exception("SerializeObject not equals SourceObject generate fail.");
4762
}
4863

49-
Console.WriteLine("Generation complete");
64+
stopwatch.Stop();
65+
66+
Console.WriteLine($"langid-model-data Generation complete. During {stopwatch.Elapsed}");
5067
}
5168

5269
#endregion Private 方法

0 commit comments

Comments
 (0)