Generating a Dictionary Function
2 views (last 30 days)
How would i make a function called 'Word' that determines whether the user has input a real english word, determined based on the content within a file called 'dictionary.txt', which contains the whole english dictionary.
For example, a worked example from the users perspective would be as follows:
What is the word? 'kinbecef'
Not a word. Try Again.
What is the word? 'Community'
... (continue on with code)
Walter Roberson on 26 Apr 2022
strcmpi() perhaps. However that depends how much intelligence you need to put into it. You give an input example with a leading capital: is the search to be case-insensitive or do you have capitalization rules (the rules in English are more complicated than just "capitalize the first letter")? Do you need to deduce the plurals from the base words, including for example goose vs geese but moose does not imply meese?
More Answers (2)
Jan on 26 Apr 2022
Data = fileread('YourDictionary.txt');
List = sort(lower(strsplit(Data, char(10))));
s = input('Input a word: ', 's');
% insert the checks and the oputput here...
% Stop the loop with "break;" if s is empty
Bruno Luong on 27 Apr 2022
Edited: Bruno Luong on 27 Apr 2022
You can use container.Map. I give here example of numbers, but it can handle char array or strings as well.
The time access, using hash is O(1) and is much quicker than ismember (and this test on TMW server seems to invalid Walter's claim that ismember on sorted array is faster, perhaps because MATLAB ismember doesn't know/check array is sorted, and it sorts anyway and we all know some sorting algorithms are not fast when working on already sorted input):
tic; for k=1:numel(b); ismember(b(k),as); end; toc
tic; for k=1:numel(b); ismember(b(k),a); end; toc
tic; for k=1:numel(b); isKey(m,b(k)); end; toc