PDA

View Full Version : Problema salvataggio e rilettura da file


Mohegan
23-12-2013, 13:18
Salve ragazzi, sto lavorando ad un progetto per il corso di programmazione e progettazione software all'università, si tratta di un classificatore di documenti con algoritmo knn, ho una classe documento definita così:

class document
{
private:
//Contain the name of the ASCII file.
std::string name_file;
//Contain the name of the class, like SPORT, HARDWARE, MOVIES and so on.
std::string name_class;
//Will be our bag of words.
std::map<std::string,int> bag;
//The number of different words in the document, also bag.size()
int size_doc;
public:
//Constructors:
document(void);
document(const char* , const char*);
//Public methods:
void LoadFile(const char*, const char*);
void PrintBag(void) const;
void Clear(void);
//Setters
//Set name_file
void SetName(std::string);
//Set name_class
void SetClass(const std::string);
//Set size_doc
void SetSize(const int);
//Getters:
//Return name_file
std::string GetName(void) const;
//Return name_class
std::string GetClass(void) const;
//Return bag
std::map<std::string,int> GetBag(void) const;
//Return size_doc
int GetSize(void) const;
};

e una classe training set definità così:

class TrainingSet
{
private:
//Our TrainingSet
std::vector<document> t_set;
//Name of TrainingSet
std::string name_ts;
//Number of documents in the t_set, also t_set.size()
int size_ts;
public:
//Default constructor
TrainingSet(void);
//Constructs the TrainingSet with the list of documents
TrainingSet(std::vector<document>);
//Return the name of training set
std::string GetName(void) const;
//The same as second constructor, must be used after default constructor
void CreateTrainingSet(std::vector<document>);
//Update the TrainingSet with one more document
//void UpdateTrainingSet(document*);
//Load existing TrainingSet from file
void LoadTrainingSet(const char *);
//Print all the documents names in the training set
void DocList(void) const;
};

i training set raccolgono documenti, voglio però essere in grado di salvare su file il tutto, scrivendoci nell'ordine:

NOME TRAINING SET
NUMERO DOCUMENTI NEL TRAINING SET
NOME DOC_1
GRANDEZZA DOC_1
CLASSE DOC_1
WORD1 OCC1
WORD2 OCC2
.
.
.
NOME DOC_N
GRANDEZZA DOC_N
CLASSE DOC_N
WORD1 OCC1
WORD2 OCC2


per ricaricare il training set in memoria:
LEGGO STRING (così conosco il nome del training set)
LEGGO INT (così so quanti documenti ci sono nel file)
per ogni documento:
LEGGO STRING (nome doc)
LEGGO INT (n° parole doc)
LEGGO STRING (classe doc)
e poi per ogni parola nel doc (il secondo intero letto) leggo creando una mappa, e quindi il documento, STRING e poi INT

il problema è che nel mio documento di prova ci sono 175 parole differenti e quando scrivo la mappa del documento in un file lui scrive solo 91 parole, i cicli per la scrittura vengono fatti con gli iteratori standard del c++, la parte di codice che scrive è questa:

stream_t<<name_ts;
stream_t<<" ";
stream_t<<size_ts;
stream_t<<" ";
for(index=0x00;index<size_ts;index++)
{
stream_t<<docs[index].GetName();
stream_t<<" ";
stream_t<<docs[index].GetSize();
stream_t<<" ";
stream_t<<docs[index].GetClass();
stream_t<<" ";
ToStream(stream_t,docs[index]);
}

Date per scontato che lo stream_t apre il file correttamente e che la funzione ToStream è questa:

void ToStream(std::fstream& stream, const document& doc)
{
std::map<std::string,int>::const_iterator iter;
iter=doc.GetBag().begin();
while(iter!=doc.GetBag().end())
{
stream<<iter->first;
stream<<" ";
stream<<iter->second;
stream<<" ";
iter++;
}
}

Grazie in anticipo per l'aiuto