Skip to content

Commit

Permalink
chore: Use source and target instead of direction
Browse files Browse the repository at this point in the history
  • Loading branch information
neet committed Jan 18, 2025
1 parent a357b09 commit af3f2a4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
7 changes: 4 additions & 3 deletions src/api/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const dedupe = <T>(arr: T[]): T[] => Array.from(new Set(arr));
const normalize = (text: string): string => text.toLowerCase();

export type BaseTranslateParams = {
readonly direction: string; // "ja2ain" | "ain2ja";
readonly source: string;
readonly target: string;
readonly dialect: string;
readonly pronoun: string;
};
Expand Down Expand Up @@ -36,10 +37,10 @@ export async function translate(
input: string,
params: TranslateParams,
): Promise<string | string[]> {
const { direction, dialect, pronoun, numReturnSequences = 1 } = params;
const { source, target, dialect, pronoun, numReturnSequences = 1 } = params;

let prompt: string = "";
if (direction === "ja2ain") {
if (source === "ja" && target === "ain") {
prompt = `translate Japanese to Ainu (${dialect}, ${pronoun}): ${input}`;
} else {
prompt = `translate Ainu (${dialect}, ${pronoun}) to Japanese: ${input}`;
Expand Down
22 changes: 13 additions & 9 deletions src/app/[locale]/_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const isServiceUnavailableError = (error: unknown): boolean => {
};

export type TranslationParams = {
direction?: string;
source?: string;
target?: string;
dialect?: string;
pronoun?: string;
};
Expand Down Expand Up @@ -47,7 +48,7 @@ export async function fetchTranslation(
text: string,
params: TranslationParams,
): Promise<Result> {
const { direction, dialect, pronoun } = params;
const { source, target, dialect, pronoun } = params;

if (typeof text !== "string" || text.length === 0) {
return {
Expand All @@ -57,7 +58,7 @@ export async function fetchTranslation(
};
}

if (typeof direction !== "string") {
if (typeof source !== "string" || typeof target !== "string") {
return {
type: "error",
error: "INVALID_ARGUMENT",
Expand Down Expand Up @@ -92,7 +93,7 @@ export async function fetchTranslation(
try {
let translationSource: string;
try {
translationSource = await normalize(text, direction);
translationSource = await normalize(text, source);
} catch (error) {
if (isServiceUnavailableError(error)) {
return {
Expand All @@ -113,7 +114,8 @@ export async function fetchTranslation(
let translation: string;
try {
translation = await api.translate(translationSource, {
direction,
source,
target,
dialect,
pronoun,
});
Expand All @@ -140,14 +142,14 @@ export async function fetchTranslation(
transcriptions: {},
};

if (direction === "ja2ain") {
if (source === "ja") {
result.transcriptions.translation = {
type: "kana",
text: to_kana(translation),
};
}

if (direction === "ain2ja") {
if (source === "ain") {
if (isKana(text)) {
result.transcriptions.text = {
type: "latin",
Expand Down Expand Up @@ -175,7 +177,8 @@ export async function fetchTranslation(
export async function fetchAlternativeTranslations(
text: string,
result: Result,
direction: string,
source: string,
target: string,
dialect: string,
pronoun: string,
): Promise<string[]> {
Expand All @@ -193,7 +196,8 @@ export async function fetchAlternativeTranslations(

try {
const translations = await api.translate(text, {
direction,
source,
target,
dialect,
pronoun,
numReturnSequences: genMax,
Expand Down
18 changes: 8 additions & 10 deletions src/app/[locale]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,16 @@ export default async function Home(props: HomeProps) {
setRequestLocale(params.locale);

const text = searchParams?.text;
const direction =
searchParams?.source && searchParams?.target
? `${searchParams.source}2${searchParams.target}`
: "ja2ain";
const source = searchParams?.source ?? "ja";
const target = searchParams?.target ?? "ain";
const dialect = searchParams?.dialect ?? "沙流";
const pronoun = searchParams?.pronoun ?? "first";

let result: Result | undefined;
if (text) {
result = await fetchTranslation(text, {
direction,
source,
target,
dialect,
pronoun,
});
Expand All @@ -68,18 +67,17 @@ export default async function Home(props: HomeProps) {
alternativeTranslationsPromise = fetchAlternativeTranslations(
text,
result,
direction,
source,
target,
dialect,
pronoun,
);
}

let exampleSentences: Promise<SearchResponse<SearchEntry>> | undefined;
if (text && result?.type === "ok") {
const words = tokenize(
direction === "ain2ja" ? text : result.translation,
false,
);
const words = tokenize(source === "ain" ? text : result.translation, false);

if (words.length < 4) {
exampleSentences = searchClient.searchSingleIndex<SearchEntry>({
indexName: "entries",
Expand Down
4 changes: 2 additions & 2 deletions src/utils/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { isKana } from "./is_kana";

export const normalize = async (
text: string,
direction: string,
source: string,
): Promise<string> => {
if (direction === "ain2ja" && isKana(text)) {
if (source === "ain" && isKana(text)) {
text = await api.romanize(text);
}

Expand Down

0 comments on commit af3f2a4

Please sign in to comment.