chatbot respond to onequery type

39 Views Asked by At

i implemented chatbot in dart ... my issue which is i have many lists like greeting list , bye list and so on when the use enter the question , the response will be only for the longest key

for example : greeting list has [ hello ] --> return Hello how can i help you
bye list has [ Good Bye ] --> return see you later

if the user input was : Hello good bye
the expected output : Hello how can i help you see you later
but the result was ' see you later '

i wanna it work as the expected

the chatbot i implemented depends on similarity between input and keys

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutterapp/screens/home_screen.dart';
import 'package:flutterapp/screens/search_screen.dart';
import 'package:flutterapp/screens/task_manager.dart';
import 'package:google_nav_bar/google_nav_bar.dart';
import 'package:google_fonts/google_fonts.dart';

import '../widgets/widgets.dart';
import 'chats_screen.dart';



import 'package:string_similarity/string_similarity.dart';
List<String> Letters =['\0','\0','\0'];
class chatbot_Screen extends StatefulWidget {
  final int selectedIndex;

  const chatbot_Screen({Key? key, required this.selectedIndex}) : super(key: key);

  @override
  State<chatbot_Screen> createState() => _chatbot_ScreenState(selectedIndex);
}

class _chatbot_ScreenState extends State<chatbot_Screen> {
  _chatbot_ScreenState(this._selectedIndex);

  int _selectedIndex = 2;

  final List<String> _messages = [];
  final TextEditingController _controller = TextEditingController();

  void _handleSubmitted(String text) {
    _controller.clear();
    setState(() {
      _messages.add("You  \n$text");
      _messages.add("Bot  \n${_getResponse(text)}");
    });
  }

  String? greetingList(String userText) {
    List<String> greetings = [
      'hello',
      'hi',
      'hey',
      'howdy',
      'hi there',
      'hello there',
      'greetings',
      'good morning',
      'good afternoon',
      'good evening',
      'hey there',
      'yo',
      'hiya',
      'what\'s up',
      'how\'s it going',
      'nice to see you',
      'how are you',
      'how do you do',
      'what\'s new',
      'what\'s happening',
      'sup',
      'hola',
      'bonjour',
      'ciao',
      'namaste',
      'hallo',
      'salutations',
      'top of the morning to you',
      'how are things',
      'pleased to meet you',
      'how\'s everything',
      'what\'s cracking',
      'what\'s good',
      'how are you doing',
      'good to see you',
      'long time no see',
      'how have you been',
      'what\'s the news',
      'how\'s your day',
      'howdy-do',
      'g\'day mate',
      'hi-ya',
      'how goes it',
      'good day',
      'what\'s popping',
      'what\'s the craic',
      'what\'s cooking',
      'good to meet you',
      'how\'s life',
      'hi-ho',
      'well hello there',
      'what\'s the buzz',
      'hi-de-ho',
      'hi-ya',
      'hi there, partner',
      'how\'s your day going',
      'what\'s the good word',
      'what\'s shakin\'',
      'what\'s happening',
      'how\'s your morning',
      'what\'s new with you',
      'greetings and salutations',
      'what\'s the latest',
      'what\'s been happening',
      'how are things going',
      'how are you keeping',
      'how\'s tricks',
      'how\'s your day been',
      'how goes the battle',
      'how goes the day',
      'how goes your day',
      'how do you do',
      'how\'s your afternoon',
      'what\'s the news',
      'how\'s your evening',
      'good morning to you',
      'how\'s it hanging',
      'what\'s new with you',
      'how\'s your day been',
      'how are things with you',
      'how do you fare',
      'how\'s your afternoon been',
      'how have you been doing',
      'how\'s your day treating you',
      'what\'s been going on',
    ];
// Define the target phrases for other responses

// Initialize variables to keep track of the maximum similarity and the best matching phrase
    double maxSimilarity = 0; // to count the similarity of the user input and keys
    String bestPhrase = ''; // the key has the most maxSimilarity  it's gonna be ourphrase

// Iterate through the list of greetings and find the one with the highest similarity
    for (String greeting in greetings) { //for i=0 , greeting[i] != null, i++
      double similarity = userText.similarityTo(
          greeting); //compareTwoString(str1,str2)
      //and value between 0 to 1
      if (similarity >
          maxSimilarity) { //find the maximum similarity of the list
        maxSimilarity = similarity;
        bestPhrase =
            greeting; //the phrase needed assigned to variable bestPhrase
      }
    }

// Iterate through the list of other phrases and find the one with the highest similarity


// Set a threshold for similarity score
    double threshold = 0.5; // Adjust as needed
// If the best matching phrase has a similarity score above the threshold, respond accordingly
    if (maxSimilarity >= threshold) {
      Letters[0]='g';
      if (bestPhrase == 'hello' || bestPhrase == 'hi') {
        return 'Hello there! How can I help you today?';
      } else if (bestPhrase == 'hey') {
        return 'Hey! What can I do for you?';
      } else if (bestPhrase == 'howdy') {
        return 'Howdy! How can I assist you?';
      } else if (bestPhrase == 'hi there') {
        return 'Hi there! How may I assist you?';
      } else if (bestPhrase == 'hello there') {
        return 'Hello there! How can I assist you today?';
      } else if (bestPhrase == 'greetings') {
        return 'Greetings! How can I assist you today?';
      } else if (bestPhrase == 'good morning') {
        return 'Good morning! How may I help you?';
      } else if (bestPhrase == 'good afternoon') {
        return 'Good afternoon! How can I assist you today?';
      } else if (bestPhrase == 'good evening') {
        return 'Good evening! How may I assist you?';
      } else if (bestPhrase == 'hey there') {
        return 'Hey there! How can I help you today?';
      } else if (bestPhrase == 'yo') {
        return 'Yo! How can I assist you?';
      } else if (bestPhrase == 'hiya') {
        return 'Hiya! How may I help you?';
      } else if (bestPhrase == 'what\'s up') {
        return 'Hey! What\'s up?';
      } else if (bestPhrase == 'how\'s it going') {
        return 'Hey! How\'s it going?';
      } else if (bestPhrase == 'nice to see you') {
        return 'Nice to see you too! What can I assist you with?';
      } else if (bestPhrase == 'how are you') {
        return 'I\'m just a bot, but thanks for asking!';
      } else if (bestPhrase == 'how do you do') {
        return 'How do you do? How can I assist you today?';
      } else if (bestPhrase == 'what\'s new') {
        return 'Not much, just here to assist you. How about you?';
      } else if (bestPhrase == 'what\'s happening') {
        return 'Not much, just here to assist you. How about you?';
      } else if (bestPhrase == 'sup') {
        return 'Sup! How can I assist you today?';
      } else if (bestPhrase == 'hola') {
        return 'Hola! How can I assist you today?';
      } else if (bestPhrase == 'bonjour') {
        return 'Bonjour! How can I assist you today?';
      } else if (bestPhrase == 'ciao') {
        return 'Ciao! How may I assist you?';
      } else if (bestPhrase == 'namaste') {
        return 'Namaste! How can I help you today?';
      } else if (bestPhrase == 'hallo') {
        return 'Hallo! How may I assist you?';
      } else if (bestPhrase == 'salutations') {
        return 'Salutations! How can I assist you today?';
      } else if (bestPhrase == 'top of the morning to you') {
        return 'Top of the morning to you! How can I help you today?';
      } else if (bestPhrase == 'how are things') {
        return 'How are things with you? How can I assist you today?';
      } else if (bestPhrase == 'pleased to meet you') {
        return 'Pleased to meet you too! What can I assist you with?';
      } else if (bestPhrase == 'how\'s everything') {
        return 'How\'s everything going? How can I assist you today?';
      } else if (bestPhrase == 'what\'s cracking') {
        return 'Not much, just here to assist you. How about you?';
      } else if (bestPhrase == 'what\'s good') {
        return 'Not much, just here to assist you. How about you?';
      } else if (bestPhrase == 'how are you doing') {
        return 'I\'m just a bot, but thanks for asking!';
      } else if (bestPhrase == 'good to see you') {
        return 'Good to see you too! What can I assist you with?';
      } else if (bestPhrase == 'long time no see') {
        return 'Long time no see indeed! How may I assist you?';
      } else if (bestPhrase == 'how have you been') {
        return 'I\'m just a bot, but thanks for asking!';
      } else if (bestPhrase == 'what\'s the news') {
        return 'Not much, just here to assist you. How about you?';
      } else if (bestPhrase == 'how\'s your day') {
        return 'How\'s your day going? How can I assist you today?';
      } else if (bestPhrase == 'howdy-do') {
        return 'Howdy-do! How may I assist you?';
      } else if (bestPhrase == 'g\'day mate') {
        return 'G\'day mate! How can I assist you today?';
      } else if (bestPhrase == 'hi-ya') {
        return 'Hi-ya! How may I assist you?';
      } else if (bestPhrase == 'how goes it') {
        return 'How goes it? How may I assist you?';
      } else if (bestPhrase == 'good day') {
        return 'Good day! How can I assist you today?';
      } else if (bestPhrase == 'what\'s popping') {
        return 'What\'s popping? How may I assist you?';
      } else if (bestPhrase == 'what\'s the craic') {
        return 'What\'s the craic? How may I assist you?';
      } else if (bestPhrase == 'what\'s cooking') {
        return 'What\'s cooking? How may I assist you?';
      } else if (bestPhrase == 'good to meet you') {
        return 'Good to meet you too! What can I assist you with?';
      } else if (bestPhrase == 'how\'s life') {
        return 'How\'s life going? How may I assist you?';
      } else if (bestPhrase == 'hi-ho') {
        return 'Hi-ho! How may I assist you?';
      } else if (bestPhrase == 'well hello there') {
        return 'Well hello there! How may I assist you?';
      } else if (bestPhrase == 'what\'s the buzz') {
        return 'What\'s the buzz?';
      } else if (bestPhrase == 'hi-de-ho') {
        return 'Hi-de-ho! How may I assist you?';
      } else if (bestPhrase == 'hi there, partner') {
        return 'Hi there, partner! How may I assist you?';
      } else if (bestPhrase == 'how\'s your day going') {
        return 'How\'s your day going? How may I assist you?';
      } else if (bestPhrase == 'what\'s the good word') {
        return 'What\'s the good word? How may I assist you?';
      } else if (bestPhrase == 'what\'s shakin\'') {
        return 'What\'s shakin\'? How may I assist you?';
      } else if (bestPhrase == 'what\'s happening') {
        return 'What\'s happening? How may I assist you?';
      } else if (bestPhrase == 'how\'s your morning') {
        return 'How\'s your morning? How may I assist you?';
      } else if (bestPhrase == 'what\'s new with you') {
        return 'What\'s new with you? How may I assist you?';
      } else if (bestPhrase == 'greetings and salutations') {
        return 'Greetings and salutations! How may I assist you?';
      } else if (bestPhrase == 'what\'s the latest') {
        return 'What\'s the latest? How may I assist you?';
      } else if (bestPhrase == 'what\'s been happening') {
        return 'What\'s been happening? How may I assist you?';
      } else if (bestPhrase == 'how are things going') {
        return 'How are things going? How may I assist you?';
      } else if (bestPhrase == 'how are you keeping') {
        return 'How are you keeping? How may I assist you?';
      } else if (bestPhrase == 'how\'s tricks') {
        return 'How\'s tricks? How may I assist you?';
      } else if (bestPhrase == 'how\'s your day been') {
        return 'How\'s your day been? How may I assist you?';
      } else if (bestPhrase == 'how goes the battle') {
        return 'How goes the battle? How may I assist you?';
      } else if (bestPhrase == 'how goes the day') {
        return 'How goes the day? How may I assist you?';
      } else if (bestPhrase == 'how goes your day') {
        return 'How goes your day? How may I assist you?';
      } else if (bestPhrase == 'how do you do') {
        return 'How do you do? How may I assist you?';
      } else if (bestPhrase == 'how\'s your afternoon') {
        return 'How\'s your afternoon? How may I assist you?';
      } else if (bestPhrase == 'what\'s the news') {
        return 'What\'s the news? How may I assist you?';
      } else if (bestPhrase == 'how\'s your evening') {
        return 'How\'s your evening? How may I assist you?';
      } else if (bestPhrase == 'good morning to you') {
        return 'Good morning to you! How may I assist you?';
      } else if (bestPhrase == 'how\'s it hanging') {
        return 'How\'s it hanging? How may I assist you?';
      } else if (bestPhrase == 'what\'s new with you') {
        return 'What\'s new with you? How may I assist you?';
      } else if (bestPhrase == 'how\'s your day been') {
        return 'How\'s your day been? How may I assist you?';
      } else if (bestPhrase == 'how are things with you') {
        return 'How are things with you? How may I assist you?';
      } else if (bestPhrase == 'how do you fare') {
        return 'How do you fare? How may I assist you?';
      } else if (bestPhrase == 'how\'s your afternoon been') {
        return 'How\'s your afternoon been? How may I assist you?';
      } else if (bestPhrase == 'how have you been doing') {
        return 'How have you been doing? How may I assist you?';
      } else if (bestPhrase == 'how\'s your day treating you') {
        return 'How\'s your day treating you? How may I assist you?';
      } else if (bestPhrase == 'what\'s been going on') {
        return 'What\'s been going on? How may I assist you?';
      }
    }

// Fallback response
    return null;
  }

