An unfinished system to manage all your paper documentation in an easy way.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

load_dicts.pl 1.8KB

il y a 5 ans
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/perl
  2. use strict;
  3. use JSON;
  4. use DBI;
  5. use utf8;
  6. use GD::Simple;
  7. use Data::Dumper;
  8. use Data::UUID;
  9. use warnings;
  10. $|=1;
  11. $Data::Dumper::Sortkeys = 1;
  12. my $conf = load_conf("../etc/autodoc.json");
  13. my $dbh = sqlconnect($conf->{sql});
  14. my %lang = (
  15. 'fr' => [ 'french' ],
  16. 'de' => [ 'swiss', 'ngerman' ],
  17. 'en' => [ 'british-english-large', 'american-english-large' ],
  18. 'it' => [ 'italian' ],
  19. );
  20. foreach my $lang ( sort keys %lang ) {
  21. print "Loading language $lang ...\n";
  22. foreach my $dict ( @{$lang{$lang}} ) {
  23. my $file = '/usr/share/dict/'.$dict;
  24. my $len;
  25. open(LEN,"wc -l $file |");
  26. while(<LEN>) { if ( /(\d+)/ ) { $len = $1; } }
  27. close(LEN);
  28. next if !defined $len;
  29. my $cnt = 0;
  30. my $start = time();
  31. open(DICT,$file);
  32. while(<DICT>) {
  33. chomp;
  34. sqlquery($dbh, "INSERT IGNORE INTO dict SET word = ?, lang = ?",
  35. $_, $lang);
  36. $cnt++;
  37. if ( ! ( $cnt % 777 ) || $cnt eq $len ) {
  38. printf("\t%s %s/%s (%i%%) ETA: %ss \r",
  39. $dict, $cnt, $len, int($cnt/$len*100),
  40. time()-$start eq 0 ? '-' : int( ( (time()-$start)/$cnt*$len ) - ( time()-$start ) )
  41. );
  42. }
  43. }
  44. close(DICT);
  45. printf("\n");
  46. }
  47. }
  48. sub load_conf {
  49. my($file) = @_;
  50. my $x='';
  51. open(F,"$file") || die "Failed to load configuration file";
  52. while(<F>) { $x.=$_; }
  53. close(F);
  54. return from_json($x);
  55. }
  56. sub sqlconnect {
  57. my($sql) = @_;
  58. my $dsn = "DBI:mysql:database=$sql->{base};host=$sql->{host}";
  59. my $dbh = DBI->connect($dsn, $sql->{user}, $sql->{pass}, {
  60. mysql_enable_utf8 => 1
  61. }) || die "Failed to connect to database";
  62. return $dbh;
  63. }
  64. sub sqlquery {
  65. my $dbh = shift;
  66. my $query = shift;
  67. my @args = @_;
  68. #print STDERR "$query\n";
  69. my $sth = $dbh->prepare($query) || die "Failed to execute SQL query";
  70. $sth->execute(@args) || die "Failed to execute SQL query";
  71. return $sth;
  72. }