- use RnnTextHelper
git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@837 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: RnnTextHelper.cpp
|
||||
* Author: jens
|
||||
*
|
||||
* Created on 18. Januar 2022, 19:12
|
||||
*/
|
||||
|
||||
#include "RnnTextHelper.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
RnnTextHelper::RnnTextHelper()
|
||||
{
|
||||
}
|
||||
|
||||
RnnTextHelper::~RnnTextHelper()
|
||||
{
|
||||
}
|
||||
|
||||
int RnnTextHelper::char_is(char c, const char* pTable)
|
||||
{
|
||||
int found = 0;
|
||||
while (*pTable)
|
||||
{
|
||||
if (c == *pTable)
|
||||
{
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
pTable++;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
int RnnTextHelper::ch2idx(char c)
|
||||
{
|
||||
c = toupper(c);
|
||||
|
||||
int index = 0;
|
||||
if (isalpha(c))
|
||||
{
|
||||
index = 1 + (int) (c - 'A');
|
||||
}
|
||||
if (isdigit(c))
|
||||
{
|
||||
index = 1 + 26 + (int) (c - '0');
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
char RnnTextHelper::idx2ch(int index)
|
||||
{
|
||||
char c = '?';
|
||||
if (index == 0)
|
||||
{
|
||||
c = ' ';
|
||||
}
|
||||
else if (index >= 1 and index < 27)
|
||||
{
|
||||
c = (char) (index + 'A' - 1);
|
||||
}
|
||||
else if (index >= 27 and index < 38)
|
||||
{
|
||||
c = (char) (index + '0' - 27);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
arma::mat RnnTextHelper::createTraining(const string& filename, size_t seq_len)
|
||||
{
|
||||
FILE *pFile;
|
||||
|
||||
pFile = fopen(filename.c_str(), "r");
|
||||
|
||||
if (!pFile)
|
||||
{
|
||||
std::cout << "Could not open " << filename << "!" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int numTraining = 0;
|
||||
int numVisible = seq_len*NUM_CODES;
|
||||
|
||||
arma::mat batch = arma::zeros(numTraining, numVisible);
|
||||
arma::mat pattern = arma::zeros(seq_len, NUM_CODES);
|
||||
|
||||
for (int i = 0; i < seq_len; i++)
|
||||
{
|
||||
pattern.row(i)[0] = 1;
|
||||
}
|
||||
|
||||
int last_index = ch2idx(' ');
|
||||
while (!feof(pFile))
|
||||
{
|
||||
char c;
|
||||
int result = fread(&c, 1, 1, pFile);
|
||||
if (result < 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
int index = ch2idx(c);
|
||||
if (index == 0 and last_index == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
last_index = index;
|
||||
pattern = arma::shift(pattern, 1);
|
||||
arma::mat data = arma::zeros(1, NUM_CODES);
|
||||
data[index] = 1;
|
||||
pattern.row(0) = data;
|
||||
arma::mat pattern_vector = pattern.as_row();
|
||||
batch.insert_rows(batch.n_rows, pattern_vector);
|
||||
}
|
||||
fclose(pFile);
|
||||
|
||||
return batch;
|
||||
}
|
||||
Reference in New Issue
Block a user