#include <dirent.h> #include <stdio.h> #include #include <fstream> #include bool exists(const std::string& filename, const std::string& pattern) { // entree se referme tout seul, lors de la desuction // de l'objet ifstream std::ifstream entree(filename.c_str()); std::string s; while (entree >> s) if (s == pattern) return true; return false; } bool isValid(const std::string& path) { return !path.empty() && path != "." && path != ".."; } int main() { static const std::string folder = "in"; DIR* rep = opendir(folder.c_str()); if (rep) { struct dirent* lecture; int nbFile = 0; int totalFile = 0; while ((lecture = readdir(rep))) { if (!isValid(lecture->d_name)) continue; std::cout << std::left << std::setw(25) << lecture->d_name; ++totalFile; if (exists(folder + "/" + lecture->d_name, "118")) { std::cout << "existe" << std::endl; ++nbFile; } else std::cout << "non" << std::endl; } if (nbFile > 0) std::cout << "Le nombre de fichier contenant le terme est " << nbFile << std::endl; closedir(rep); std::cout << "Le nombre de fichiers est : " << totalFile << std::endl; } return 0; }
#include <dirent.h> #include <stdio.h> #include #include <fstream> #include #include <map> bool exists(const std::string& filename, const std::string& pattern) { // entree se referme tout seul, lors de la desuction // de l'objet ifstream std::ifstream entree(filename.c_str()); std::string s; while (entree >> s) if (s == pattern) return true; return false; } bool isValid(const std::string& path) { return !path.empty() && path != "." && path != ".."; } void fillMap(std::map<std::string, int>& map, const std::string& filename) { std::ifstream entree(filename.c_str()); std::string s; while (entree >> s) ++map[s]; } void showTermFrequency(const std::string& filename) { std::map<std::string, int> map; fillMap(map, filename); int som = 0; typedef std::map<std::string, int>::const_iterator iter; for (iter it = map.begin(); it != map.end(); ++it) som += it->second; std::cout << "\tLes tf du document " << filename << std::endl; for (iter it = map.begin(); it != map.end(); ++it) if (som !=0) std::cout << "\t\t* tf du mot " << it->first << " est " << (float)it->second / som << std::endl; } int main() { static const std::string folder = "in"; DIR* rep = opendir(folder.c_str()); if (rep) { struct dirent* lecture; int nbFile = 0; int totalFile = 0; while ((lecture = readdir(rep))) { if (!isValid(lecture->d_name)) continue; std::cout << std::left << std::setw(25) << lecture->d_name; ++totalFile; if (exists(folder + "/" + lecture->d_name, "118")) { std::cout << "existe" << std::endl; ++nbFile; } else std::cout << "non" << std::endl; showTermFrequency(folder + "/" + lecture->d_name); std::cout << std::endl; } if (nbFile > 0) std::cout << "Le nombre de fichier contenant le terme est " << nbFile << std::endl; closedir(rep); std::cout << "Le nombre de fichiers est : " << totalFile << std::endl; } return 0; }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionvoid creermap(const std::string& filename) { std::map<std::string, int> map; std::ifstream entree(filename.c_str()); std::string s; while (entree >> s) ++map[s]; } int main() {.... /*j'ai mis ce code du main juste après l'instruction (std::cout << "non" << std::endl;)*/ creermap(folder + "/" + lecture->d_name); typedef std::map<std::string, int>::const_iterator iter; for (iter it = map.begin(); it != map.end(); ++it) som = som+ it->second; if (strcmp (lecture->d_name, ".") !=0 && strcmp (lecture->d_name, "..") !=0) sortie << " les tf du document " << lecture->d_name << std::endl; for (iter it = map.begin(); it != map.end(); ++it) if (som !=0) sortie << "tf du mot " << it->first << " est " << (float)it->second / som << std::endl; som=0; } ....}
les tf du document a.txt les tf du document b.txt les tf du document c.txt les tf du document d.txt les tf du document e.txt les tf du document f.txt les tf du document g.txt
while (entree >> s) ++map[s]; typedef std::map<std::string, int>::const_iterator iter; for (iter it = map.begin(); it != map.end(); ++it) som = som+ it->second; if (strcmp (lecture->d_name, ".") !=0 && strcmp (lecture->d_name, "..") !=0) sortie << " les tf du document " << lecture->d_name << std::endl; for (iter it = map.begin(); it != map.end(); ++it) if (som !=0) sortie << "tf du mot " << it->first << " est " << (float)it->second / som << std::endl; som=0;
tf du mot ... est ... nombre de fichiers contenant ce terme est ...
#include <dirent.h> #include <stdio.h> #include #include <fstream> #include #include <map> #include<string> bool exists(const std::string& filename, const std::string& pattern) { std::ifstream entree(filename.c_str()); std::string s; while (entree >> s) if (s == pattern) return true; return false; } bool isValid(const std::string& path) { return !path.empty() && path != "." && path != ".."; } void fillMap(std::map<std::string, int>& map, const std::string& filename) { std::ifstream entree(filename.c_str()); std::string s; while (entree >> s) ++map[s]; } void fillMaps(std::map<std::string, int>& maps) { std::string s; static const std::string folder = "in"; DIR* rep = opendir(folder.c_str()); if (rep) { struct dirent* lecture; while ((lecture = readdir(rep))) { if (!isValid(lecture->d_name)) continue; std::ifstream entree(lecture->d_name); while (entree >> s) ++maps[s]; } closedir(rep); } } void showTermFrequency(const std::string& filename) { std::map<std::string, int> map; fillMap(map, filename); int som = 0; typedef std::map<std::string, int>::const_iterator iter; for (iter it = map.begin(); it != map.end(); ++it) som += it->second; std::cout << "\tLes tf du document " << filename << std::endl; for (iter it = map.begin(); it != map.end(); ++it) if (som !=0) std::cout << "\t\t* tf du mot " << it->first << " est " << (float)it->second / som << std::endl; } int main() { std::map<std::string, int> maps; static const std::string folder = "in"; DIR* rep = opendir(folder.c_str()); if (rep) { struct dirent* lecture; int nbFile = 0; int totalFile = 0; fillMaps(maps); typedef std::map<std::string, int>::const_iterator iter; for (iter it = maps.begin(); it != maps.end(); ++it) { while ((lecture = readdir(rep))) { if (!isValid(lecture->d_name)) continue; std::cout << std::left << std::setw(25) << lecture->d_name; ++totalFile; if (exists(folder + "/" + lecture->d_name, it->first)) { std::cout << "existe" << std::endl; ++nbFile; } else std::cout << "non" << std::endl; showTermFrequency(folder + "/" + lecture->d_name); std::cout << std::endl; } if (nbFile > 0) std::cout << "Le nombre de fichier contenant le terme " << it->first <<" est " << nbFile << std::endl; } closedir(rep); std::cout << "Le nombre de fichiers est : " << totalFile << std::endl; } system("pause"); return 0; }