Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you write this function for me? What it does is open the filename and populate the map for the inverted index. int buildIndex(string filename,

Can you write this function for me? What it does is open the filename and populate the map for the inverted index.

int buildIndex(string filename, map>& index) { // To do return 0; }

For example, the filename is tiny.txt:

www.shoppinglist.com EGGS! milk, fish, @ bread cheese www.rainbow.org red ~green~ orange yellow blue indigo violet www.dr.seuss.net One Fish Two Fish Red fish Blue fish !!! www.bigbadwolf.com I'm not trying to eat you

the first line is the URL which is a string and the second line is a single string. What I want to do is check whether any word is in any of the URLs. So the map will be populated as follows:

fish = > {www.shoppinglist.com, www.dr.seuss.net}

Here the key is the word and set is the set of URL that contains that word.

Use these two helper function(first one removes any punctuations from any word and convert it to lowercase and the second one converts any sentence into a set of cleaned tokens. Cleaning means calling the first function.

// Look for inline comments to undersatnd how the function works string cleanToken(string s) { // This for loop checks if there exists any alphabet in the string int found = 0; for (int i = 0, len = s.size(); i < len; i++) { if (isalpha(s[i])) { found = 1; break; } } // This for loop will remove any punctuation from the beginning until the first letter is encountered or return empty string if no alphabet is found for (int i = 0, len = s.size(); i < len; i++) { if (found != 1) { s = ""; } else if (ispunct(s[i])) { s.erase(i--, 1); len = s.size(); } else { break; } } // This for loop will remove any punctuation from the end until the first letter is encountered or return empty string if no alphabet is found for (int i = s.size() - 1; i > 0; i--) { if (found != 1) { s = ""; } else if (ispunct(s[i])) { s.erase(i); } else { break; } } transform(s.begin(), s.end(), s.begin(), ::tolower); // transform string to lowercase return s; }

// TODO: Add a function header comment here to explain the // behavior of the function and how you implemented this behavior set gatherTokens(string text) { set tokens; string word = ""; for (auto x : text) { if (x == ' ') { word = cleanToken(word); if (word != "") { tokens.insert(word); word = ""; } } else { word = word + x; } } word = cleanToken(word); if (word != "") { tokens.insert(word); } return tokens; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions