Skip to content

Commit cdaffb9

Browse files
committed
fixed
0 parents  commit cdaffb9

37 files changed

+90103
-0
lines changed

basic.cpp

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "basic.h"
2+
3+
Basic::Basic(FileStrBridge* fsb) :
4+
ComplexInfo(fsb, "all", true) {
5+
construct();
6+
}
7+
8+
Basic::~Basic() {
9+
destruct();
10+
}
11+
12+
void Basic::transStrToProp(const string& infoStr) {
13+
int lp, rp;
14+
lp = infoStr.find("@timestamp@");
15+
if (lp == string::npos) {
16+
timestamp = 0;
17+
}
18+
else {
19+
lp += 11;
20+
rp = infoStr.find(".", lp) - 1;
21+
timestamp = str::strToInt(infoStr.substr(lp, rp - lp + 1));
22+
}
23+
lp = infoStr.find("@level@");
24+
if (lp == string::npos) {
25+
level = 0;
26+
}
27+
else {
28+
lp += 7;
29+
rp = infoStr.find(".", lp) - 1;
30+
level = str::strToInt(infoStr.substr(lp, rp - lp + 1));
31+
}
32+
33+
/*
34+
lp = infoStr.find("@strategy@");
35+
if (lp == string::npos) {
36+
///////////
37+
}
38+
else {
39+
lp += 10;
40+
rp = infoStr.find(".", lp) - 1;
41+
///////////
42+
}
43+
*/
44+
}
45+
46+
string Basic::transPropToStr() {
47+
string res = "@timestamp@" + str::intToStr(timestamp) + ".\n";
48+
res += "@level@" + str::intToStr(level) + ".";
49+
////////
50+
return res;
51+
}
52+
53+
54+
int Basic::getTimestamp() {
55+
return timestamp;
56+
}
57+
58+
int Basic::getLevel() {
59+
return level;
60+
}
61+
62+
////////// getStrategy()
63+
64+
void Basic::setTimestamp(int ts) {
65+
timestamp = ts;
66+
}
67+
68+
void Basic::setLevel(int le) {
69+
level = le;
70+
}
71+
72+
////////// setStrategy()

basic.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef BASIC_H
2+
#define BASIC_H
3+
4+
#include "info.h"
5+
6+
class Basic : public ComplexInfo {
7+
8+
public:
9+
Basic(FileStrBridge* fsb);
10+
~Basic();
11+
12+
int getTimestamp();
13+
int getLevel();
14+
void setTimestamp(int ts);
15+
void setLevel(int le);
16+
17+
protected:
18+
void transStrToProp(const string& infoStr);
19+
string transPropToStr();
20+
21+
private:
22+
int timestamp, level;
23+
24+
};
25+
26+
27+
#endif

def.cpp

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include "def.h"
2+
3+
OnePieceOfDef::OnePieceOfDef(const string& pospStr, const string& def) :
4+
posp(word::strToPOSP(pospStr)), def(def) {
5+
}
6+
7+
OnePieceOfDef::OnePieceOfDef(const word::POSP& posp, const string& def) :
8+
posp(posp), def(def) {
9+
}
10+
11+
OnePieceOfDef::OnePieceOfDef(const string& entireDef) {
12+
string _entireDef = entireDef;
13+
str::clearEnds(_entireDef);
14+
int dotPos = _entireDef.find('.');
15+
posp = word::strToPOSP(_entireDef.substr(0, dotPos));
16+
def = _entireDef.substr(dotPos + 1, _entireDef.length() - 1 - dotPos);
17+
str::clearEnds(def);
18+
}
19+
20+
OnePieceOfDef::~OnePieceOfDef() {
21+
}
22+
23+
string OnePieceOfDef::toString() {
24+
return word::POSPToStr(posp) + ". " + def;
25+
}
26+
27+
word::POSP OnePieceOfDef::getPosp() {
28+
return posp;
29+
}
30+
31+
string OnePieceOfDef::getDef() {
32+
return def;
33+
}
34+
35+
36+
Def::Def(FileStrBridge* fsb, const string& itemName) :
37+
ComplexInfo(fsb, itemName) {
38+
construct();
39+
}
40+
41+
Def::~Def() {
42+
destruct();
43+
}
44+
45+
int Def::getSize() {
46+
return defList.size();
47+
}
48+
49+
OnePieceOfDef* Def::getItem(int no) {
50+
return defList[no];
51+
}
52+
53+
void Def::transStrToProp(const string& infoStr) {
54+
//cout << "Def::transStrTopProp called" << endl;
55+
int lastEnd = -1;
56+
for (;;) {
57+
int lp = infoStr.find('#', lastEnd + 1);
58+
if (lp == string::npos) break;
59+
int rp = infoStr.find('#', lp + 1);
60+
string temps = infoStr.substr(lp + 1, rp - 1 - (lp + 1) + 1);
61+
OnePieceOfDef* opod = new OnePieceOfDef(temps);
62+
defList.push_back(opod);
63+
lastEnd = rp;
64+
}
65+
}
66+
67+
string Def::transPropToStr() {
68+
//cout << "Def::transPropToStr called" << endl;
69+
string res = "";
70+
for (vector <OnePieceOfDef*>::iterator it = defList.begin(); it != defList.end(); ++it) {
71+
res += '#' + (*it)->toString() + '#' + '\n';
72+
}
73+
str::clearEnds(res);
74+
return res;
75+
}

def.h

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#ifndef DEF_H
2+
#define DEF_H
3+
4+
#include <vector>
5+
6+
#include "info.h"
7+
#include "util.h"
8+
9+
10+
class OnePieceOfDef {
11+
12+
public:
13+
OnePieceOfDef(const string& pospStr, const string& def);
14+
OnePieceOfDef(const word::POSP& posp, const string& def);
15+
OnePieceOfDef(const string& entireDef);
16+
~OnePieceOfDef();
17+
18+
string toString();
19+
word::POSP getPosp();
20+
string getDef();
21+
22+
private:
23+
word::POSP posp;
24+
string def;
25+
26+
};
27+
28+
29+
class Def : public ComplexInfo {
30+
31+
public:
32+
Def(FileStrBridge* fsb, const string& itemName);
33+
~Def();
34+
35+
OnePieceOfDef* getItem(int no);
36+
int getSize();
37+
38+
// It's better to let print be virtual.
39+
// However, template cannot be declared as virtual.
40+
template <class T>
41+
void print(T& display) {
42+
vector <OnePieceOfDef*>::iterator it = defList.begin();
43+
sendStr(display, "\n");
44+
for (int no = 1; it != defList.end(); ++it, ++no) {
45+
string s = " ";
46+
s += str::intToStr(no);
47+
s += ". [";
48+
s += word::POSPToStr((*it)->getPosp()) + "] ";
49+
s += (*it)->getDef() + '\n';
50+
51+
sendStr(display, s);
52+
}
53+
sendStr(display, "\n");
54+
}
55+
56+
protected:
57+
void transStrToProp(const string& infoStr);
58+
string transPropToStr();
59+
60+
private:
61+
vector <OnePieceOfDef*> defList;
62+
63+
};
64+
65+
66+
#endif

env.cpp

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include "env.h"
2+
3+
Env::Env(const string& userName) : userName(userName) {
4+
fsbDef = new FileStrBridge("vocabulary/def.dat");
5+
fsbSent = new FileStrBridge("user/" + userName + "/sentences.dat");
6+
fsbProg = new FileStrBridge("user/" + userName + "/progress.dat");
7+
8+
string staName = "user/" + userName + "/stats.dat";
9+
fsbSta = new FileStrBridge(staName);
10+
11+
FileStrBridge* fsbVoc;
12+
13+
for (int i = 0;; ++i) {
14+
string vocName = "user/" + userName + "/" + str::intToStr(i) + ".dat";
15+
ifstream ifs(vocName.c_str());
16+
if (!ifs) break;
17+
fsbVoc = new FileStrBridge(vocName);
18+
fsbVocList.push_back(fsbVoc);
19+
SimpleInfo* spi = new SimpleInfo(fsbVoc);
20+
vocList.push_back(spi);
21+
Stats* sta = new Stats(fsbSta, str::intToStr(i));
22+
statList.push_back(sta);
23+
}
24+
25+
fsbMast = new FileStrBridge("user/" + userName + "/mastered.dat");
26+
masteredVoc = new SimpleInfo(fsbMast);
27+
28+
fsbHist = new FileStrBridge("user/" + userName + "/history.dat");
29+
history = new SimpleInfo(fsbHist);
30+
31+
fsbBasic = new FileStrBridge("user/" + userName + "/basic.dat");
32+
basic = new Basic(fsbBasic);
33+
}
34+
35+
Env::~Env() {
36+
delete fsbDef;
37+
delete fsbSent;
38+
delete fsbProg;
39+
delete masteredVoc;
40+
delete history;
41+
delete basic;
42+
delete fsbMast;
43+
delete fsbHist;
44+
delete fsbBasic;
45+
46+
for (vector <SimpleInfo*>::iterator it = vocList.begin();
47+
it != vocList.end(); ++it) {
48+
delete (*it);
49+
}
50+
51+
for (vector <Stats*>::iterator it = statList.begin();
52+
it != statList.end(); ++it) {
53+
delete (*it);
54+
}
55+
56+
for (vector <FileStrBridge*>::iterator it = fsbVocList.begin();
57+
it != fsbVocList.end(); ++it) {
58+
delete (*it);
59+
}
60+
61+
delete fsbSta;
62+
}
63+
64+
string Env::getUserName() {
65+
return userName;
66+
}
67+
68+
void Env::pass(const string& word, int diffi, bool ok) {
69+
statList[diffi]->setTotal(statList[diffi]->getTotal() + 1);
70+
Progress prog(fsbProg, word);
71+
prog.setTimestamp(basic->getTimestamp());
72+
if (ok) {
73+
statList[diffi]->setKnown(statList[diffi]->getKnown() + 1);
74+
prog.setFamilarity(prog.getFamilarity() + 1);
75+
if (prog.getFamilarity() == NEED_TIME) {
76+
vocList[diffi]->del(word);
77+
masteredVoc->add(word);
78+
prog.suicide();
79+
}
80+
}
81+
calcLevel();
82+
basic->setTimestamp(basic->getTimestamp() + 1);
83+
}
84+
85+
void Env::calcLevel() {
86+
// TODO: calc more accurately
87+
for (int i = DIFFI_NUM - 1; i >= 0; --i) {
88+
double ratio;
89+
if (statList[i]->getTotal() == 0) {
90+
ratio = 0.5;
91+
}
92+
else {
93+
(double)statList[i]->getKnown() / statList[i]->getTotal();
94+
}
95+
if (ratio > 0.9) {
96+
basic->setLevel((i + 1) * (LEVEL_MAX / DIFFI_NUM)
97+
+ (LEVEL_MAX / DIFFI_NUM) * ratio * 0.5);
98+
if (basic->getLevel() > LEVEL_MAX) {
99+
basic->setLevel(LEVEL_MAX);
100+
}
101+
return;
102+
}
103+
}
104+
double ratio = (double)statList[0]->getKnown() / statList[0]->getTotal();
105+
basic->setLevel((LEVEL_MAX / DIFFI_NUM) * ratio);
106+
}

env.h

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#ifndef ENV_H
2+
#define ENV_H
3+
4+
#include <fstream>
5+
#include "util.h"
6+
#include "info.h"
7+
#include "def.h"
8+
#include "sentences.h"
9+
#include "progress.h"
10+
#include "stats.h"
11+
#include "basic.h"
12+
13+
class Env {
14+
15+
public:
16+
Env(const string& userName);
17+
~Env();
18+
19+
void pass(const string& word, int diffi, bool ok);
20+
string getUserName();
21+
22+
FileStrBridge* fsbDef;
23+
FileStrBridge* fsbSent;
24+
FileStrBridge* fsbProg;
25+
vector <SimpleInfo*> vocList;
26+
SimpleInfo* masteredVoc;
27+
SimpleInfo* history;
28+
Basic* basic;
29+
vector <Stats*> statList;
30+
31+
private:
32+
void calcLevel();
33+
34+
vector <FileStrBridge*> fsbVocList;
35+
FileStrBridge* fsbSta;
36+
FileStrBridge* fsbMast;
37+
FileStrBridge* fsbHist;
38+
FileStrBridge* fsbBasic;
39+
string userName;
40+
41+
};
42+
43+
44+
#endif

0 commit comments

Comments
 (0)