In: Computer Science
C++ -
De Morgan’s laws commonly apply to text searching using Boolean operators AND, OR, and NOT. Consider a set of documents containing the words “cars” and “trucks”. De Morgan’s laws hold that these two searches will return the same set of documents:
Search A: NOT (cars OR trucks)
Search B: (NOT cars) AND (NOT trucks)
The corpus of documents containing “cars” or “trucks” can be represented by four documents:
Document 1: Contains only the word “cars”.
Document 2: Contains only “trucks”.
Document 3: Contains both “cars” and “trucks”.
Document 4: Contains neither “cars” nor “trucks”.
To evaluate Search A, clearly the search “(cars OR trucks)” will hit on Documents 1, 2, and 3. So the negation of that search (which is Search A) will hit everything else, which is Document 4.
Evaluating Search B, the search “(NOT cars)” will hit on documents that do not contain “cars”, which is Documents 2 and 4. Similarly the search “(NOT trucks)” will hit on Documents 1 and 4. Applying the AND operator to these two searches (which is Search B) will hit on the documents that are common to these two searches, which is Document 4.
Please write a C++ program to De Morgan's Law via document search.
You can use four text files as document 1, 2 3 and 4.
Using this Assignment, read a document and create frequency map. Take the frequency map and display histogram for each word in the document.
Draw histogram in horizontal direction.
This (6) **********
my (4) ******
Truck (6) *********
Sol:
This is the program for De Morgan's law with given conditions
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//Searches in the file for word Car and return true if its there
else returns false
bool hasCar(string fileName) {
//opening file to check
ifstream file( fileName+".txt");
string line;
//initially setting result false, changes only when word is
found
bool result = false;
//going through file line by line
while (getline(file, line)) {
//if cars i written then set result
to true and stop loop
if (line == "cars") {
result =
true;
break;
}
}
return result;
}
//Searches in the file for word Truck and return true if its
there else returns false
bool hasTruck(string fileName) {
//opening file to check
ifstream file( fileName+".txt");
string line;
//initially setting result false, changes only when word is
found
bool result = false;
//going through file line by line
while (getline(file, line)) {
//if trucks is written then set
result to true and stop loop
if (line == "trucks") {
result =
true;
break;
}
}
//cout << result<<"\n";
return result;
}
int main()
{
cout << "Search A: NOT (cars OR
trucks)\n";
//iterating through every file
for (int i = 1;i <= 4;i++) {
//dynanamically creating file
name
string fileName = "Doc" +
to_string(i);
//use these to know condition foe
every file
//cout << "for " <<
fileName<<"\n";
//cout << "if (!(" <<
hasCar(fileName) << " || " << hasTruck(fileName)
<< "))\n";
//writing search A comdition as it
is
if (!(hasCar(fileName) ||
hasTruck(fileName))) {
cout <<
"Document " << i << "\n";
}
}
cout << "Search B: (NOT cars) AND (NOT
trucks)\n";
//iterating through every file
for (int i = 1;i <= 4;i++) {
//dynanamically creating file
name
string fileName = "Doc" +
to_string(i);
//use these to know condition foe
every file
//cout << "for " <<
fileName<<"\n";
//cout << "if (!" <<
hasCar(fileName) << " && !" <<
hasTruck(fileName) << "))\n";
//writing search B condition as it
is
if (!hasCar(fileName) &&
!hasTruck(fileName)) {
cout <<
"Document " << i << "\n";
}
}
return 0;
}
----------------------------
The output is: