1
00:00:00,170 --> 00:00:06,529
Great! The program is going well. I hope
you enjoying this! It's not a big program

2
00:00:06,529 --> 00:00:13,590
so we don't have lots of code here but
however in this implements quite a lot

3
00:00:13,590 --> 00:00:18,869
of aspects of Python, so we're loading
data in Python, into a Python data type

4
00:00:18,869 --> 00:00:24,439
and we have functions, we have
conditionals here, we have user input, and

5
00:00:24,439 --> 00:00:30,150
later we also going to implement for
loops in this code. So we'll talk about

6
00:00:30,150 --> 00:00:33,719
that later,
why we need for loops. And so we have

7
00:00:33,719 --> 00:00:38,550
libraries, we're reporting libraries here.
And so this is JSON. We also

8
00:00:38,550 --> 00:00:44,610
need to import a difflib library which we
are currently working on, so let's focus

9
00:00:44,610 --> 00:00:53,940
on difflib for now and what we
have is you know we passed this rainn

10
00:00:53,940 --> 00:01:01,050
string with double n here and we got a
set of suggestions so the best matches

11
00:01:01,050 --> 00:01:07,200
of the word rain out of the keys of our
dictionary, but you may also pass a word

12
00:01:07,200 --> 00:01:13,490
that doesn't exist at all and it doesn't
have any similar matches there like that.

13
00:01:13,490 --> 00:01:20,250
So basically you have three scenarios.
The user enters the exact word like rain

14
00:01:20,250 --> 00:01:28,560
and immediately you return data, so with
that particular key. That's the first

15
00:01:28,560 --> 00:01:35,119
scenario. The second scenario is when
user enters a word that is very close to

16
00:01:35,119 --> 00:01:40,310
one of the words you have in your
dictionary so like rain with an extra N.

17
00:01:40,310 --> 00:01:48,869
In that case you want to return the
first item of the list that the

18
00:01:48,869 --> 00:01:53,990
get_close_matches generates.
And the third scenario is when you get

19
00:01:53,990 --> 00:02:01,310
an empty list which means that there is
no such word or similar to that in your

20
00:02:01,310 --> 00:02:09,039
list and so we have three scenarios
which means we need to sort of apply

21
00:02:09,039 --> 00:02:13,730
three conditionals in the conditional
block here. At the moment we have only

22
00:02:13,730 --> 00:02:18,260
two so we need one more which says:
�Did you mean this word?

23
00:02:18,260 --> 00:02:23,780
Please double check it�. For now we'll
just will just pass a message and also

24
00:02:23,780 --> 00:02:34,540
note something. You may also want to pass
a higher cutoff value there like 0.8

25
00:02:34,540 --> 00:02:48,500
because you know sometimes if this is a
low like 0.5 and you pass like some

26
00:02:48,500 --> 00:02:54,920
random word, that will suggest you a few
words which maybe you don't want so you

27
00:02:54,920 --> 00:03:00,290
may want to play around with that. In
that case you get an empty list. I think

28
00:03:00,290 --> 00:03:06,760
I'll use a 0.8 value for the cutoff
when I implement it in the script here.

29
00:03:06,760 --> 00:03:13,069
So let's go to our code. The first
thing we need to check is if the word is

30
00:03:13,069 --> 00:03:18,230
in data and return the value for that
word. You may ask what if you want to

31
00:03:18,230 --> 00:03:25,250
check for some similarities first and
not this? Well, I think the first

32
00:03:25,250 --> 00:03:30,319
conditional you need to apply is the
one that you think will have more

33
00:03:30,319 --> 00:03:34,989
occurrences so if you think that most of
the users will enter the correct word,

34
00:03:34,989 --> 00:03:38,420
then you need to check for that
conditional first. You don't want the

35
00:03:38,420 --> 00:03:43,940
program to go through the conditionals
that have less probability to happen so

36
00:03:43,940 --> 00:03:47,060
you first want to go through this and
then the others are ignored

37
00:03:47,060 --> 00:03:54,470
if this returns to true. If this
doesn't return to true, if it's false,

38
00:03:54,470 --> 00:03:58,790
then you want to check for another
conditional. And when you when you check

39
00:03:58,790 --> 00:04:03,010
for more than two conditionals you need
to implement the elif statement.

40
00:04:03,010 --> 00:04:10,599
So for multiple conditionals and so we
need to, basically we need to check

41
00:04:10,599 --> 00:04:18,040
whether this list is empty or not. So if
it's not empty that means we got a match

42
00:04:18,040 --> 00:04:23,410
in the dictionary. In that case we want to
suggest that match, we want to mention

