Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ jobs:
strategy:
matrix:
dotnet-version:
- '2.1'
- '2.2'
- '9.0'
name: .NET ${{ matrix.dotnet-version }} sample
steps:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v2
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ matrix.dotnet-version }}
- run: dotnet restore
- env:
- name: Restore dependencies
run: dotnet restore
- name: Run tests
env:
DETECTLANGUAGE_API_KEY: ${{ secrets.DETECTLANGUAGE_API_KEY }}
run: dotnet test
24 changes: 24 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Publish dotnet package

on:
release:
types: [published]

jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.0
- name: Build
run: dotnet build --configuration Release
- name: Pack
run: dotnet pack --configuration Release
- name: Publish the package to nuget.org
run: dotnet nuget push */bin/Release/*.nupkg -k $NUGET_AUTH_TOKEN -s https://api.nuget.org/v3/index.json
env:
NUGET_AUTH_TOKEN: ${{ secrets.NUGET_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## files generated by popular Visual Studio add-ons.

# User-specific files
.env.local
*.suo
*.user
*.sln.docstates
Expand Down
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## v2.0.0

### Changed
- Switched to v3 API which uses updated language detection model
- ⚠️ Added `DetectResult` field `score`, removed `confidence` and `reliable`
- Renamed `GetUserStatusAsync` to `GetAccountStatusAsync`
- Updated `Newtonsoft.Json` dependency to `13.0.3`
8 changes: 4 additions & 4 deletions DetectLanguage.Tests/DetectLanguage.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFrameworks>net9.0</TargetFrameworks>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="nunit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="nunit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
</ItemGroup>

<ItemGroup>
Expand Down
18 changes: 8 additions & 10 deletions DetectLanguage.Tests/DetectLanguageClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public async Task TestDetectAsync() {
var results = await client.DetectAsync("Labas rytas");

Assert.That(results[0].language, Is.EqualTo("lt"));
Assert.That(results[0].reliable, Is.EqualTo(true));
Assert.That(results[0].confidence, Is.GreaterThan(0));
Assert.That(results[0].score, Is.GreaterThan(0));
}

[Test]
Expand All @@ -37,7 +36,7 @@ public async Task TestDetectCodeAsync() {

[Test]
public async Task TestDetectCodeAsyncNull() {
string language = await client.DetectCodeAsync("-");
string language = await client.DetectCodeAsync(" ");

Assert.IsNull(language);
}
Expand All @@ -48,8 +47,7 @@ public async Task TestBatchDetectAsync() {
var results = await client.BatchDetectAsync(texts);

Assert.That(results[0][0].language, Is.EqualTo("en"));
Assert.That(results[0][0].reliable, Is.EqualTo(true));
Assert.That(results[0][0].confidence, Is.GreaterThan(0));
Assert.That(results[0][0].score, Is.GreaterThan(0));
Assert.That(results[1][0].language, Is.EqualTo("lt"));
}

Expand All @@ -58,12 +56,12 @@ public async Task TestGetLanguagesAsync() {
var languages = await client.GetLanguagesAsync();

Assert.That(languages[0].code, Is.EqualTo("aa"));
Assert.That(languages[0].name, Is.EqualTo("AFAR"));
Assert.That(languages[0].name, Is.EqualTo("Afar"));
}

[Test]
public async Task TestGetUserStatusAsync() {
var userStatus = await client.GetUserStatusAsync();
public async Task TestGetAccountStatusAsync() {
var userStatus = await client.GetAccountStatusAsync();

Assert.IsNotEmpty(userStatus.date);
Assert.IsNotEmpty(userStatus.plan);
Expand All @@ -75,9 +73,9 @@ public async Task TestGetUserStatusAsync() {
}

[Test]
public void TestGetUserStatusAsyncError() {
public void TestGetAccountStatusAsyncError() {
var testClient = new DetectLanguageClient("someApiKey");
var ex = Assert.ThrowsAsync<DetectLanguageException>(() => testClient.GetUserStatusAsync());
var ex = Assert.ThrowsAsync<DetectLanguageException>(() => testClient.GetAccountStatusAsync());

Assert.IsNotEmpty(ex.Message);
Assert.IsNotNull(ex.Error);
Expand Down
2 changes: 1 addition & 1 deletion DetectLanguage.Tests/DetectLanguageConfigurationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public void TestApiKeyConstructor() {
var config = new DetectLanguageConfiguration("someApiKey");

Assert.That(config.ApiKey, Is.EqualTo("someApiKey"));
Assert.That(config.ApiBase, Is.EqualTo("https://ws.detectlanguage.com/0.2/"));
Assert.That(config.ApiBase, Is.EqualTo("https://ws.detectlanguage.com/v3/"));
Assert.That(config.UserAgent, Does.Match("detectlanguage-dotnet/\\d+"));
}
}
Expand Down
6 changes: 3 additions & 3 deletions DetectLanguage/DetectLanguage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
<Description>Language Detection API Client for .NET (Official Library)</Description>
<AssemblyName>DetectLanguage</AssemblyName>
<PackageId>DetectLanguage</PackageId>
<Version>1.0.0</Version>
<Version>2.0.0</Version>
<PackageTags>detect;identify;language;text;detectlanguage;detection;identification;nlp</PackageTags>
<Authors>Laurynas Butkus</Authors>
<Company>detectlanguage.com</Company>
<PackageProjectUrl>https://github.com/detectlanguage/detectlanguage-dotnet</PackageProjectUrl>
<RepositoryUrl>https://github.com/detectlanguage/detectlanguage-dotnet</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageIconUrl>https://www.nuget.org/profiles/detectlanguage/avatar?imageSize=128</PackageIconUrl>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>net5.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<None Include="..\README.md" Pack="true" PackagePath=""/>
</ItemGroup>

Expand Down
12 changes: 4 additions & 8 deletions DetectLanguage/DetectLanguageClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public DetectLanguageClient(DetectLanguageConfiguration configuration) {
/// <exception cref="DetectLanguageException">Thrown if the request fails.</exception>
public async Task<DetectResult[]> DetectAsync(string text) {
var request = new DetectRequest{ q = text };
var response = await httpClient.PostAsync<DetectResponse>("detect", request);

return response.data.detections;
return await httpClient.PostAsync<DetectResult[]>("detect", request);
}

/// <summary>
Expand All @@ -56,17 +54,15 @@ public async Task<string> DetectCodeAsync(string text) {
/// <exception cref="DetectLanguageException">Thrown if the request fails.</exception>
public async Task<DetectResult[][]> BatchDetectAsync(string[] texts) {
var request = new BatchDetectRequest{ q = texts };
var response = await httpClient.PostAsync<BatchDetectResponse>("detect", request);

return response.data.detections;
return await httpClient.PostAsync<DetectResult[][]>("detect-batch", request);
}

/// <summary>
/// Get account status
/// </summary>
/// <exception cref="DetectLanguageException">Thrown if the request fails.</exception>
public async Task<UserStatus> GetUserStatusAsync() {
return await httpClient.GetAsync<UserStatus>("user/status");
public async Task<AccountStatus> GetAccountStatusAsync() {
return await httpClient.GetAsync<AccountStatus>("account/status");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion DetectLanguage/DetectLanguageConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class DetectLanguageConfiguration {
/// <summary>
/// The API base URI to use on a per-request basis
/// </summary>
public string ApiBase { get; set; } = "https://ws.detectlanguage.com/0.2/";
public string ApiBase { get; set; } = "https://ws.detectlanguage.com/v3/";

/// <summary>
/// HTTP request timeout
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Newtonsoft.Json;

namespace DetectLanguage {
public class UserStatus {
public class AccountStatus {
/// <summary>
/// Current date
/// </summary>
Expand Down
10 changes: 0 additions & 10 deletions DetectLanguage/Entities/BatchDetectResponse.cs

This file was deleted.

10 changes: 0 additions & 10 deletions DetectLanguage/Entities/DetectResponse.cs

This file was deleted.

10 changes: 2 additions & 8 deletions DetectLanguage/Entities/DetectResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@ public class DetectResult {
public string language;

/// <summary>
/// Is detection reliable
/// Detection score (0-1)
/// </summary>
[JsonProperty("isReliable")]
public bool reliable;

/// <summary>
/// Detection confidence score
/// </summary>
public float confidence;
public float score;
}
}
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ From within Visual Studio:
5. Click on the DetectLanguage package, select the appropriate version in the
right-tab and click *Install*.

### Upgrading

When upgrading please check [changelog](CHANGELOG.md) for breaking changes.

## Documentation

For a comprehensive list of examples, check out the [API documentation][api-docs].
Expand All @@ -55,15 +59,15 @@ DetectLanguageClient client = new DetectLanguageClient("YOUR API KEY");
### Language detection

```c#
DetectResult[] results = await client.DetectAsync("Buenos dias señor");
DetectResult[] results = await client.DetectAsync("Dolce far niente");
```

### Single language code detection

If you need just a language code you can use `DetectCodeAsync`. It returns first detected language code.

```c#
string languageCode = await client.DetectCodeAsync("Buenos dias señor");
string languageCode = await client.DetectCodeAsync("Dolce far niente");
```

### Batch detection
Expand All @@ -80,7 +84,7 @@ DetectResult[][] results = await client.BatchDetectAsync(texts);
### Getting your account status

```c#
UserStatus userStatus = await client.GetUserStatusAsync();
AccountStatus accountStatus = await client.GetAccountStatusAsync();
```

### Getting list supported languages
Expand Down
8 changes: 8 additions & 0 deletions mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[tools]
dotnet = "9.0"

[env]
_.file = ".env.local"

[tasks]
test = "dotnet test"