Boost logo

Boost-Commit :

From: phil_at_[hidden]
Date: 2008-08-07 06:56:55


Author: pbouchard
Date: 2008-08-07 06:56:54 EDT (Thu, 07 Aug 2008)
New Revision: 48014
URL: http://svn.boost.org/trac/boost/changeset/48014

Log:
Learning brain kernel.
Added:
   sandbox/shifted_ptr/libs/smart_ptr/example/t100.h (contents, props changed)
Text files modified:
   sandbox/shifted_ptr/libs/smart_ptr/example/regex_test1.cpp | 44 ++++-----------------------------------
   1 files changed, 5 insertions(+), 39 deletions(-)

Modified: sandbox/shifted_ptr/libs/smart_ptr/example/regex_test1.cpp
==============================================================================
--- sandbox/shifted_ptr/libs/smart_ptr/example/regex_test1.cpp (original)
+++ sandbox/shifted_ptr/libs/smart_ptr/example/regex_test1.cpp 2008-08-07 06:56:54 EDT (Thu, 07 Aug 2008)
@@ -1,9 +1,6 @@
 /**
         @file
         regex_test1.cpp
-
- @note
- Brain kernel.
 */
 
 #include <vector>
@@ -12,47 +9,16 @@
 #include <boost/regex.hpp>
 #include <boost/shifted_ptr.hpp>
 
+#include "t100.h"
+
 using namespace std;
 using namespace boost;
+using boost::detail::sh::neuron_sight;
 
 
-struct neuron
-{
- regex e;
- vector< shifted_ptr<neuron> > sub;
-
- neuron(string const & s, shifted<neuron> * p1 = 0, shifted<neuron> * p2 = 0, shifted<neuron> * p3 = 0)
- :
- e(s),
- sub(e.mark_count())
- {
- if (p1) sub[0] = p1;
- if (p2) sub[1] = p2;
- if (p3) sub[2] = p3;
- }
-
- double operator () (std::string const & input)
- {
- match_results<std::string::const_iterator> what;
-
- if (! regex_match(input, what, e, match_default | match_partial))
- return 0;
-
- if (! what[0].matched)
- return 0;
-
- double accuracy = 0;
- for (int i = 1; i < what.size(); i ++)
- if (what[i].matched)
- accuracy += (* sub[i])(what[i].str()) / (what.size() - 1);
-
- return accuracy;
- }
-};
-
 int main(int argv, char * argc[])
 {
- shifted_ptr<neuron> brain = new shifted<neuron>("I eat ([a-z]+) then drink ([a-z]+)", new shifted<neuron>("beef|chicken"), new shifted<neuron>("vodka|water"));
+ shifted_ptr<neuron_sight> t100 = new shifted<neuron_sight>("I eat ([a-z]+) then drink ([a-z]+)", new shifted<neuron_sight>("beef|chicken"), new shifted<neuron_sight>("vodka|water"));
 
- cout << ((* brain)("I eat beef then drink water") > .5 ? "true" : "false") << endl;
+ cout << ((* t100)("I eat beef then drink water") > .5 ? "true" : "false") << endl;
 }

Added: sandbox/shifted_ptr/libs/smart_ptr/example/t100.h
==============================================================================
--- (empty file)
+++ sandbox/shifted_ptr/libs/smart_ptr/example/t100.h 2008-08-07 06:56:54 EDT (Thu, 07 Aug 2008)
@@ -0,0 +1,129 @@
+/**
+ @file
+ T100 brain kernel.
+
+ @note
+ Copyright (c) 2008 Phil Bouchard <phil_at_[hidden]>.
+
+ Distributed under the Boost Software License, Version 1.0.
+
+ See accompanying file LICENSE_1_0.txt or copy at
+ http://www.boost.org/LICENSE_1_0.txt
+
+ See http://www.boost.org/libs/smart_ptr/doc/index.html for documentation.
+*/
+
+
+#ifndef T100_HPP_INCLUDED
+#define T100_HPP_INCLUDED
+
+
+#include <vector>
+#include <string>
+#include <iostream>
+#include <boost/regex.hpp>
+#include <boost/shifted_ptr.hpp>
+
+
+namespace boost
+{
+
+namespace detail
+{
+
+namespace sh
+{
+
+
+struct map_node
+{
+ typedef boost::shifted_ptr<map_node> pointer;
+
+ pointer prev_;
+ pointer next_;
+};
+
+struct neuron_base : map_node
+{
+ typedef boost::shifted_ptr<neuron_base> pointer;
+
+ enum sense_t {sight, sound, touch, smell, taste};
+
+ boost::regex exp_;
+ std::vector<pointer> sub_;
+
+ neuron_base(std::string const & s) : exp_(s), sub_(exp_.mark_count()) {}
+ virtual ~neuron_base() {};
+
+ virtual double operator () (std::string const & input) {};
+};
+
+template <neuron_base::sense_t>
+ struct neuron : neuron_base
+ {
+ typedef boost::shifted<neuron> pointee;
+ typedef std::map<std::string, pointer> map_sn_t;
+
+ static map_sn_t search_;
+
+ neuron(std::string const & s, pointee * p1 = 0, pointee * p2 = 0, pointee * p3 = 0) : neuron_base(s)
+ {
+ search_[s] = reinterpret_cast<pointee *>(this);
+
+ if (p1) sub_[0] = p1;
+ if (p2) sub_[1] = p2;
+ if (p3) sub_[2] = p3;
+ }
+
+ double operator () (std::string const & input)
+ {
+ boost::match_results<std::string::const_iterator> what;
+
+ if (! boost::regex_match(input, what, exp_, boost::match_default | boost::match_partial))
+ return 0;
+
+ if (! what[0].matched)
+ return 0;
+
+ // ponderate
+ double accuracy = 0;
+ for (int i = 1; i < what.size(); i ++)
+ if (what[i].matched)
+ accuracy += (* sub_[i])(what[i].str()) / (what.size() - 1);
+
+ // learn if sounds equitable
+ if (accuracy > .7)
+ for (int i = 1; i < what.size(); i ++)
+ if (! what[i].matched)
+ {
+ typename map_sn_t::iterator j = search_.find(what[i].str());
+
+ if (j != search_.end())
+ sub_[i] = j->second;
+ else
+ ; // pick a guess...
+ }
+
+ return accuracy;
+ }
+ };
+
+template <neuron_base::sense_t I>
+ typename neuron<I>::map_sn_t neuron<I>::search_;
+
+
+typedef neuron<neuron_base::sight> neuron_sight;
+typedef neuron<neuron_base::sound> neuron_sound;
+typedef neuron<neuron_base::touch> neuron_touch;
+typedef neuron<neuron_base::smell> neuron_smell;
+typedef neuron<neuron_base::taste> neuron_taste;
+
+
+} // namespace sh
+
+} // namespace detail
+
+} // namespace boost
+
+
+#endif


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk