The unified diff between revisions [08b6eb4b..] and [a69b843c..] is displayed below. It can also be downloaded as a raw diff.
#
#
# add_file "markov.cc"
# content [3d988003848ccd30d002c01ba0c37b5a18af3ee2]
#
# add_file "markov.hh"
# content [4a90060f8f59d6ec659476c5907bb4949e779973]
#
# patch "CMakeLists.txt"
# from [8ed254205fc2321a64b60d053bcef5b8e4d50f13]
# to [b8570d1c8da32bb3f49860f4e186da1ad3dbd59c]
#
# patch "fritz_this.cc"
# from [5899cbc88d4f237b849058bb09df347f5c0ccb72]
# to [8b19c7ba50e1da9f7cb38956278c65d37cd4771a]
#
# patch "test.cc"
# from [499b68055943d776d47fc9cc587c60b52a124475]
# to [18431b5fb65bd97f2738dc0292ac0424075dd96e]
#
# patch "tokens.hh"
# from [ae7420a0271eaa3a8670352c03cb871b00b53a30]
# to [47f907b4e592104293128c54dcdc8e222901cd35]
#
# patch "tokens_test.cc"
# from [9c0ec9ccebc6a0e1d4097cd7fc0d50352f941f71]
# to [2fbe6a442cbc6b29943b87051b164da7a12c14e3]
#
============================================================
--- markov.cc 3d988003848ccd30d002c01ba0c37b5a18af3ee2
+++ markov.cc 3d988003848ccd30d002c01ba0c37b5a18af3ee2
@@ -0,0 +1,56 @@
+
+#include <iostream>
+#include <deque>
+
+#include "markov.hh"
+
+using namespace std;
+
+MarkovState::MarkovState ()
+{
+ valid = false;
+}
+
+MarkovState::MarkovState (const deque<token_t> &q)
+{
+ state = q;
+}
+
+MarkovState::~MarkovState ()
+{
+
+}
+
+bool
+MarkovState::operator==(const MarkovState &s)
+{
+ return s.state == state;
+}
+
+MarkovModel::MarkovModel (const Stash &s, int length)
+{
+ const vector<token_t> stash = s.get_stash ();
+ deque<token_t> q;
+ MarkovState last_state;
+
+ cout << "Hello" << endl;
+ for (vector<token_t>::const_iterator i=stash.begin();i!=stash.end();i++) {
+ q.push_back (*i);
+ cout << "token: " << *i << " : " << q.size() << endl;
+ if (q.size() == length) {
+ MarkovState this_state(q);
+ if (last_state.valid) {
+ markov_pair p(last_state, this_state);
+ edges[p]++;
+ cout << "edges go to " << edges[p] << endl;
+ }
+ last_state = MarkovState (q);
+ q.pop_front();
+ }
+ }
+}
+
+MarkovModel::~MarkovModel ()
+{
+
+}
============================================================
--- markov.hh 4a90060f8f59d6ec659476c5907bb4949e779973
+++ markov.hh 4a90060f8f59d6ec659476c5907bb4949e779973
@@ -0,0 +1,77 @@
+#ifndef __MARKOV_HH
+#define __MARKOV_HH
+
+#include "tokens.hh"
+#include <vector>
+#include <deque>
+
+class MarkovState {
+public:
+ std::deque<token_t> state;
+ bool valid;
+
+ MarkovState ();
+ MarkovState (const std::deque<token_t> &);
+ ~MarkovState ();
+ bool operator==(const MarkovState &);
+};
+
+typedef std::pair< MarkovState, MarkovState > markov_pair;
+
+namespace __EXT__ {
+ template <>
+ struct hash<MarkovState>
+ {
+ size_t
+ operator()(MarkovState __s) const
+ {
+ size_t rv=0;
+ __EXT__::hash<int> h;
+
+ for (std::deque<token_t>::const_iterator i=__s.state.begin(); i!=__s.state.end(); i++) {
+ rv ^= h(*i);
+ }
+ return rv;
+ }
+ };
+
+ template <>
+ struct hash<markov_pair>
+ {
+ size_t
+ operator()(markov_pair __s) const
+ {
+ __EXT__::hash<MarkovState> h;
+ size_t h1, h2;
+
+ h1 = h(__s.first);
+ h2 = h(__s.second);
+ return h1 ^ (h2*h2);
+ }
+ };
+}
+
+namespace std {
+ template <>
+ struct equal_to<markov_pair>
+ {
+ size_t
+ operator()(markov_pair p1, markov_pair p2)
+ {
+ return p1.first == p2.first && p1.second == p2.second;
+ }
+ };
+}
+
+class MarkovModel {
+private:
+ __EXT__::hash_map<markov_pair, int, __EXT__::hash<markov_pair> > edges;
+
+public:
+ MarkovModel (const Stash&, int);
+ ~MarkovModel ();
+};
+
+
+
+#endif
============================================================
--- CMakeLists.txt 8ed254205fc2321a64b60d053bcef5b8e4d50f13
+++ CMakeLists.txt b8570d1c8da32bb3f49860f4e186da1ad3dbd59c
@@ -22,7 +22,7 @@
add_executable (fritz_this fritz_this.cc)
-add_executable (tokens_test tokens_test.cc)
+add_executable (tokens_test tokens_test.cc markov.cc)
add_executable (test test.cc)
============================================================
--- fritz_this.cc 5899cbc88d4f237b849058bb09df347f5c0ccb72
+++ fritz_this.cc 8b19c7ba50e1da9f7cb38956278c65d37cd4771a
@@ -6,6 +6,8 @@
#include <sys/stat.h>
#include <fcntl.h>
+using namespace std;
+
int
main (int argc, char *argv[])
{
@@ -17,7 +19,7 @@ main (int argc, char *argv[])
break;
} else {
buf[nbytes] = '\0';
- std::cout << buf;
+ cout << buf;
}
}
}
============================================================
--- test.cc 499b68055943d776d47fc9cc587c60b52a124475
+++ test.cc 18431b5fb65bd97f2738dc0292ac0424075dd96e
@@ -4,15 +4,17 @@
#include <iostream>
#include "tokens.hh"
+using namespace std;
+
int
main (int argc, char *argv[])
{
- __gnu_cxx::hash_map<std::string, int> h;
- std::string blah("test"), cat("goat");
+ __gnu_cxx::hash_map<string, int> h;
+ string blah("test"), cat("goat");
h[blah] = 2;
h[cat] = 23;
- std::cout << blah << " ::: " << h[blah] << ":::" << h[cat] << std::endl;
+ cout << blah << " ::: " << h[blah] << ":::" << h[cat] << endl;
return 0;
}
============================================================
--- tokens.hh ae7420a0271eaa3a8670352c03cb871b00b53a30
+++ tokens.hh 47f907b4e592104293128c54dcdc8e222901cd35
@@ -19,40 +19,49 @@ namespace __EXT__ {
};
}
+class Stash {
+protected:
+ std::vector<token_t> stash;
+
+public:
+ const std::vector<token_t>& get_stash() const {
+ return stash;
+ }
+};
+
template<class T, class H = __EXT__::hash<T> >
-class Tokens {
- private:
- token_t last_id;
- std::vector<token_t> stash;
- __EXT__::hash_map<T, token_t, H> token_to_id;
- __EXT__::hash_map<token_t, T, __EXT__::hash<token_t> > id_to_token;
+class Tokens : public Stash {
+private:
+ token_t last_id;
+ __EXT__::hash_map<T, token_t, H> token_to_id;
+ __EXT__::hash_map<token_t, T, __EXT__::hash<token_t> > id_to_token;
- public:
- Tokens(void) {
- last_id = 0;
- }
-
- token_t lookup(const T &query) {
- return token_to_id[query];
- }
+public:
+ Tokens(void) {
+ last_id = 0;
+ }
+
+ token_t lookup(const T &query) {
+ return token_to_id[query];
+ }
- token_t add(const T &add) {
- token_t tid;
-
- tid = token_to_id[add];
- if (tid == 0) {
- tid = token_to_id[add] = ++last_id;
- id_to_token[tid] = add;
- }
- stash.push_back (tid);
- return tid;
+ token_t add(const T &add) {
+ token_t tid;
+
+ tid = token_to_id[add];
+ if (tid == 0) {
+ tid = token_to_id[add] = ++last_id;
+ id_to_token[tid] = add;
}
-
- void playback (void) {
- for (std::vector<token_t>::iterator i=stash.begin(); i!=stash.end(); ++i) {
- std::cout << *i << " : " << id_to_token[*i] << std::endl;
- }
+ stash.push_back (tid);
+ return tid;
+ }
+
+ void playback (void) {
+ for (std::vector<token_t>::iterator i=stash.begin(); i!=stash.end(); ++i) {
+ std::cout << *i << " : " << id_to_token[*i] << std::endl;
}
+ }
};
#endif
============================================================
--- tokens_test.cc 9c0ec9ccebc6a0e1d4097cd7fc0d50352f941f71
+++ tokens_test.cc 2fbe6a442cbc6b29943b87051b164da7a12c14e3
@@ -1,13 +1,16 @@
#include <iostream>
+#include "markov.hh"
#include "tokens.hh"
-typedef Tokens<std::string> s_tok;
+using namespace std;
+typedef Tokens<string> s_tok;
+
void
string_test (s_tok &t)
{
- std::string s;
+ string s;
t.add ("goat");
t.add ("cheese");
@@ -17,10 +20,10 @@ input_test (s_tok &t)
void
input_test (s_tok &t)
{
- std::string i;
+ string i;
for (int c=0;c<5;c++) {
- std::cin >> i;
+ cin >> i;
t.add (i);
}
}
@@ -33,6 +36,8 @@ test (void)
string_test (t);
input_test (t);
t.playback ();
+
+ MarkovModel m(t, 2);
}
int