Help with Mathematica? Find Dictionary Lengths & Longest Words

  • Mathematica
  • Thread starter fruitbygrace
  • Start date
  • Tags
    Mathematica
In summary, it is recommended to use the Map function to perform the same operation on every element in a list. To find the longest word in a dictionary, the Max function should be used on the list of all words in the dictionary. However, using Max on DictionaryLookup[All] will not yield the desired result, so it is important to check what DictionaryLookup[All] actually outputs. Additionally, the Max function cannot be used directly on the output of StringLength, so adjustments to the code may be necessary. Finally, for finding all words of a specific length, the same techniques used in Part B and C can be applied.
  • #1
fruitbygrace
1
0
Part A:
Get the list of pairs of the form {language,number of words in dictionary}. Call this list Alength.

What I did:
lang = DictionaryLookup[All] to get all the languages. Then,
Alength = {Length[DictionaryLookup[{"Finnish", All}]], Length[DictionaryLookup[{"Arabic", All}]]}...}

What is a shorter way of doing this?


-------------
Part B:

Get a list of the form {language, length longest word in the dictionary}. Call this list longestword.

I did:
longestword = Max[DictionaryLookup[All]]

But that's not coming out correctly.


--------------
Part C:
Look at all the words contained in all the dictionaries. What are the ones of maximum length?

I did:
Max[StringLength[lang]]

It comes out as 19, but I think there's something wrong here as well.


--------------
Part D:
What are all the words of length 20 in the Mathematica English dictionary.
This one I have no idea.
 
Physics news on Phys.org
  • #2
fruitbygrace said:
Part A:
Get the list of pairs of the form {language,number of words in dictionary}. Call this list Alength.

What I did:
lang = DictionaryLookup[All] to get all the languages. Then,
Alength = {Length[DictionaryLookup[{"Finnish", All}]], Length[DictionaryLookup[{"Arabic", All}]]}...}

What is a shorter way of doing this?

Almost every time you "want to do the same thing to every element in a list" the solution is to use the Map function. Look in the help system for Map and see if you can see a way to use that.

There are two methods of using this, first to define a function like f and then use Map[f, lang], the second is to use a shortcut method of defining functions using # and &. That second method is probably difficult to understand initially, so using the first method might be best to start with.

fruitbygrace said:
Part B:

Get a list of the form {language, length longest word in the dictionary}. Call this list longestword.

I did:
longestword = Max[DictionaryLookup[All]]

But that's not coming out correctly.

Hint: What is DictionaryLookup[All] and what would Max of that be? Why would that not be what you want? It is also possible that you might have mis-quoted that question.


fruitbygrace said:
Part C:
Look at all the words contained in all the dictionaries. What are the ones of maximum length?

I did:
Max[StringLength[lang]]

It comes out as 19, but I think there's something wrong here as well.

Again, look at StringLength[lang] and see if that is exactly what Max will accept.

fruitbygrace said:
Part D:
What are all the words of length 20 in the Mathematica English dictionary.
This one I have no idea.

Study the hints. Try to understand the method they are suggesting for diagnosing when something doesn't work. Then see if you can apply those ideas to make progress on the last problem.
 

FAQ: Help with Mathematica? Find Dictionary Lengths & Longest Words

1. How do I find the length of a dictionary in Mathematica?

To find the length of a dictionary in Mathematica, you can use the Length function. For example, if your dictionary is named dict, you can use the command Length[dict] to get the number of entries in the dictionary.

2. How do I find the longest word in a dictionary using Mathematica?

To find the longest word in a dictionary, you can use the MaximalBy function. For example, if your dictionary is named dict, you can use the command MaximalBy[dict, StringLength] to get the longest word in the dictionary based on its string length.

3. Can I find the length of each word in a dictionary using Mathematica?

Yes, you can use the Map function to apply the StringLength function to each word in the dictionary. For example, if your dictionary is named dict, you can use the command Map[StringLength, dict] to get a list of the string lengths for each word in the dictionary.

4. How do I exclude words with special characters when finding the longest word in a dictionary using Mathematica?

You can use the Select function to exclude words with special characters before using the MaximalBy function. For example, if your dictionary is named dict, you can use the command MaximalBy[Select[dict, StringMatchQ[#, RegularExpression["[a-zA-Z]+"]]&], StringLength] to get the longest word in the dictionary without any special characters.

5. Can I find the length of words in a dictionary without using built-in functions in Mathematica?

Yes, you can use the Length function and a loop to iterate through each word in the dictionary and count its characters. However, using built-in functions like StringLength and Map can make the process more efficient and less prone to errors.

Similar threads

Replies
6
Views
3K
Replies
1
Views
1K
Replies
22
Views
3K
Replies
4
Views
2K
Replies
12
Views
4K
Replies
2
Views
3K
Replies
4
Views
2K
Back
Top