FLUTTER / DART LISTS - 如何检查一个列表中包含的一系列元素是否以相同的顺序存在于另一个列表中?

FLUTTER / DART LISTS - How can you check if a sequence of elements contained in a list exists in the same order in another list?

我正在开发一个词汇测验应用程序(法语到英语/英语到法语)。 我需要检查用户类型是否符合预期。 示例:“Une machine à laver” --> 预期答案是“A washing machine”。 用户可能会犯许多不同类型的错误,例如拼写错误:“A watching machine”,词序错误:“A machine washing”或完全错误的“A garden tool”。

当预期的单词只是一个单词时,我已经设法处理了检查:“un jardin : a garden”。

但是对于复合词,就会出现词序问题。 我将字符串分成两个列表: _expectedAnswer 包含答案的不同元素:[washing,machine] _typedAnswer 包含用户输入内容的不同元素:[machine, washing] 或 [watching,machine] 或 [washing,machine] 或 [a,washing,machine](这里,用户在名词前添加了冠词,这不应被视为错误)。

最后,算法必须告诉用户他犯了什么类型的错误:

  1. 词序错误。
  2. 词序正确,但一个或所有元素存在拼写问题。
  3. 词序错误+拼写问题
  4. 完全错误。

对于拼写检查,我使用 levenstein 算法。而且很满意。

所以首先我想我应该检查 _typedAnswer 是否包含 _expectedAnswer 的所有元素。 -> 应该检查元素的顺序是否相同:顺序没问题,没有拼写 pb。

-> 元素都存在,但不遵守顺序:词序有问题,没有拼写错误。

-> 元素不存在 --> 然后我们检查是否存在“相似元素”(这表明用户拼写错误)....并检查元素的顺序...

有什么建议可以帮助我解决这个算法吗?

我已经阅读了很多有关与飞镖列表相关的所有功能的信息,但我不得不承认,我有点迷失了哪个功能适合使用...

我花了最后一个小时来解决你的问题。我让您可以轻松测试并了解它背后的逻辑。

为了在您的代码中使用它,您必须 put the control-flow statements into a separate functionpass the list of user input elements 以及它的 list of expected result elements

对于两个列表,您需要将每个单词作为一个字符串,从所有空格中删除!

我相信你能够完成那部分,正如你的问题中所描述的那样。如果您能为我的努力点赞并采纳答案,如果对您有所帮助,我将不胜感激。

编辑:需要 pub.dev

上的官方飞镖包
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); 
}