Skip to content

Commit

Permalink
v0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
LINYE-MARIANA committed Apr 4, 2024
1 parent 21343f4 commit aba4b59
Show file tree
Hide file tree
Showing 14 changed files with 786 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dart-lang/setup-dart@v1
- run: dart pub get
- run: dart format --output=none --set-exit-if-changed .
- run: dart analyze
- run: dart test --reporter=expanded
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/

# Avoid committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.1

- Initial version.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
ObotAI入力補完サーバーデータを基づき、渡される入力テキストに対し、補完候補を生成するためのパッケージ

## Features

Fetcherを利用してデータを取得し、Generatorを用いて、入力内容に対して補完データが生成できる

## Usage

```dart
import 'package:obot_completion_generator/index.dart';
void main() async {
// Fetcherを利用してサーバーからデータを取得
Fetcher fetcher = Fetcher(
apiKey: "$your_api_key",
getEndpoint: (String locale) {
return "$api_host/input_completion/$locale/";
}
);
List<LocaleDataItem> jaData = await fetcher.fetch("ja");
Generator generator = Generator(
minKeywordLength: 2,
keywordSeparator: ",",
strictMatchLocales: ["en"]
);
generator.loadData("ja", jaData);
List<MatchedResultData> completions = generator.generateCompletions("こんにちは", "ja");
print(completions);
}
```
30 changes: 30 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.

include: package:lints/recommended.yaml

# Uncomment the following section to specify additional rules.

# linter:
# rules:
# - camel_case_types

# analyzer:
# exclude:
# - path/to/excluded/files/**

# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints

# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options
51 changes: 51 additions & 0 deletions example/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'dart:io';

import 'package:obot_completion_generator/index.dart';

void main(List<String> args) async {
String host = "";
String apiKey = "";
String locale = "ja";

for (var i = 0; i < args.length; i++) {
if (i >= args.length - 1) {
break;
}
if (args[i] == "--host") {
host = args[i + 1];
} else if (args[i] == "--key") {
apiKey = args[i + 1];
} else if (args[i] == "--locale") {
locale = args[i + 1];
}
}

print("Fetching [$locale] data from $host with API key $apiKey");

Fetcher fetcher = Fetcher(
apiKey: apiKey,
getEndpoint: (String locale) {
return "$host/input_completion/$locale/";
});

List<LocaleDataItem> jaData = await fetcher.fetch(locale);
print("Fetched ${jaData.length} items: $jaData");

Generator generator = Generator();
generator.loadData(locale, jaData);

while (true) {
print("Enter a keyword to get completions:");
String input = stdin.readLineSync() ?? "";
if (input.isEmpty) {
break;
}

List<MatchedResultData> results =
generator.generateCompletions(input, locale);
print("Results:");
for (var result in results) {
print(result);
}
}
}
12 changes: 12 additions & 0 deletions lib/index.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
library;

export 'src/generator.dart' show Generator;
export 'src/fetcher.dart' show Fetcher;
export 'src/types.dart'
show
LocaleDataItem,
MatchedResultData,
LocaleDataComparator,
LocaleDataFilter,
GetEndpoint,
HandleResponse;
65 changes: 65 additions & 0 deletions lib/src/fetcher.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'dart:convert';
import 'dart:io';

import 'types.dart';

class Fetcher {
final String _apiKey;
final String _apiKeyHeaderName;
final String _httpMethod;
final GetEndpoint? _getEndpoint;
final HandleResponse? _handleResponse;

Fetcher(
{required String apiKey,
String apiKeyHeaderName = "X-Secret-Key",
String httpMethod = "GET",
GetEndpoint? getEndpoint,
HandleResponse? handleResponse = _defaultHandleResponse})
: _apiKey = apiKey,
_apiKeyHeaderName = apiKeyHeaderName,
_httpMethod = httpMethod,
_getEndpoint = getEndpoint,
_handleResponse = handleResponse;

static List<LocaleDataItem> _defaultHandleResponse(String responseBody) {
List<LocaleDataItem> results = [];
dynamic data = json.decode(responseBody);
if (data['user_says'] != null && data['user_says'] is List) {
data['user_says'].forEach((item) {
results.add(LocaleDataItem(
text: item['text'] as String,
keywords: item['keywords'] as String));
});
}
return results;
}

Future<List<LocaleDataItem>> fetch(String locale) async {
String endpoint = _getEndpoint!(locale);
Map<String, String> headers = {_apiKeyHeaderName: _apiKey};

dynamic response = await _fetch(endpoint, headers);
return _handleResponse!(response);
}

Future<dynamic> _fetch(String endpoint, Map<String, String> headers) async {
// HTTPRequestでデータを取得する
String? responseBody;

HttpClient client = HttpClient();
HttpClientRequest request =
await client.openUrl(_httpMethod, Uri.parse(endpoint));
headers.forEach((key, value) {
request.headers.add(key, value);
});
HttpClientResponse response = await request.close();

try {
responseBody = await response.transform(utf8.decoder).join();
} finally {
client.close();
}
return responseBody;
}
}
Loading

0 comments on commit aba4b59

Please sign in to comment.