我正在開發一個詞匯測驗應用程式(法語到英語/英語到法語)。我需要檢查用戶型別是否符合預期。示例:“Une machine à laver” --> 預期答案是“一臺洗衣機”。用戶可能會犯許多不同型別的錯誤,例如拼寫錯誤:“A watch machine”,詞序錯誤:“A machinewash”或完全錯誤“A garden tool”。
當預期的單詞只是一個單詞時,我已經設法處理了檢查:“un jardin:a garden”。
但是對于復合詞,詞序問題就出現了。我將字串分成兩個串列:_expectedAnswer 包含答案的不同元素:[washing,machine] _typedAnswer 包含用戶輸入的不同元素:[machine,washing] 或 [watching,machine] 或 [washing,machine] , 或 [a,washing,machine] (此處,用戶在名詞前添加了冠詞,不應視為錯誤)。
最后,演算法必須告訴用戶他犯了什么型別的錯誤:
- 詞序錯誤。
- 詞序正確,但一個或所有元素包含拼寫問題。
- 詞序錯誤 拼寫問題
- 完全錯誤。
對于拼寫檢查,我使用了 levenstein 演算法。這是令人滿意的。
所以首先我想我應該檢查 _typedAnswer 是否包含 _expectedAnswer 的所有元素。-> 應該檢查元素的順序是否相同:順序正常,不拼寫pb。
-> 元素都存在,但不遵守順序:詞序有問題,沒有拼寫錯誤。
-> 元素不存在 -> 然后我們檢查是否存在“相似元素”(這表明用戶犯了拼寫錯誤)......并檢查元素的順序......
有什么建議可以幫助我解決這個演算法嗎?
我已經閱讀了很多關于與飛鏢串列相關的所有功能,但我不得不承認,我有點迷失了哪個適合使用......
uj5u.com熱心網友回復:
我花了最后一個小時來解決你的問題。我使您可以輕松地對其進行測驗并了解其背后的邏輯。
為了在你的代碼中使用它,你必須put the control-flow statements into a separate function和它pass the list of user input elements一樣list of expected result elements。
對于 BOTH 串列,您需要將每個單詞作為字串,從所有空格中洗掉!
正如您的問題中所述,我相信您能夠做到這一點。如果您能支持我的努力并接受答案,如果它對您有所幫助,我將不勝感激。
編輯:需要pub.dev上的官方 dart 包
Add a line like this to your package's pubspec.yaml:
dependencies:
collection: ^1.15.0
這是邏輯,請在DartPad中復制并測驗:
import 'package:collection/collection.dart';
void main() {
List expectedAnswer = ["one", "two", "three"];
List listWrongSpelling = ["oe", "two", "three"];
List listWrongSpellings = ["oe", "twoo", "three"];
List listWrongOrder = ["two", "one", "three"];
List listEntirelyWrong = ["dskfrm", "twhoo", "111", "tedsf"];
List listIdentical = ["one", "two", "three"];
// FYI: Checks if there is any kind of mistake (is used below dynamically)
Function eq = const ListEquality().equals;
final result1 = eq(expectedAnswer, listWrongSpelling); // false
final result2 = eq(expectedAnswer, listWrongSpellings); // false
final result3 = eq(expectedAnswer, listWrongOrder); // false
final result4 = eq(expectedAnswer, listEntirelyWrong); // false
final result5 = eq(expectedAnswer, listIdentical); // true, the perfect answer
// print(result1);
// print(result2);
// print(result3);
// print(result4);
// print(result5);
// CHECK IF ANSWER IS PERFECT:
bool isPerfect(List toBeChecked, List expectedResult) {
Function eq = const ListEquality().equals;
return eq(toBeChecked, expectedResult) ? true : false;
}
// ONLY EXECUTE OTHERS IF THERE IS AN MISTAKE:
// Checks for word order mistake
// Condition: Must contain each element with identical value, hence only order can be wrong
bool checkOrder(List toBeChecked, List expectedElements) {
List<bool> listOfResults = [];
for (var element in toBeChecked)
{
bool result = expectedElements.contains(element);
listOfResults.add(result);
}
// If one element is not in expectedElements return false
return listOfResults.contains(false) ? false : true;
}
// Checks for any spelling errors
bool checkSpelling(List toBeChecked, List expectedElements) {
// Once this function gets executed there are only two errors possible:
// 1st: Unexpected elements (e.g. an article) (and possibly spelling errors) >> return false
// 2nd: No unexpected elements BUT spelling errors >> return true
return toBeChecked.length == expectedElements.length ? true : false;
}
// FINAL RESULT
String finalResult = "";
// Please try out the other lists from above for all possible cases!
bool isPerfectAnswer = isPerfect(listIdentical, expectedAnswer);
bool isWordOrderIncorrect = checkOrder(listIdentical, expectedAnswer);
bool isSpellingIncorrect = checkSpelling(listIdentical, expectedAnswer);
if(isPerfectAnswer) {
// The user entered the correct solution perfectly
finalResult = "Everything is correct!";
} else if(isWordOrderIncorrect) {
// CHECKS IF ONLY WORD ORDER IS WRONG
// false means there are unexpected elements in the user input
// true there are no unexpected elements, but the order is not correct, since the first check failed!
// Is true the case, then both lists contain the same elements.
finalResult = "Your word order is incorrect!";
} else if(isSpellingIncorrect) {
// Either unexpected elements (lenght of lists must differ) OR misspelled (same length, error in spelling)
finalResult = "Your spelling is incorrect!";
} else {
// If it gets here, the input has:
// Unexpected elements (e.g. an article), possibly spelling errors AND possibly order mistakes
// You could check if the order is correct, but what′s the point to write complex logic for that,
// knowing there are also unexpected elements, like additional prepositions or wrong words, in addition
// to spelling mistakes.
// Just show your user a message like this:
finalResult = "Unfortunatelly your answer is incorrect. Try again!";
}
// PRINT RESULT:
print(finalResult);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/413404.html
標籤:
