下面是我的程式碼 import kuroshiro from "kuroshiro"; import KuromojiAnalyzer from…
- User
下面是我的程式碼
import kuroshiro from "kuroshiro"; import KuromojiAnalyzer from "kuroshiro-analyzer-kuromoji";
const contractions = { "i'm": "I am", "i'll": "I will", "you've": "You have", "he's": "He is", "she's": "She is", "it's": "It is", "we're": "We are", "they're": "They are", "you're": "You are", "i'd": "I would", "you'd": "You would", "he'd": "He would", "she'd": "She would", "we'd": "We would", "they'd": "They would", "can't": "Cannot", "won't": "Will not", "don't": "Do not", "doesn't": "Does not", "isn't": "Is not", "aren't": "Are not", "wasn't": "Was not", "weren't": "Were not", "haven't": "Have not", "hasn't": "Has not", "hadn't": "Had not", "couldn't": "Could not", "wouldn't": "Would not", "shouldn't": "Should not", "mightn't": "Might not", "mustn't": "Must not", "let's": "Let us", "that's": "That is", "who's": "Who is", "what's": "What is", "where's": "Where is", "when's": "When is", "why's": "Why is", "how's": "How is", "she'll": "She will", "he'll": "He will", "it'll": "It will", "we'll": "We will", "they'll": "They will", "you'll": "You will", "that'd": "That would", "who'd": "Who would", "what'd": "What would", "where'd": "Where would", "when'd": "When would", "why'd": "Why would", "how'd": "How would", "i'd've": "I would have", "you'd've": "You would have", "he'd've": "He would have", "she'd've": "She would have", "we'd've": "We would have", "they'd've": "They would have", "y'all": "You all", "i'ma": "I am going to", "i'mo": "I am going to", "it'd've": "It would have", "that's": "That has", "who's": "Who has", "what's": "What has", "where's": "Where has", "when's": "When has", "why's": "Why has", "how's": "How has", "she's": "She has", "he's": "He has", "it's": "It has", "i've": "I have", };
function expandContractions(text, contractions) { text = text.replace(/’/g, "'"); return text .split(" ") .map((word) => contractions[word.toLowerCase()] || word) .join(" "); }
export default async function correctWithLCS(submit, answer, lang) { let originalAnswer, submitWords, answerWords;
if (lang === "jp") { const kuroshiroInstance = new kuroshiro(); await kuroshiroInstance.init(new KuromojiAnalyzer({ dictPath: "/dict" })); originalAnswer = Array.from(answer); submit = await kuroshiroInstance.convert(submit, { to: "hiragana" }); answer = await kuroshiroInstance.convert(answer, { to: "hiragana" }); submitWords = Array.from( submit.toLowerCase().replace(/[\s。、,「」!?……——・]/g, "") ); answerWords = Array.from( answer.toLowerCase().replace(/[\s。、,「」!?……——・]/g, "") ); } else { let expandedSubmit = expandContractions(submit, contractions); let expandedAnswer = expandContractions(answer, contractions); originalAnswer = answer.split(" "); submitWords = expandedSubmit .toLowerCase() .replace(/[.,/#!?%\^&\*;:{}=\-_`~()]/g, "") .split(" "); answerWords = expandedAnswer .toLowerCase() .replace(/[.,\/#!?%^&*;:{}=-_`~()]/g, "") .split(" "); }
- Assistant