  String? byeList(String userText) {
    List<String> byeWords = [
      'bye',
      'goodbye',
      'see you later',
      'farewell',
      'take care',
      'see you',
      'catch you later',
      'adios',
      'later',
      'so long',
      'until next time',
      'bye for now',
      'bye-bye',
      'see ya',
      'thank you',
      'thanks',
      'i love you',

    ];

    double maxSimilarity = 0;
    String bestPhrase = '';


    for (String byeWord in byeWords) {
      double similarity = userText.similarityTo(byeWord);
      if (similarity > maxSimilarity) {
        maxSimilarity = similarity;
        bestPhrase = byeWord;
      }
    }


    double threshold = 0.5;


    // If the best matching phrase has a similarity score above the threshold, respond accordingly
    if (maxSimilarity >= threshold) {
      Letters[1]='b';
      if (bestPhrase == 'bye' || bestPhrase == 'goodbye' || bestPhrase == 'thank you'
          || bestPhrase == 'thanks' || bestPhrase == 'i love you'
      )
      {
        return 'Goodbye! Have a great day!';
      }
      else if (bestPhrase == 'see you later') {
        return 'See you later! Take care!';
      } else if (bestPhrase == 'farewell') {
        return 'Farewell! Until we meet again!';
      } else if (bestPhrase == 'take care') {
        return 'Take care! See you soon!';
      } else if (bestPhrase == 'catch you later') {
        return 'Catch you later! Bye!';
      } else if (bestPhrase == 'adios') {
        return 'Adios! Hasta luego!';
      } else if (bestPhrase == 'later') {
        return 'Later! Take care!';
      } else if (bestPhrase == 'so long') {
        return 'So long! See you soon!';
      } else if (bestPhrase == 'until next time') {
        return 'Until next time! Bye!';
      } else if (bestPhrase == 'bye for now') {
        return 'Bye for now! See you later!';
      } else if (bestPhrase == 'bye-bye') {
        return 'Bye-bye! Take care!';
      } else if (bestPhrase == 'see ya') {
        return 'See ya! Have a good one!';
      }
      // Add more response conditions here for other bye words as needed
    }

    // Fallback response
    return null;
  }


  String? drEmail(String userText) {
    List<String>Dr_Names = [
      'bassam hammo',
      'firas alghanim',
      'ahmad altamimi',
      'ammar odeh'
      ,
      'abdallah qusef',
      'hazem qattous',
      'abdullah al refai',
      'rania alzubaidi'
    ];

    double maxSimilarity = 0;
    String bestPhrase = '';

    for (String DrName in Dr_Names) {
      double similarity = userText.similarityTo(DrName);
      if (similarity > maxSimilarity) {
        maxSimilarity = similarity;
        bestPhrase = DrName;
      }
    }

    double threshold = 0.4;



    if (maxSimilarity >= threshold) {
      Letters[2] = 'd';
      if (bestPhrase == 'bassam hammo') {
        return 'Email of Dr.Bassam Hammo that you are looking for is [email protected]';
      } else if (bestPhrase == 'firas alghanim') {
        return 'Email of Dr.Firas Alghanim that you are looking for is [email protected]';
      }
      else if (bestPhrase == 'ahmad altamimi') {
        return 'Email of Dr.Ahmad Altamimi that you are looking for is [email protected]';
      }
      else if (bestPhrase == 'ammar odeh') {
        return 'Email of Dr.Ammar Odeh that you are looking for is [email protected]';
      }
      else if (bestPhrase == 'abdallah qusef') {
        return 'Email of Dr.Abdallah Qusef that you are looking for is [email protected]';
      }
      else if (bestPhrase == 'hazem qattous') {
        return 'Email of Dr.Hazem Qattous that you are looking for is [email protected]';
      }
      else if (bestPhrase == 'abdullah al refai') {
        return 'Email of Dr.Abdullah Al-Refai that you are looking for is [email protected]';
      }
      else if (bestPhrase == 'rania alzubaidi') {
        return 'Email of Dr.Rania Alzubaidi that you are looking for is [email protected]';
      }
    }

    return null;

  }
  String _getResponse(String userText) {
    // Lowercase the user text to simplify matching
    userText = userText.toLowerCase(); //Get the message from the user .
    //to make sure we filled the list with the letters
    String? isGreeting=greetingList(userText);
    String? isBye=byeList(userText);
    String? isDr=drEmail(userText);


    //check which query needed
    if (isGreeting!=null && isBye!=null && isDr!=null) {

      return isGreeting + isBye + isDr;
    }
    else if (isGreeting!=null && isDr!=null) {

      return isGreeting +isDr;
    }
    else if (isGreeting!=null && isBye!=null ) {

      return isGreeting + isBye;
    }
    else if (isBye!=null && isDr!=null) {

      return  isBye + isDr;
    }
    else if (isGreeting!=null) {
      return isGreeting;
    }
    else if (isBye!=null ) {
      return isBye;
    }
    else if (isDr!=null) {
      return isDr;
    }
    else {
      return 'Sorry, I\'m not sure how to respond to that.';
    }

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: const Text(
          'Ask AI',
          style: TextStyle(
            color: Colors.blue,
            fontSize: 30,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: ListView.builder(
              padding: const EdgeInsets.all(8.0),
              reverse: true,
              itemCount: _messages.length,
              itemBuilder: (_, int index) => Align(
                alignment: Alignment.centerRight,
                child: ListTile(
                  leading: _messages.reversed.toList()[index].contains("You")
                      ? const Icon(Icons.person, color: Colors.blue)
                      : const Icon(Icons.chat_bubble_outline),
                  title: Text(
                    _messages.reversed.toList()[index],
                    style: TextStyle(
                      fontWeight: _messages.reversed.toList()[index].contains("You")
                          ? FontWeight.normal
                          : FontWeight.normal,
                    ),
                  ),
                ),
              ),
            ),
          ),
          const Divider(height: 1.0),
          Container(
            decoration: BoxDecoration(
              color: Theme.of(context).cardColor,
            ),
            child: _buildTextComposer(),
          ),
        ],
      ),
      bottomNavigationBar: Container(
        color: Colors.blue,
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 15.0),
          child: GNav(
            backgroundColor: Colors.blue,
            color: Colors.white,
            activeColor: Colors.white,
            tabBackgroundColor: Colors.blue.shade200,
            gap: 8,
            selectedIndex: _selectedIndex,
            onTabChange: _onItemTapped,
            padding: const EdgeInsets.all(18),
            tabs: [
              GButton(
                icon: Icons.home,
                text: 'Home',
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => const HomeScreen(selectedIndex: 0)),
                  );
                },
              ),
              GButton(
                icon: Icons.chat_outlined,
                text: 'Chats',
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => const chatScreen(selectedIndex: 1)),
                  );
                },
              ),
              GButton(
                icon: Icons.question_answer_outlined,
                text: 'Ask AI',
              ),
              GButton(
                icon: Icons.assignment_sharp,
                text: 'Task manager',
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => const TaskManagar_Screen(selectedIndex: 3)),
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildTextComposer() {
    return IconTheme(
      data: IconThemeData(color: Theme.of(context).colorScheme.secondary),
      child: Container(
        margin: const EdgeInsets.symmetric(horizontal: 20.0, vertical: 8),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Flexible(
              child: TextField(
                controller: _controller,
                onSubmitted: _handleSubmitted,
                decoration: InputDecoration(
                  hintText: 'How can I help you today?',
                  hintStyle: GoogleFonts.albertSans(color: Colors.black54.withOpacity(0.5)),
                  enabledBorder: OutlineInputBorder(
                    borderSide: BorderSide(
                      color: Colors.black.withOpacity(0.5),
                      width: 1,
                    ),
                    borderRadius: BorderRadius.circular(20),
                  ),
                ),
              ),
            ),
            Container(
              margin: const EdgeInsets.symmetric(horizontal: 4.0),
              child: IconButton(
                icon: const Icon(Icons.send),
                onPressed: () => _handleSubmitted(_controller.text),
                color: Colors.blue,
              ),
            ),
          ],
        ),
      ),
    );
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  void initState() {
    super.initState();
    _selectedIndex = widget.selectedIndex;
  }
}


0

There are 0 best solutions below