summaryrefslogtreecommitdiff
path: root/src/pages/api/nlp.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/pages/api/nlp.ts')
-rw-r--r--src/pages/api/nlp.ts32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/pages/api/nlp.ts b/src/pages/api/nlp.ts
new file mode 100644
index 0000000..0e5eacb
--- /dev/null
+++ b/src/pages/api/nlp.ts
@@ -0,0 +1,32 @@
+// import db from "../../lib/db";
+import { z } from "zod";
+import { NLP } from "sortug-ai";
+
+const schema = z.object({
+ app: z.enum(["stanza", "spacy"]),
+ text: z.string().min(3, "minimum 3 characters"),
+ lang: z
+ .custom<string>((val) => {
+ const check = NLP.ISO.BCP47.parse(val);
+ if (!check.language) return false;
+ const twochars = Object.values(NLP.ISO.iso6393To1);
+ return twochars.includes(check.language);
+ })
+ .optional(),
+});
+
+export const POST = async (request: Request): Promise<Response> => {
+ const bod = await request.json();
+ const { app, text, lang } = await schema.parseAsync(bod);
+
+ try {
+ const res =
+ app === "stanza"
+ ? NLP.Stanza.segmenter(text, lang)
+ : NLP.Spacy.run(text, lang);
+ const r = await res;
+ return Response.json(r, { status: 200 });
+ } catch (error) {
+ return Response.json({ message: "Failure" }, { status: 500 });
+ }
+};