【问题标题】:FLUTTER / DART LISTS - How can you check if a sequence of elements contained in a list exists in the same order in another list?FLUTTER / DART LISTS - 如何检查列表中包含的元素序列是否以相同的顺序存在于另一个列表中?
【发布时间】:2022-01-14 14:08:02
【问题描述】:

我正在开发一个词汇测验应用程序(法语到英语/英语到法语)。 我需要检查用户类型是否符合预期。 示例:“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](这里,用户在名词前添加了一个冠词,即不应被视为错误)。

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

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

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

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

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

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

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

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

【问题讨论】:

    标签: list flutter dart contains


    【解决方案1】:

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

    为了在你的代码中使用它,你必须 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); 
    }
    

    【讨论】:

    • 哇!!没想到这么多:) 我现在在 Dart Pad 中查看它,试图理解你的逻辑。感谢您的帮助 :) 如果一切正常,我会尽快通知您 :)
    • 顺便说一句,如果您想直接检查意外元素(例如文章),只需在 else 语句中添加单独的 if 检查以检查 isSpellingIncorrect == false(这意味着长度不同)
    • 相等函数来自一个包?
    • @SylvainJack 是的,您可以从 pub.dev 获得它,但它由 dart 团队自己维护,因此您不必担心支持。 Here是怎么添加的。
    • 您确定 checkOrder 功能正常吗?无论元素是否在相同的序列中,“包含”功能都是正确的,不是吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2012-08-01
    • 1970-01-01
    • 2022-01-06
    • 2021-04-14
    • 1970-01-01
    相关资源
    最近更新 更多