Utilisateur:PamputtBot/find-LLbot-doublon.cpp

Définition, traduction, prononciation, anagramme et synonyme sur le dictionnaire libre Wiktionnaire.

Ce programme écrit en c++ va anaylser un fichier de dump (au format xml) et repérer tous les articles qui contiennent deux fois ou plus un même fichier audio. Il enregistre le titre de l’article et les fichiers audios qui apparaissent plus d’une fois dans un fichier texte (double_llbot.txt). Ce fichier texte peut ensuite être exploité par fix_LLbot_doublon.py.

/*
  Pour compiler:
  g++ find_LLbot_doublon.cpp -o find_LLbot_doublon
  Pour exécuter:
  ./find_LLbot_doublon

  Un fichier double_llbot.txt est créé
 */
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <unordered_map>
  
void only_distinct_duplicates(std::vector <std::string> &v)
{
  std::vector < std::string> v_output = v;
  std::sort(v.begin(), v.end());
  auto output = v.begin();
  auto test = v.begin();
  auto run_start = v.begin();
  auto const end = v.end();
  for (auto test = v.begin(); test != end; ++test) {
    if (*test == *run_start) {
      if ((test - run_start) == 1) {
	*output = *run_start;
	++output;
      }
    } else {
      run_start = test;
    }
  }
  v.erase(output, end);
}

int main(int argc, char **argv) {
  std::string filename = "frwiktionary-20211001-pages-meta-current.xml";
  std::ifstream ifile(filename.c_str());
  if (!ifile) {
    std::cout << "Problème à l'ouverture de " << filename << std::endl;
    return -1;
  }

  std::ofstream ofile("double_llbot.txt");
  
  std::string line;
  std::string title = "";
  std::string content = "";
  std::vector < std::string > audios;
  while (getline(ifile,line)) {
    
    if (line.find("<title>") != std::string::npos) {
      size_t pos1 = line.find("<title>")+7;
      size_t pos2 = line.find("</title>");
      title = line.substr(pos1, pos2-pos1);
    }

    content = "";
    audios.clear();
    bool lets_break = false;
    if (line.find("<text") != std::string::npos) {
      size_t pos = line.find(">");
      content += line.substr(pos+1);
      while (getline(ifile, line)) {
	if(line.find("</text>") != std::string::npos) {
	  size_t pos1 = line.find("</text>");
	  content += line.substr(0,pos1);
	  lets_break = true;
	}
	content += line;
	if (line.find("{{écouter") != std::string::npos) {
	  size_t pos1 = line.find("|audio=")+7;
	  size_t pos2 = line.find("|",pos1);
	  if (pos2 == std::string::npos)
	    pos2 = line.find("}",pos1);
	  std::string audio = "";
	  if (pos1 != 6)
	    audio = line.substr(pos1,pos2-pos1);
	  if (!audio.empty())
	    audios.push_back(audio);
	}
	if (lets_break)
	  break;
      }
      
    }
    
    only_distinct_duplicates(audios);
    
    if(!title.empty() &&
       !content.empty() &&
       title.find(":") == std::string::npos &&
       !audios.empty()) {

      /*
      std::cout << " >>> >>> " << title << " <<< <<<" << std::endl;
      for (unsigned i=0 ; i<audios.size() ; ++i) {
	std::cout << "* " << audios.at(i) << std::endl;
      }
      std::cout << content << std::endl;
      std::cout << "--------------------------------" << std::endl;
      */
      ofile << "# [[" << title << "]]"; 
      for (unsigned i=0 ; i<audios.size() ; ++i) {
	ofile << "\t" << audios.at(i);
      }
      ofile << std::endl;
    }
    
  }
  
  ifile.close();
  ofile.close();
  return 0;
}