43
00:04:23,410 --> 00:04:35,380
that match in the message so which
means that if get close, sorry! Let me import

44
00:04:35,380 --> 00:04:46,229
it first. From difflib import
get_close_matches, so elif get_close_

45
00:04:46,229 --> 00:05:01,660
matches with the word given in data.keys.
Like that. So this is a list object,

46
00:05:01,660 --> 00:05:08,080
right? That's the same thing as this on here.
So this is a list object and you want to

47
00:05:08,080 --> 00:05:22,419
check whether the length, len, the length of
that list is greater than 0. It is you

48
00:05:22,419 --> 00:05:37,139
want return a message. Let's say did you
mean? We use a string formatter here %s.

49
00:05:37,139 --> 00:05:45,340
I'll explain what that is.
Instead. Did you mean that instead?

50
00:05:45,340 --> 00:05:51,580
An after the string, after you have closed
the quotes, use the percentage operator and here

51
00:05:51,580 --> 00:05:59,020
you pass the variable that will replace
this formatter inside this string,

52
00:05:59,020 --> 00:06:04,860
so this will be replaced with whatever
variable you pass on the right to this here.

53
00:06:04,860 --> 00:06:15,219
In our case we want to pass get_close_
matches for W and data.keys and you

54
00:06:15,219 --> 00:06:20,940
want to get
the first element of that list and put

55
00:06:20,940 --> 00:06:27,060
it here. So this is to understand it.
Let's say we have a variable which has

56
00:06:27,060 --> 00:06:40,110
a value of one and you say: �hey %s� like that
and percentage a and the percentage s

57
00:06:40,110 --> 00:06:45,000
will be replaced with one. That's the
same what we're doing in here. And yeah,

58
00:06:45,000 --> 00:06:52,979
that's all we have to do. Else,
so if this evaluates to false,

59
00:06:52,979 --> 00:07:00,630
and this evaluates to false as well, then
what that means is we are dealing with

60
00:07:00,630 --> 00:07:08,010
an empty list. So the user entered a
world that didn't have any match in our

61
00:07:08,010 --> 00:07:13,650
dictionary, didn't have any similar
words in our dictionary. So you want to

62
00:07:13,650 --> 00:07:20,030
return �the word doesn't exist�. Let me say
the script. Go to the terminal.

63
00:07:20,030 --> 00:07:26,910
The upper arrow key to call a previously
executed a command. So python 3 app1.py.

64
00:07:26,910 --> 00:07:36,210
Execute, enter a word. Let me start
with a correct word. Rain and you get

65
00:07:36,210 --> 00:07:43,289
the definitions for rainn. Let me execute
again. Rainn with an extra n and yeah,

66
00:07:43,289 --> 00:07:49,289
we get this message: �Did you mean rain
instead?� So the %s was

67
00:07:49,289 --> 00:07:57,300
replaced with the first item of the list
returned by get_close_matches and again

68
00:07:57,300 --> 00:08:02,820
if there's a very random string there
It says the word doesn't exist. Please

69
00:08:02,820 --> 00:08:08,909
double check it. And yeah that's it.
However I still see an issue with

70
00:08:08,909 --> 00:08:16,500
this, so we still can make the program
more user friendly. The problem now is that

71
00:08:16,500 --> 00:08:24,630
when the user enters like rainn by
mistake what you say you just pass a

72
00:08:24,630 --> 00:08:28,500
message there saying: �Did you
mean the rain instead?� �Oh yes, I mean

73
00:08:28,500 --> 00:08:36,930
rain, but how can I enter rain? Why do I
have to exit the program again?� So I'm

74
00:08:36,930 --> 00:08:43,110
thinking of a solution where instead of
just passing a message here and ending

75
00:08:43,110 --> 00:08:51,060
the program, instead of that you execute
another input statement so another input

76
00:08:51,060 --> 00:08:56,370
statement and another input function like
this one in here where you ask

77
00:08:56,370 --> 00:09:06,180
the users to enter let's say Y if they
meant rain or N for no. So Y for yes,

78
00:09:06,180 --> 00:09:17,250
N for no, if they didn't mean rain.
So if yes, then you basically you pass, you

79
00:09:17,250 --> 00:09:25,170
return the value for the rain word. If you
pass no to the input message, then you

80
00:09:25,170 --> 00:09:29,460
say sorry if they say that's not the
word that you meant that we don't know

81
00:09:29,460 --> 00:09:33,240
what you have in mind, so we say:
�The word doesn't exist. Please double-check

82
00:09:33,240 --> 00:09:38,580
it�. That's what I'm thinking, and yeah
think about this and I'm going to

83
00:09:38,580 --> 00:09:42,000
implement it see the next lecture.
Talk to you there.

