FLUTTER - Checking if a string contains another one

251

In this case, separate the whole answer with the "space", then compare it with the correct word.

For an example:
User's answer: That is my school
Separate it with space, so that you will find an array of words:
that, is, my, school. 

Then compare each word with your word. It will give you the correct answer.

The flutter code will be like below:

  usersAnswer?.split(" ").forEach((word){
    if(word == correctAnswer) 
      print("this is a correct answer");
  });
Share:
251
SylvainJack
Author by

SylvainJack

I have worked as a teacher for about 25 years and decided I wanted to embrace a new career. I started learning Flutter / Dart in January 2020. I know it will be a long way to be able to develop apps on my own, but I want to prove that I can do it!

Updated on January 03, 2023

Comments

  • SylvainJack
    SylvainJack over 1 year

    I am working on an English vocabulary learning app. Some of the exercises given to the users are written quizzes. They have to translate French words into English words and vice versa.

    To make the checking a little more sophisticated than just "1" or "0" (TypedWord == expectedWord), I have been working with similarities between strings and that worked well (for spelling mistakes for example).

    I had also used the contains function, so that for example, if the user adds an article in front of the expected word, it doesn't consider it wrong. (Ex : Ecole (School is expected), but user writes "A school").

    So I was checking with lines such as "if (typedWord.contains(word)==true) then...". It works fine for the article problem. But it prompts another issue :

    Ex : A bough --> the expected French word is "branche". If user types "une branche", it considers it correct, which is great. But if user types "débrancher" (to unplug), it considers it correct as well as the word "branche" is a part of "débrancher"...

    How could I keep this from happening ? Any idea of other ways to go about it ?

    I read the three proposed answers which are really interesting. The thing is that some of the words are compound.... "Ex : kitchen appliance, garden tool" etc... so then I think the "space" functions might be problematic...

  • Gourango Sutradhar
    Gourango Sutradhar over 2 years
    This will not work for the first word and last word of the sentence.
  • Yanni TheDeveloper
    Yanni TheDeveloper over 2 years
    contains will still have with "débrancher", since its going to split it as one word.
  • Ramin
    Ramin over 2 years
    @YanniTheDeveloper The result of split on "débrancher" will be an array of length 1. Then by using contains on this array it will check whether the two strings are exactly the same or not. It's not like contains on a string where if it partially matches it returns true.
  • Yanni TheDeveloper
    Yanni TheDeveloper over 2 years
    oh, yeah got it.
  • SylvainJack
    SylvainJack over 2 years
    It's interesting. Some of the words have more than one word... ex : "kitchen appliance"... then the user could type "A kitchen appliance"....
  • SylvainJack
    SylvainJack over 2 years
    I edited the end of my question... as some words will be compound words : such as "kitchen appliance"
  • Gourango Sutradhar
    Gourango Sutradhar over 2 years
    In that case you may split your answers into array too, and compare each of them with the answered array. If every item in your answer list is present in the user's answer list, then you may predict it as a correct answer.
  • SylvainJack
    SylvainJack over 2 years
    Yep, that's what I have started to work on.... and now I have to combine this with the checking for spelling mistakes with "chain similarity function'... Indeed, if for the French word for "Kitchen appliance" (Appareil électroménager), the user answers "Kitchin appliance", it's not the same type of mistakes as if he answered "garden tool"... yet it shouldn't be considered as good as "kitchen appliance" ;)
  • Gourango Sutradhar
    Gourango Sutradhar over 2 years
    I see, so you are going to develop an intelligent system which will be able to predict the answers and compare that with the input one. Also, there will be no limitation for any language. I think to gain that you will have to use a neural network.
  • SylvainJack
    SylvainJack over 2 years
    no no nothing so sophisticated as that :) It compares the user's answer to a wordbank which is in memory. so the student will work on vocabulary quizzes but there will be a "somewhat" intelligent way of checking what the user types : did he add the article in front ? Did he write something completely wrong ? Did he make spelling mistakes ? Did he use a synonym for the word (Ex : little vs small) etc... but all the words are in a long list in memory.
  • Gourango Sutradhar
    Gourango Sutradhar over 2 years
    You may try to use some nested if-else then. But you are going to require a huge set of synonyms word book-entry :) All the best.
  • SylvainJack
    SylvainJack over 2 years
  • SylvainJack
    SylvainJack over 2 years
    yep.... about 5000 words. This is what is recommended for middle school learners of English in France. Am working on it ;) Did you get my "Hello" in the chat ?
  • SylvainJack
    SylvainJack over 2 years
    Here is a working solution : It solves the article problem, it takes into account if the user's mistake is a mere "accent" (in French, we have accentuated letters), and it takes into account if it's just a spelling mistake (similarity >0,8) :
  • SylvainJack
    SylvainJack over 2 years
    typedWord!.split(" ").forEach((subAnswer) { if (subAnswer.toLowerCase() == word!.toLowerCase()) { _reussiteExo = Reussite.totale; } else if (removeAccents(subAnswer.toLowerCase()) == removeAccents(word.toLowerCase())) { _reussiteExo = Reussite.partielleAccent; } else if (removeAccents(subAnswer.toLowerCase()) .similarityTo(removeAccents(word.toLowerCase())) > 0.8) { _reussiteExo = Reussite.partielleOrth; } });
  • Gourango Sutradhar
    Gourango Sutradhar over 2 years
    Great, you may use some LCM algorithm to find out the best match for the spelling mistakes :)
  • SylvainJack
    SylvainJack over 2 years
    I use the levenstein algorithm for this. It seams to work quite ok. Have come up against a new problem I hadn't thought about yesterday : the order of the words ;) If expected answer is "garden tool" and the user answers "a tool garden"... which is actually a typical mistake for native French (order of the words are different), it should be pointed out that the mistake is due to a word order problem... Am working on that today... and trying to combine all mistakes : if a user answers "A toul garden" --> two mistakes : 1 spelling mistake and an order mistake...
  • Gourango Sutradhar
    Gourango Sutradhar over 2 years
    So, you will have to check the order too :)
  • SylvainJack
    SylvainJack over 2 years
    Am in the chat, you there ?