...
This commit is contained in:
14
src/app/api/ipa/route.ts
Normal file
14
src/app/api/ipa/route.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import { getIPA } from "@/utils";
|
||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
|
||||||
|
export async function GET(request: NextRequest) {
|
||||||
|
const searchParams = request.nextUrl.searchParams;
|
||||||
|
|
||||||
|
const text = searchParams.get('text');
|
||||||
|
if (!text) return NextResponse.json({ 'error': 400 }, { status: 400 });
|
||||||
|
|
||||||
|
const r = await getIPA(text);
|
||||||
|
if (r === null) return NextResponse.json({ 'error': 424 }, { status: 424 });
|
||||||
|
|
||||||
|
return NextResponse.json({ r }, { status: 200 });
|
||||||
|
}
|
||||||
9
src/app/api/route.ts
Normal file
9
src/app/api/route.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
|
||||||
|
export async function GET(request: NextRequest) {
|
||||||
|
const url = request.url;
|
||||||
|
return NextResponse.json({
|
||||||
|
message: "Hello World",
|
||||||
|
url: url
|
||||||
|
}, { status: 200 });
|
||||||
|
}
|
||||||
33
src/app/ipa-reader/layout.tsx
Normal file
33
src/app/ipa-reader/layout.tsx
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import type { Metadata } from "next";
|
||||||
|
import { Geist, Geist_Mono } from "next/font/google";
|
||||||
|
|
||||||
|
const geistSans = Geist({
|
||||||
|
variable: "--font-geist-sans",
|
||||||
|
subsets: ["latin"],
|
||||||
|
});
|
||||||
|
|
||||||
|
const geistMono = Geist_Mono({
|
||||||
|
variable: "--font-geist-mono",
|
||||||
|
subsets: ["latin"],
|
||||||
|
});
|
||||||
|
|
||||||
|
export const metadata: Metadata = {
|
||||||
|
title: "IPA Reader",
|
||||||
|
description: "read ipa aloud",
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function RootLayout({
|
||||||
|
children,
|
||||||
|
}: Readonly<{
|
||||||
|
children: React.ReactNode;
|
||||||
|
}>) {
|
||||||
|
return (
|
||||||
|
<html lang="en">
|
||||||
|
<body
|
||||||
|
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
);
|
||||||
|
}
|
||||||
46
src/app/ipa-reader/page.tsx
Normal file
46
src/app/ipa-reader/page.tsx
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import Button from "@/components/Button";
|
||||||
|
import { getIPA, urlGoto } from "@/utils";
|
||||||
|
import { useRef, useState } from "react";
|
||||||
|
|
||||||
|
export default function Home() {
|
||||||
|
const respref = useRef<HTMLParagraphElement>(null);
|
||||||
|
const inputref = useRef<HTMLTextAreaElement>(null);
|
||||||
|
const [ipa_result, set_ipa_result] = useState<{ lang: string, ipa: string } | null>(null);
|
||||||
|
|
||||||
|
const generateIPA = () => {
|
||||||
|
const text = inputref.current!.value.trim();
|
||||||
|
if (text.length === 0) return;
|
||||||
|
getIPA(text).then((result: { lang: string, ipa: string } | null) => {
|
||||||
|
set_ipa_result(result);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const readIPA = () => {
|
||||||
|
const text = inputref.current!.value.trim();
|
||||||
|
if (text.length === 0) return;
|
||||||
|
// urlGoto(`https://fanyi.baidu.com/gettts?lan=uk&text=${text}&spd=3`);
|
||||||
|
respref.current!.innerText = '暂不支持朗读';
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<div className="flex w-screen justify-center">
|
||||||
|
<div className="mt-8 bg-gray-100 shadow-xl rounded-xl p-4 flex items-center flex-col">
|
||||||
|
<h1 className="text-5xl mb-4">IPA Reader</h1>
|
||||||
|
<div className="flex flex-row">
|
||||||
|
<textarea ref={inputref}
|
||||||
|
placeholder="输入任意语言的文本"
|
||||||
|
className="h-8 w-128 border-gray-300 border rounded focus:outline-blue-400 focus:outline-2">
|
||||||
|
</textarea>
|
||||||
|
</div>
|
||||||
|
<div className="m-2 flex-row flex gap-2">
|
||||||
|
<Button onClick={generateIPA} label="生成IPA"></Button>
|
||||||
|
<Button onClick={readIPA} label="朗读"></Button>
|
||||||
|
</div>
|
||||||
|
<div ref={respref} className="whitespace-pre-line">
|
||||||
|
语言:{ipa_result?.lang}{'\n'}
|
||||||
|
IPA:{ipa_result?.ipa}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
43
src/utils.ts
Normal file
43
src/utils.ts
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import { GoogleGenAI } from "@google/genai";
|
||||||
|
|
||||||
|
export function inspect(word: string) {
|
||||||
|
const goto = (url: string) => {
|
||||||
|
window.open(url, '_blank');
|
||||||
|
}
|
||||||
|
return () => {
|
||||||
|
word = word.toLowerCase();
|
||||||
|
goto(`https://www.youdao.com/result?word=${word}&lang=en`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function urlGoto(url: string) {
|
||||||
|
window.open(url, '_blank');
|
||||||
|
}
|
||||||
|
|
||||||
|
const ai = new GoogleGenAI({});
|
||||||
|
const prompt = `[TEXT]
|
||||||
|
请推断以上文本的语言,并返回其宽式国际音标(IPA),以JSON格式
|
||||||
|
如:
|
||||||
|
{
|
||||||
|
"lang": "german",
|
||||||
|
"ipa": "[ˈɡuːtn̩ ˈtaːk]"
|
||||||
|
}
|
||||||
|
注意:直接返回json文本,
|
||||||
|
不要带markdown记号,
|
||||||
|
ipa一定要加[],
|
||||||
|
lang的值是小写英语的语言名称`;
|
||||||
|
export async function getIPA(text: string) {
|
||||||
|
const response = await ai.models.generateContent({
|
||||||
|
model: "gemini-2.5-flash",
|
||||||
|
contents: prompt.replace("[TEXT]", text),
|
||||||
|
});
|
||||||
|
if (response.text === undefined) return null;
|
||||||
|
return JSON.parse(response.text);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function ggetIPA(text: string): Promise<{ lang: string, ipa: string } | null> {
|
||||||
|
return {
|
||||||
|
lang: `(这是的${text}的lang)`,
|
||||||
|
ipa: `(这是的${text}的ipa)`
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user