I want to create a program to load all unique words of a specific file in an array
and display them and also a program to load and display all words of a specific file with their occurrences as 2D array
Kindly help me in the creation of this program which logic will be used in this program
I want to create a program to load all unique words of a specific file in an array
and display them and also a program to load and display all words of a specific file with their occurrences as 2D array
Kindly help me in the creation of this program which logic will be used in this program
What part of this are you having difficulty with? What have you attempted so far?
Can you read text from a file? Do you know how to use arrays? What is meant by a word? Do you exclude punctuation etc etc. Is "its" the same as "it's"...
Do you have to use array or could you use std::map which would probably be more suitable?
I could not understand one thing how to store unique words in an array and all words of specific file in 2D array I can't understand how to create a logic for that The requirement is to use array not maps
This program is finding unique words through using maps but I want to do same work by using array
// C++ program to print unique words in a string
#include <bits/stdc++.h>
using namespace std;
// Prints unique words in a file
void printUniquedWords(char filename[])
{
// Open a file stream
fstream fs(filename);
// Create a map to store count of all words
map<string, int> mp;
// Keep reading words while there are words to read
string word;
while (fs >> word)
{
// If this is first occurrence of word
if (!mp.count(word))
mp.insert(make_pair(word, 1));
else
mp[word]++;
}
fs.close();
// Traverse map and print all words whose count
//is 1
for (map<string, int> :: iterator p = mp.begin();
p != mp.end(); p++)
{
if (p->second == 1)
cout << p->first << endl;
}
}
// Driver program
int main()
{
// Create a file for testing and write something in it
char filename[] = "E:/C++ NUML/New folder/Doc1.txt";
//ofstream fs(filename, ios::trunc);
// fs << "geeks for geeks quiz code geeks practice for qa";
// fs.close();
printUniquedWords(filename);
return 0;
}
Why? You would do a lot LESS work if you used a map.
as 2D array
Well, you can't. A word is a string and a frequency is an integer, whereas an array - 1D, 2D or whatever - has elements of a single datatype.
You could have a 1D array of pair<string,int> ... but then, you might as well have a map.
Are you sure that "2D array" isn't simply referring to how you OUTPUT your data? i.e. as 2 columns of a table?
Construct a program to load all unique words of a specific file in an array
and display them
Construct a program to load all unique words of a specific file in an array
and display them
Design and develop a search engine that will take a word from user as
input and will suggest top 5 most relevant documents in specified system
directory. For every search operation:
Array will be a two dimensional array that will maintain filename along with
the frequency/occurrences of provided word in each document.
After loading your system should be able to display the top 5 five most
relevant documents on the basis of word’s frequency/occurrence in
document Because it is requirement of my school assignment
Yeah, but that says (before you went back and edited it):
"array" - not 2-d array
... and it doesn't say what type of array you have to use
... and it doesn't say count the occurrences
... and it doesn't say that you can't use a std::map (or std::set) to establish a nice sorted collection of unique ones.
If ALL you want to do is find the unique words then put them first in a std::set.
If you also want to count the occurrences of unique words then you can use a std::map.
Either way, put them in a uniqueness-enforcing container first, then copy them to whatever array you want afterwards.
Construct a program to load and display all words of a specific file with
their occurrences as 2D array The second part is this
I have never studied that std::map So i don't know nothing about it That's why I can't use it and also my tutor has never taught that std::map method
Array will be a two dimensional array that will maintain filename along with
the frequency/occurrences of provided word in each document.
After loading your system should be able to display the top 5 five most
relevant documents on the basis of word’s frequency/occurrence in
document
// C++ program to print unique words in a string
#include <fstream>
#include <iostream>
#include <string>
#include <map>
// Prints unique words in a file
void printUniquedWords(const std::string& filename) {
std::fstream fs(filename);
if (!fs) {
std::cout << "Cannot open file\n";
return;
}
std::map<std::string, size_t> mp;
for (std::string word; fs >> word; ++mp[word]);
// Traverse map and print all words whose count is 1
for (constauto& [wd, cnt] : mp)
if (cnt == 1)
std::cout << wd << '\n';
}
// Driver program
int main() {
const std::string filename {"Doc1.txt"};
printUniquedWords(filename);
}
Construct a program to load and display all words of a specific file with
their occurrences as 2D array
Design and develop a search engine that will take a word from user as
input and will suggest top 5 most relevant documents in specified system
directory. For every search operation:
1. Array will be a two dimensional array that will maintain filename along with
the frequency/occurrences of provided word in each document
2.After loading your system should be able to display the top 5 five most
relevant documents on the basis of word’s frequency/occurrence in
documents
Can you tell me what logic will be used for these programs?
Now it is running fine bcz I ran it on VS 2019 But can you tell me in detail that how this code is working bcz I can't understand the execution of program
L22 uniq is defined as an array of type Hist with MaxWrds elements.
L25 word is defined as type std::string. If got (number of different words) is less than maxWrds and word is extracted OK from the file then execute the code in brackets.
L26 define fnd as type bool and initialise to its default value (false here)
L28 - 33 - you should be able to work out what this code does
L35/36 If word not found in array uniq, then add it at the end of the existing words.
L47 - define filename as type std::string, set it's value to "Doc1.txt" and define as const (ie it's value can't be changed once initialised).