Created
August 8, 2013 07:01
-
-
Save selbyk/6182208 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <fstream> | |
#include <iostream> | |
#include <ctype.h> | |
#include "fann.h" | |
#include "fann_cpp.h" | |
using namespace std; | |
void error(const char* p, const char* p2 = ""){ | |
cerr << p << ' ' << p2 << endl; | |
exit(1); | |
} | |
/** | |
Generates letter frequencies from a file to add to the training data | |
*/ | |
void generate_frequencies(const char *filename, float *frequencies){ | |
ifstream infile(filename); | |
if(!infile) error("Cannot open input file", filename); | |
vector<unsigned int> letter_count(26, 0); | |
unsigned int num_characters = 0; | |
char c; | |
while(infile.get(c)){ | |
c = tolower(c); | |
if(c >= 'a' && c <= 'z'){ | |
letter_count[c - 'a']++; | |
num_characters++; | |
} | |
} | |
if(!infile.eof()) error("Something strange happened"); | |
for(unsigned int i = 0; i != 26; i++) frequencies[i] = letter_count[i]/(double)num_characters; | |
} | |
/** | |
Generates letter frequencies from a file to add to the training data | |
*/ | |
/*int main(int argc, char* argv[]){ | |
if(argc != 2) error("Remember to specify an input file"); | |
float frequencies[26]; | |
generate_frequencies(argv[1], frequencies); | |
for(unsigned int i = 0; i != 26; i++) cout << frequencies[i] << ' '; | |
cout << endl; | |
return 0; | |
}*/ | |
/** | |
Tests a file and tells where it's english, french, or polish | |
*/ | |
int main(int argc, char* argv[]){ | |
if(argc != 2) error("Remember to specify an input file"); | |
float frequencies[26]; | |
generate_frequencies(argv[1], frequencies); | |
for(unsigned int i = 0; i != 26; i++) cout << frequencies[i] << ' '; | |
FANN::neural_net ann; | |
ann.create_from_file("language_classify.net"); | |
float *output = ann.run(frequencies); | |
cout << "English: " << output[0] << endl | |
<< "French: " << output[1] << endl | |
<< "Polish: " << output[2] << endl; | |
return 0; | |
} | |
/** | |
Creates the neural network and trains it on the data file | |
*/ | |
/*int main(){ | |
const unsigned int num_layers = 3; | |
const unsigned int num_input = 26; | |
const unsigned int num_hidden = 13; | |
const unsigned int num_output = 3; | |
const unsigned int layers[3] = {26,26,3}; | |
FANN::neural_net ann; | |
ann.create_standard_array(3, layers); | |
ann.train_on_file("frequencies.data", 2000, 10, 0.0001); | |
ann.save("language_classify.net"); | |
ann.destroy(); | |
return 0; | |
}*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 26 3 | |
0.103 0.016 0.054 0.060 0.113 0.010 0.010 0.048 0.056 0.003 0.010 0.035 0.014 0.065 0.075 0.013 0.000 0.051 0.083 0.111 0.030 0.008 0.019 0.000 0.016 0.000 | |
1 0 0 | |
0.076 0.010 0.022 0.039 0.151 0.013 0.009 0.009 0.081 0.001 0.000 0.058 0.024 0.074 0.061 0.030 0.011 0.069 0.100 0.074 0.059 0.015 0.000 0.009 0.003 0.003 | |
0 1 0 | |
0.088 0.016 0.030 0.034 0.089 0.004 0.011 0.023 0.071 0.032 0.030 0.025 0.047 0.058 0.093 0.040 0.000 0.062 0.044 0.035 0.039 0.002 0.044 0.000 0.037 0.046 | |
0 0 1 | |
0.0953608 0.0206186 0.0289233 0.0441008 0.119989 0.0214777 0.0177549 0.0395189 0.0693013 0.00171821 0.00515464 0.0395189 0.0252005 0.064433 0.0827606 0.0237686 0.000286369 0.0630011 0.0715922 0.111111 0.0237686 0.00715922 0.00973654 0.00200458 0.0111684 0.000572738 | |
1 0 0 | |
0.0812233 0.00559027 0.0335416 0.0345281 0.14436 0.016442 0.020388 0.00887866 0.0825386 0.000986518 0 0.0723446 0.0272937 0.0730023 0.0473528 0.0437356 0.00427491 0.0792502 0.083854 0.0670832 0.057218 0.0092075 0.000328839 0.00493259 0.00131536 0.000328839 | |
0 1 0 | |
0.102384 0.0140252 0.0357644 0.0392707 0.0806452 0.00420757 0.0154278 0.013324 0.0841515 0.0189341 0.0518934 0.0399719 0.0273492 0.0631136 0.0806452 0.0224404 0.000701262 0.0350631 0.0575035 0.056101 0.0329593 0.00210379 0.0399719 0 0.0336606 0.0483871 | |
0 0 1 | |
0.0754835 0.0166348 0.0366944 0.036144 0.129195 0.0204419 0.0168183 0.0430242 0.0943964 0.00241572 0.00455623 0.0426573 0.0250745 0.0796575 0.0641235 0.02363881 0.0139133 0.00180414 0.0158245 0.00223225 | |
1 0 0 | |
0.0728756 0.0115966 0.0347657 0.0388076 0.151838 0.011789 0.0109951 0.0140266 0.0912809 0.00317583 0.00228563 0.0582235 0.0286546 0.0822106 0.0524974 0.028799 0.0109229 0.0690501 0.0762439 0.0720094 0.0510779 0.0142672 0.00202098 0.00437879 0.00481186 0.00139544 | |
0 1 0 | |
0.102671 0.0212689 0.0358034 0.0299306 0.0970478 0.00689311 0.0164618 0.0131287 0.0970931 0.0206567 0.0262346 0.0265294 0.0265521 0.076255 0.0681375 0.0268242 6.80241e-05 0.0551449 0.0503152 0.0538071 0.0221532 0.00108839 0.0447599 0.000453494 0.0375947 0.0431273 | |
0 0 1 | |
0.0787958 0.0158488 0.0399622 0.0349005 0.126359 0.0174751 0.0185539 0.0327929 0.0825298 0.00137743 0.00685398 0.0399788 0.0258891 0.0829115 0.0741822 0.0247274 0.000796588 0.0700665 0.0666645 0.0889689 0.0252419 0.0125629 0.015185 0.00217402 0.0135254 0.00167615 | |
1 0 0 | |
0.0701951 0.0102924 0.0399208 0.0411266 0.142759 0.0143405 0.0110676 0.00701951 0.0817794 0.00198097 0.00189484 0.0520219 0.0285087 0.086172 0.0619698 0.0310925 0.00736402 0.0776452 0.0756212 0.0837604 0.0523233 0.0101201 0.00284225 0.00430645 0.00335903 0.000516774 | |
0 1 0 | |
0.0715975 0.0082006 0.0348525 0.0329601 0.0928876 0.00583504 0.0138779 0.0102507 0.0815329 0.0205015 0.0329601 0.0206592 0.0247595 0.0769595 0.0851601 0.0392683 0.000157704 0.0608737 0.0492036 0.0756978 0.0332755 0.00141933 0.0517269 0.000946223 0.0370604 0.0373758 | |
0 0 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment