Click here to Skip to main content
15,886,067 members
Articles / Desktop Programming / Win32

True Natural Language Understanding through a Conceptual Language Understanding Engine

Rate me:
Please Sign up or sign in to vote.
4.97/5 (55 votes)
11 Jan 201029 min read 67.7K   3.1K   118  
An article introducing an Artificial Intelligence technique that understands and manipulates concepts from text.
/*
The usage of this source code file, including but not limited to the reading, the compilation and 
redistribution, is subject to the license agreement detailed in the license.txt and license.pdf files
which accompany this source code. By proceeding to use this file you are acknowledging that you have
read either license.txt or license.pdf, and that you agree to be bound by the terms and conditions of
the license. If license.txt and license.pdf are not available, you are not licensed this file and you 
must delete it permanently; or contact the author at proy@conceptualspeech.com to obtain a copy of 
the license before proceeding to use this file.

Copyright Philippe Roy 2010 - proy@conceptualspeech.com.
*/

#ifndef __JSOBJECTSUPPORT_H__
#define __JSOBJECTSUPPORT_H__

#pragma warning( disable : 4251 )
#include "v8.h"
#include <string>
#include <exception>
#include <stack>

#include "shared_auto_ptr.h"

#ifdef _DEBUG
#include "afx.h"
#endif

using namespace v8;
using std::string;
using std::exception;
using std::stack;

class CPredicate;
class POSNode;

Handle<ObjectTemplate> JSGetGlobal();

void InitJavascriptGlobal();

class JSTrace
{
	public:
		static JSTrace globTrace;
		JSTrace();
		virtual ~JSTrace();
		void SetSyntacticContext(string dContext);
		void SetEnable(bool value);
		void AppendContext(string label, shared_auto_ptr<CPredicate> wp, shared_auto_ptr<POSNode> curNode);
	protected:
		string m_syntacticContext;
		string m_content;
		bool m_enabled;
};

// An exception class used in case of a mathematical error.

class CMathematicalError: public exception
{
	public:
		CMathematicalError(string& What): m_buffer(What) 
		{
			exception(m_buffer.c_str());
		}
	protected:
		string m_buffer;
};

#pragma warning( disable : 4290 )

template <class T> class JSObjectSupport
{
	public:
		JSObjectSupport();
		Handle<Object> CreateJavascriptInstance();
		void SetToJavascriptVariable(Handle<Context> context, string variableName);
		static T* GetJavascriptVariable(Persistent<Context> context, string variableName);
		static void JavascriptSetup();
	protected:
		static bool m_setUpDone;
		static Handle<FunctionTemplate> m_node_templ;
		static Handle<ObjectTemplate> m_node_proto;
		static Handle<ObjectTemplate> m_node_inst;
};

extern stack<POSNode*> g_nodeContext;
extern stack<string> g_sourceContext;

Handle<Value> ExecuteJavascriptString(Persistent<Context>, string dSourceStr, string name, bool skiptrace = false, Handle<Script> *dScript = NULL) throw(CMathematicalError);

void ExecutePredicateBuilderScript(string dWord);

Handle<Value> NullValue();

#include "DebugDefinitions.h"

template <class T> bool JSObjectSupport<T>::m_setUpDone = false;
template <class T> Handle<FunctionTemplate> JSObjectSupport<T>::m_node_templ;
template <class T> Handle<ObjectTemplate> JSObjectSupport<T>::m_node_proto;
template <class T> Handle<ObjectTemplate> JSObjectSupport<T>::m_node_inst;

template <class T> JSObjectSupport<T>::JSObjectSupport()
{
	if (!m_setUpDone)
	{
		JavascriptSetup();
	}
}

template <class T> Handle<Object> JSObjectSupport<T>::CreateJavascriptInstance()
{
	Handle<Function> node_ctor = m_node_templ->GetFunction();
	Handle<Object> obj = node_ctor->NewInstance();
	T *goodPtr = static_cast<T*>(this);
	obj->SetInternalField(0, External::New(goodPtr));
	return obj;
}

template <class T> void JSObjectSupport<T>::SetToJavascriptVariable(Handle<Context> context, string variableName)
{
	Handle<Object> obj = CreateJavascriptInstance();
	context->Global()->Set(String::New(variableName.c_str()), obj);
}

template <class T> T* JSObjectSupport<T>::GetJavascriptVariable(Persistent<Context> context, string variableName)
{
	try
	{
		Handle<Value> val = ExecuteJavascriptString(context, variableName, "GetJavascriptVariable", true);
		if (val->IsObject())
		{
			Local<External> wrap = Local<External>::Cast(val->ToObject()->GetInternalField(0));
			void* ptr = wrap->Value();
			return (static_cast<T*>(ptr));
		}
		else
		{
			return NULL;
		}
	}
	catch (...)
	{
		return NULL;
	}
}

#pragma warning( default : 4290 )

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Canada Canada
Philippe Roy was a key contributor throughout his 20+ years career with many high-profile companies such as Nuance Communications, IBM (ViaVoice and ProductManager), VoiceBox Technologies, just to name a few. He is creative and proficient in OO coding and design, knowledgeable about the intellectual-property world (he owns many patents), tri-lingual, and passionate about being part of a team that creates great solutions.

Oh yes, I almost forgot to mention, he has a special thing for speech recognition and natural language processing... The magic of first seeing a computer transform something as chaotic as sound and natural language into intelligible and useful output has never left him.

Comments and Discussions