home *** CD-ROM | disk | FTP | other *** search
- //=======================================================================
- // (c) Copyright 1990-2003, Bowne Global Solutions, Inc. All Rights Reserved
- //
- // This work is protected by United States and international copyright
- // laws and contains certain trade secret information. All use of this
- // work is governed by the terms of agreements between Bowne Global
- // Solutions and its licensees or potential licensees. Disclosure or
- // disassembly of the contents of embodied programs or algorithms is
- // prohibited. All rights are reserved worldwide.
- //
- // File:
- // MTC++api.h
- //
- // Description:
- // MT API C++ interface / header
- //
- // Cautions:
- //
- //
- // Author:
- // John Zhang
- //
- // Creation Date:
- //
- //
- // Change History:
- // Date: Initials: Comment:
- //=======================================================================
-
- /**
- \file mtc++api.h
- This file contains the supported machine translation API interface. It is this file that
- should be included in any C++ application that utilizes MT SDK
-
- \brief MT API C++ interface
- \copyright Bowne Global Solutions, Inc. 2003
- */
-
- #ifndef MTCPlusPlusAPI_H
- #define MTCPlusPlusAPI_H
-
-
- #include <vector> // STL vectors
- #include <string> // STL strings
- #include <algorithm> //STL algorithms
- #include <sstream> // Standard C++ string-like streams
- #include <exception> // Standard exceptions
- #include "windows.h"
- using namespace std; // STL
-
- #include "MTapi23.h" //C Style API
- using namespace MTApi2; // namespace of the MTtranslator2
-
- #pragma warning( disable : 4786) // 'wstring' as a param creates identifies longer than 255
-
- //-----------------------------------------------------------------------------*
-
- namespace MTCPlusPlusAPI
- {
- ////////////////////////////////////////////////////////////////////////////////
- // MT Variable Types
- //
-
-
- typedef unsigned MTId;
-
- typedef wchar_t MTChar;
-
-
- typedef MTId MTLanguage;
-
- typedef vector<MTLanguage> MTLanguageArray;
- const MTLanguage MTNoLanguage = 0;
-
- typedef MTId MTPofs;
-
- typedef vector<MTPofs> MTPofsArray;
- const MTPofs MTNoPofs = 0;
-
- typedef long MTContext;
- const MTContext MTNoContext = -1;
-
- typedef MTId MTDomain;
-
- typedef vector<MTDomain> MTDomainArray;
- const MTDomain MTNoDomain = 0;
-
- typedef MTId MTOption;
-
- typedef vector<MTOption> MTOptionArray;
-
- typedef MTId MTOptionValue;
-
- typedef vector<MTOptionValue> MTOptionValueArray;
- const MTOptionValue MTNoOptionValue = 0;
-
- typedef int MTIndex;
- const MTIndex MTNoIndex = -1;
-
- typedef int MTOffset;
- const MTOffset MTNoOffset = -1;
-
-
- typedef vector<wstring> MTStringArray;
-
- typedef unsigned long MTCount;
-
- typedef int MTAPIFunctionIndex;
-
- typedef unsigned MTHeadwordIndex;
-
- //#ifndef MTAPI_CALL
- //#define MTAPI_CALL __stdcall
- //#endif
-
- //typedef int (MTAPI_CALL *callBackFuncPtr)();
-
- typedef unsigned MTResult;
- const MTResult MT_OK = 0; // Successful completion
- const MTResult MT_DOC_SUCCESSFUL = 0; // Document translated successfully
- const MTResult MT_FAIL = 101; // returned code by QuerySupporedCalls for
- // the API not supported
-
- const MTResult MTE_BARC_SERVER_ERROR = 102; // internal error in Barcelona engine
- const MTResult MTE_KAMEJIMA_SERVER_ERROR = 103; // internal error in Japanese engine
- const MTResult MTE_CREATE_DOMAIN_FAILED = 104;
- const MTResult MTE_CREATE_WORD_FAILED = 105;
- const MTResult MTE_DELETE_WORD_FAILED = 106;
- const MTResult MTE_DOMAIN_ALREADY_EXIST = 107;
- const MTResult MTE_EMPTY_SOURCE = 108;
- const MTResult MTE_INSUFFICIENT_BUFFER = 109;
- const MTResult MTE_INVALID_DOMAIN_ID = 110;
- const MTResult MTE_INVALID_FILE_FORMAT = 111; // Source document is not a text or HTML file
- const MTResult MTE_INVALID_INTERFACE_LANGUAGE_ID =112;
- const MTResult MTE_INVALID_LANGUAGE_ID = 113;
- const MTResult MTE_INVALID_OPTION_IDENTIFIER = 114;
- const MTResult MTE_INVALID_OPTIONVALUE_IDENTIFIER= 115;
- const MTResult MTE_INVALID_PARAMETER = 116;
- const MTResult MTE_INVALID_POFS_ID = 117;
- const MTResult MTE_INVALID_WORD_ATTRIBUTE = 118;
- const MTResult MTE_LANGUAGE_PAIR_IN_USE = 119;
- const MTResult MTE_NO_REP = 120; // Missing representation object
- const MTResult MTE_NO_TRANSLATOR = 121;
- const MTResult MTE_SETHEADWORD_FAILED = 122;
- const MTResult MTE_UNFOUND_DICTIONARY_ENTRY = 123;
- const MTResult MTE_UNFOUND_MORPHOLOGY = 124;
- const MTResult MTE_UNIMPLEMENTED = 125; // Function not yet implemented
- const MTResult MTE_GUESS_LANGUAGE_FAILED = 126;
- const MTResult MTE_UNSUPPORTED = 127; // Function not supported
- const MTResult MTE_WORD_ALREADY_EXIST = 128;
-
- //the following error codes are for ConvertLIFtoDictionary
- const MTResult MTE_UNABLE_TO_CREATE_CDS_FILE = 300; // Could not open a new .cds file
- const MTResult MTE_UNABLE_TO_OPEN_LIF_FILE = 301; // Unable to open the specified LIF file
- const MTResult MTE_MISSING_ENGINE_LANGUAGE_FILES = 302; // Missing any one of the linguistic files needed by the engine
- const MTResult MTE_HEADWORDS_OUT_OF_ORDER = 303; // Headwords are not in alphabetical order - non fatal
- const MTResult MTE_INVALID_LEMMA_FIELD = 304; // Lemma field in LIF file contans an error
- const MTResult MTE_INVALID_TRANSLATION_FIELD = 305; // Translation field in LIF file contains an error
- const MTResult MTE_INVALID_POS_FIELD = 306; // Error in POS field
- const MTResult MTE_INVALID_ATTRIBUTE_FIELD = 307; // Error in attribute field
- const MTResult MTE_ENTRY_HAS_NO_GENDER = 308; // For Portuguese only - when attribute field has the "NoArticle" attribute
- const MTResult MTE_INVALID_COMMENT_FIELD = 309; // Error in comment field
- const MTResult MTE_INVALID_HEADWORD_FIELD = 310; // Error in headword field
- const MTResult MTE_INVALID_MORPHOLOGY_FIELD = 311; // Invalid morphology field
- const MTResult MTE_INVALID_INITIALS_FIELD = 312;
- const MTResult MTE_INVALID_DATE_FIELD = 313;
- const MTResult MTE_TRANSLATION_INTEGRITY_ISSUE = 314;
-
- // the following error codes are for confidence score API
- const MTResult MTE_NO_CM_SELECTED = 350; // No model has been selected
- const MTResult MTE_NO_CM_ACTIVATED = 351; // No model has been activated
- //-----------------------------------------------------------------------------*
-
- struct SBackMapArrayEntry
- {
- int targetOffset;
- int targetLength;
- int sourceOffset;
- int sourceLength;
- SBackMapArrayEntry()
- {
- targetOffset = -1;
- targetLength = -1;
- sourceOffset = -1;
- sourceLength = -1;
- }
- };
-
- typedef vector<SBackMapArrayEntry> MTBackMapArray;
-
-
- typedef struct
- {
- int offset;
- int len;
- } SOffsetLengthEntry;
-
- typedef vector<SOffsetLengthEntry> MTOffsetLengthArray;
-
-
- typedef struct
- {
- int sourceIndex;
- int targetIndex;
- } SAssociationEntry;
-
- typedef vector<SAssociationEntry> MTAssociationArray;
-
- //-----------------------------------------------------------------------------*
-
- class MTException : public std::exception
- {
- std::string message_;
- public:
- MTException(const std::string& message) : message_(message) {}
- MTException(const MTException& right)
- { message_ = right.message_; }
- MTException& operator = (const MTException& right)
- {
- if (&right == this)
- return *this;
- message_ = right.message_;
- return *this;
- }
- const char* what() const
- { return message_.c_str(); }
- };
-
- struct MTLanguagePair
- {
- MTLanguage l1;
- MTLanguage l2;
- MTLanguagePair () : l1 (MTNoLanguage), l2 (MTNoLanguage) {}
- MTLanguagePair (MTLanguage x1, MTLanguage x2) : l1 (x1), l2 (x2) {}
- };
-
- typedef vector<MTLanguagePair> MTLanguagePairArray;
-
- const unsigned FAIR_SIZE = 64; //so that it can be changed uniformly.
- const unsigned LARGE_SIZE = 256;
-
- /** \class MTapi3
- This is the abstract base class for MT interface classes
- */
- class MTapi3
- {
- public:
- /** Default constructor - loads the engine dll(s)
- The constructor loads the engine dll, loads exported function CreateMTObject,
- and creates the translator object
- */
- MTapi3()
- {
- translator = 0;
-
- // Load library .DLL explicitly
- HINSTANCE library = LoadLibrary("BarcelonaEngineWrapper.dll");
- if (library == NULL)
- {
- // failed to load DLL, log error
- throw MTException("MT engine loading error");
- }
-
- // Load exported function CreateMTObject
- fnCreateMTobject CreateMTObject =
- (fnCreateMTobject)GetProcAddress(library,
- "CreateMTobject");
-
- if (CreateMTObject == NULL)
- throw MTException("CreateMTobject function not found");
-
- translator = CreateMTObject();
-
- if (translator == NULL)
- throw MTException("MT object creation error");
- }
-
-
-
- /**Destructor
- Releases the translator object
- */
- ~MTapi3()
- {
- if (translator)
- {
- translator->ReleaseMT();
- translator = 0;
- }
- }
-
-
- /**This function queries the translation engine for the number of single-direction
- language pairs available for translation.
- \return Number of language pairs available for translation
- Possible errors:
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTCount CountAvailableLanguagePairs()
- {
- unsigned int count = 0;
- translator->CountAvailableLanguagePairs(&count);
- return count;
- }
-
- /** This function queries the translation engine for the number of translation
- options available for the active language pair.
- Some translation engines have special parameters or options that affect how
- sentences, words, or documents are translated. Each human language has certain
- options, such as the masculine and feminine forms of address, and formal and
- informal pronouns.
- \return Number of available translation options for the active language pair
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTCount CountAvailableOptions()
- {
- unsigned int count = 0;
- translator->CountAvailableOptions(&count);
- return count;
- }
-
- /**This function queries the translation engine for the number of discrete option
- values available for the specified translation option.
- Each translation option may take on a set of discrete values.
- \return Number of discrete option values available for the specified translation option
- Possible errors:
- MTE_BARC_SERVER_ERROR - Internal engine error
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_INVALID_OPTION_IDENTIFIER - <c>option</c> parameter is not one of the values reported by <c>GetAvailableOptions</c>
- */
- MTCount CountAvailableOptionValues(MTOption option)
- {
- unsigned int count = 0;
- translator->CountAvailableOptionValues(option, &count);
- return count;
- }
-
- /**This function queries the translation engine for the number of available
- parts of speech for the active language pair.
- \return Number of available parts of speech for the active language pair.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTCount CountAvailablePartsOfSpeech()
- {
- unsigned int count = 0;
- translator->CountAvailablePartsOfSpeech(&count);
- return count;
- }
-
- /**This function gets the number of global variables for the active language pair
- \return Number of global variables for the active language pair
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTCount CountAvailableVariables()
- {
- unsigned int count = 0;
- translator->CountAvailableVariables(&count);
- return count;
- }
-
- /**This function queries the translation engine for the number of specific
- subject domain dictionaries or subject domains available for the active language pair.
- \param domain The domain ID for the subdomain, or zero if we want the count of all
- root domains.
- \return The number of specific subject domain dictionaries or subject domains
- available for the active language pair.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTCount CountAvailableSubjectDomains (MTDomain domain)
- {
- unsigned int count = 0;
- translator->CountAvailableSubjectDomains(domain, &count);
- return count;
- }
-
- /**This function retrieves the active language pair.
- The language pair consists of the source and target language identifiers
- previously selected as the language pair.
- \param langPair A reference to an MTLanguagePair struct. Upon completion,
- field \a l1 of \a langPair contains the source language identifier and field \a l2
- contains the target language identifier.
- \return Error code.
- Possible errors: none
- */
- MTResult GetActiveLanguagePair(MTLanguagePair & langPair)
- {
- MTLanguage srcLang = MTAPI_LANG_NEUTRAL;
- MTLanguage trgLang = MTAPI_LANG_NEUTRAL;
- translator->GetActiveLanguagePair(&srcLang, &trgLang);
-
- langPair.l1 = srcLang;
- langPair.l2 = trgLang;
- return translator->GetMTError();
- }
-
- /**This function retrieves an array of language identifiers for the
- source and target language pairs.
- \param langPairs A reference to an \a MTLanguagePairArray vector. Upon completion,
- each element contains an \a MTLanguagePair struct where field \a l1 contains the source
- language identifier and field \a l2 contains the target language identifier.
- \return Error code.
- Possible errors:
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult GetAvailableLanguagePairs(MTLanguagePairArray & langPairs)
- {
- MTerror nError = MT_OK;
- unsigned nSize = FAIR_SIZE * 2;
- vector<MTLanguage> langArray(nSize, 0);
-
- nError = translator->GetAvailableLanguagePairs( &langArray[0], &nSize );
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- langArray.reserve(nSize);
- fill(langArray.begin(), langArray.end(), 0);
- nError = translator->GetAvailableLanguagePairs( &langArray[0], &nSize );
- }
-
- if (nError == MT_OK)
- {
- langPairs.clear();
- for (unsigned i = 0; i < nSize; i += 2)
- {
- langPairs.push_back(MTLanguagePair(langArray[i], langArray[i+1]));
- }
- }
-
- return translator->GetMTError();
- }
-
-
- /**This function retrieves an array of option identifiers for the
- translation options available for the active language pair.
- \param options A reference to an \a MTOptionArray vector. Upon completion,
- each element contains the list of available option identifiers for the
- active language pair.
- \return Error code.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult GetAvailableOptions(MTOptionArray & options)
- {
- MTerror nError = MT_OK;
- unsigned nSize = FAIR_SIZE;
- vector <MTOption> optArray(nSize, 0);
-
- nError = translator->GetAvailableOptions( &optArray[0], &nSize );
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- optArray.reserve(nSize);
- fill(optArray.begin(), optArray.end(), 0);
- nError = translator->GetAvailableOptions( &optArray[0], &nSize );
- }
-
- options.clear();
- if (nError == MT_OK)
- copy(optArray.begin(), optArray.begin() + nSize, back_inserter(options));
-
- return translator->GetMTError();
- }
-
- /**This function retrieves an array of option value identifiers for the
- specified translation option.
- \param option An option identifier that specifies the option for which values
- are retrieved.
- \param optValues A reference to an MTOptionValueArray vector. Upon completion,
- each element contains the list of available option value identifiers for the
- specified translation option.
- \return Error code.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_INVALID_OPTION_IDENTIFIER - <c>option</c> parameter is not one of the values reported by <c>GetAvailableOptions</c>
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult GetAvailableOptionValues(MTOption option, MTOptionValueArray& optValues)
- {
- MTerror nError = MT_OK;
- unsigned nSize = FAIR_SIZE;
- vector <MTOptionValue> optValueArray(nSize, 0);
-
- nError = translator->GetAvailableOptionValues( option, &optValueArray[0], &nSize );
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- optValueArray.reserve(nSize);
- fill(optValueArray.begin(), optValueArray.end(), 0);
- nError = translator->GetAvailableOptionValues( option, &optValueArray[0], &nSize );
- }
-
- optValues.clear();
- if (nError == MT_OK)
- copy (optValueArray.begin(), optValueArray.begin() + nSize, back_inserter(optValues));
-
- return translator->GetMTError();
- }
-
- /**This function retrieves an array of global variables for the active language pair.
- \param variables A reference to an \a MTStringArray vector. Upon completion, each
- element contains the name of a global variable
- \return Error code.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult GetAvailableVariables(MTStringArray& variables)
- {
- MTerror nError = MT_OK;
- unsigned nSize = LARGE_SIZE;
- vector <MTchar> varNames (nSize);
- varNames[0] = 0;
-
- nError = translator->GetAvailableVariables(&varNames[0], &nSize);
-
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- varNames.resize(nSize);
- varNames[0] = 0;
- nError = translator->GetAvailableVariables(&varNames[0], &nSize);
- }
- //fill the variables
- variables.clear();
- if (nError == MT_OK)
- {
- MTstringarray ptr = &varNames[0];
- wstring aName;
- while( ptr - &varNames[0] < (int)nSize )
- {
- aName = wstring(ptr);
- ptr += aName.size();
- ++ptr; //null-terminate current block
- ++ptr;
- variables.push_back(aName);
- } }
- return translator->GetMTError();
- }
-
-
- /**This function retrieves an array of part of speech identifiers for the parts
- of speech available for the active language pair.
- \param allPofs A reference to an \a MTPofSArray vector where each element contains
- the list of part-of-speech identifiers for the active language pair.
- \return Error code.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult GetAvailablePartsOfSpeech(MTPofsArray & allPofs)
- {
- MTerror nError = MT_OK;
- unsigned nSize = FAIR_SIZE;
- vector <MTPofs> pofsArray(nSize, 0);
-
- nError = translator->GetAvailablePartsOfSpeech( &pofsArray[0], &nSize );
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- pofsArray = vector<MTPofs>(nSize,0);
- nError = translator->GetAvailablePartsOfSpeech( &pofsArray[0], &nSize );
- }
- allPofs.clear();
- if (nError == MT_OK)
- copy (pofsArray.begin(), pofsArray.begin() + nSize, back_inserter(allPofs));
-
- return translator->GetMTError();
- }
-
-
- /**This function retrieves an array of subject domain descriptions under the
- specified subject domain that are available for the active language pair.
- \param domain Specifies the subject domain identifier whose sub domains are to
- be retrieved. A value of zero or \a MT_DOMAIN_UNSPECIFIED returns the number of root
- domains.
- \param domains A reference to an \a MTStringArray vector where each element contains
- the text description of a subject domain.
- \return Error code.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult GetAvailableSubjectDomains(MTDomain domain, MTStringArray& domains)
- {
- MTerror nError = MT_OK;
-
- unsigned nSize = LARGE_SIZE;
- vector <MTchar> domainNames(nSize);
- domainNames[0] = 0;
-
- nError = translator->GetAvailableSubjectDomains(domain, &domainNames[0], &nSize);
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- domainNames = vector <MTchar>(nSize);
- domainNames[0] = 0;
- nError = translator->GetAvailableSubjectDomains(domain, &domainNames[0], &nSize);
- }
-
- //fill the domains
- domains.clear();
- if (nError == MT_OK)
- {
- MTstringarray ptr;
- ptr = &domainNames[0];
- unsigned curPos = 0;
- wstring aName;
- while( ptr - &domainNames[0] < (int)nSize )
- {
- aName = wstring(ptr);
- ptr += aName.size();
- ++ptr; //null-terminate current block
- ++ptr;
- domains.push_back(aName);
- }
- }
- return translator->GetMTError();
- }
-
-
- /**This function retrieves an array of subject domain identifiers under the
- specified subject domain that are available for the active language pair.
- \param domain Specifies the subject domain identifier whose sub domains are to
- be retrieved. A value of zero or \a MT_DOMAIN_UNSPECIFIED returns the number of root
- domains.
- \param domains A reference to an \a MTDomainArray vector where each element contains
- the domain identifier of a subject domain.
- \return Error code.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult GetAvailableSubjectDomains(MTDomain domain, MTDomainArray& domains)
- {
- MTerror nError = MT_OK;
- unsigned nSize = FAIR_SIZE;
- vector<MTDomain> domainArray(nSize, 0);
-
- nError = translator->GetAvailableSubjectDomains( domain, &domainArray[0], &nSize );
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- domainArray = vector<MTDomain>(nSize, 0);
- nError = translator->GetAvailableSubjectDomains( domain, &domainArray[0], &nSize );
- }
-
- if (nError == MT_OK)
- copy (domainArray.begin(), domainArray.begin() + nSize, back_inserter(domains));
-
- return translator->GetMTError();
- }
-
- /**This function retrieves the description of the specified language identifier
- localized in the specified interface language.
- \param interfaceLang A language identifier of the interface language to use for
- localization of the returned language description.
- \param lang A language identifier of the language text description to be returned.
- \return A string that contains the description of the input language identifier
- localized in the specified interface language.
- Possible errors:
- MTE_INVALID_INTERFACE_LANGUAGE_ID - Language id is not MTAPI_LANG_ENGLISH (9 under MS Windows)
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- wstring GetLanguageDescription(MTLanguage interfaceLang, MTLanguage lang)
- {
- MTerror nError = MT_OK;
- unsigned nSize = FAIR_SIZE;
-
- vector <MTChar> langDescription(nSize);
- langDescription[0] = 0;
-
- nError = translator->GetLanguageDescription( interfaceLang, lang, &langDescription[0], &nSize);
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- langDescription = vector<MTChar>(nSize);
- langDescription[0] = 0;
- nError = translator->GetLanguageDescription( interfaceLang, lang, &langDescription[0], &nSize);
- }
-
- wstring langDesrpt;
- if (nError == MT_OK)
- langDesrpt = &langDescription[0];
-
- return langDesrpt;
- }
-
- /**This function retrieves a description of the specified option identifier
- localized in the specified interface language.
- \param interfaceLang A language identifier of the interface language to use for
- localization of the returned option description.
- \param option A translation option identifier.
- \return A string that contains the description of the specified option identifier
- localized in the specified interface language.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_INVALID_INTERFACE_LANGUAGE_ID - Language id is not MTAPI_LANG_ENGLISH (9 under MS Windows)
- MTE_INVALID_OPTION_IDENTIFIER - <c>option</c> parameter is not one of the values reported by <c>GetAvailableOptions</c>
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- wstring GetOptionDescription(MTLanguage interfaceLang, MTOption option)
- {
- MTerror nError = MT_OK;
- unsigned nSize = FAIR_SIZE;
-
- vector <MTChar> optDescription (nSize);
- optDescription[0] = 0;
-
- nError = translator->GetOptionDescription( interfaceLang, option, &optDescription[0], &nSize);
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- optDescription = vector<MTChar> (nSize);
- optDescription[0] = 0;
- nError = translator->GetOptionDescription( interfaceLang, option, &optDescription[0], &nSize);
- }
-
- wstring optDesrpt;
- if (nError == MT_OK)
- optDesrpt = &optDescription[0];
-
- return optDesrpt;
- }
-
- /**This function retrieves the description of the specified option value identifier
- localized in the specified interface language.
- \param interfaceLang A language identifier of the interface language to use for
- localization of the returned option value description.
- \param option A translation option identifier whose selected value description is
- to be returned.
- \param optValue An option value identifier whose description is to be returned.
- \return A string that contains the description of the specified option value
- identifier localized in the specified interface language
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_INVALID_INTERFACE_LANGUAGE_ID - Language id is not MTAPI_LANG_ENGLISH (9 under MS Windows)
- MTE_INVALID_OPTION_IDENTIFIER - <c>option</c> parameter is not one of the values reported by <c>GetAvailableOptions</c>
- MTE_INVALID_PARAMETER - Internal engine error
- MTE_INVALID_LANGUAGE_ID - Internal engine error
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- wstring GetOptionValueDescription(MTLanguage interfaceLang, MTOption option, MTOptionValue optValue)
- {
- MTerror nError = MT_OK;
- unsigned nSize = FAIR_SIZE;
-
- vector<MTChar> optDescription (nSize);
- optDescription[0] = 0;
-
- nError = translator->GetOptionValueDescription( interfaceLang, option, optValue, &optDescription[0], &nSize);
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- optDescription = vector<MTChar>(nSize);
- optDescription[0] = 0;
- nError = translator->GetOptionValueDescription( interfaceLang, option, optValue, &optDescription[0], &nSize);
- }
-
- wstring optDesrpt;
- if (nError == MT_OK)
- optDesrpt = &optDescription[0];
-
- return optDesrpt;
- }
-
- /**This function retrieves the description of the specified part-of-speech
- identifier localized in the specified interface language.
- \param interfaceLang A language identifier of the interface language to use for
- localization of the returned part of speech description.
- \param pofs A part-of-speech identifier.
- \return A string that contains the description of the part-of-speech identifier
- localized in the specified interface language.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_INVALID_INTERFACE_LANGUAGE_ID - Language id is not MTAPI_LANG_ENGLISH (9 under MS Windows)
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- wstring GetPartOfSpeechDescription(MTLanguage interfaceLang, MTPofs pofs)
- {
- MTerror nError = MT_OK;
- unsigned nSize = FAIR_SIZE;
-
- vector <MTChar> pofsDescription(nSize);
- pofsDescription[0] = 0;
-
- nError = translator->GetPartOfSpeechDescription( interfaceLang, pofs, &pofsDescription[0], &nSize );
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- pofsDescription = vector<MTChar> (nSize);
- pofsDescription[0] = 0;
- nError = translator->GetPartOfSpeechDescription( interfaceLang, pofs, &pofsDescription[0], &nSize );
- }
-
- wstring pofsDesrpt;
- if (nError == MT_OK)
- pofsDesrpt = &pofsDescription[0];
-
- return pofsDesrpt;
- }
-
- /**This function retrieves the description of the specified subject domain
- identifier localized in the specified interface language.
- \param interfaceLang A language identifier of the interface language to use for
- localization of the returned subject domain description.
- \param domain A subject domain identifier.
- \return A string that contains the description of the specified subject domain
- identifier localized in the specified interface language.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_INVALID_INTERFACE_LANGUAGE_ID - Language id is not MTAPI_LANG_ENGLISH (9 under MS Windows)
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- wstring GetSubjectDomainDescription(MTLanguage interfaceLang, MTDomain domain)
- {
- MTerror nError = MT_OK;
- unsigned nSize = FAIR_SIZE;
-
- vector <MTChar> domainDescription (nSize);
- domainDescription[0] = 0;
-
- nError = translator->GetSubjectDomainDescription( interfaceLang, domain, &domainDescription[0], &nSize );
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- domainDescription = vector<MTChar>(nSize);
- domainDescription[0] = 0;
- nError = translator->GetSubjectDomainDescription( interfaceLang, domain, &domainDescription[0], &nSize );
- }
-
- wstring domainDesrpt;
- if (nError == MT_OK)
- domainDesrpt = &domainDescription[0];
-
- return domainDesrpt;
- }
-
- /**This function sets the active language pair. This language pair consists of a
- source and target language identifier for translation.
- \param srcLang The language identifier that specifies the source language for the
- language pair.
- \param trgLang The language identifier that specifies the target language for the
- language pair.
- \return Error code.
- Possible errors:
- MTE_INVALID_LANGUAGE_ID - Invalid source language, target language or language pair
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult SetActiveLanguagePair(const MTLanguage srcLang, const MTLanguage trgLang)
- {
- translator->SetActiveLanguagePair( srcLang, trgLang);
- return translator->GetMTError();
- }
-
-
-
- /**This function loads all available language pairs from the specified directory.
- The dictionary and rule files for the specified languages must be in the specified
- directory.
- \param path A string that specifies the directory path where the language pair(s)
- are located.
- \return Error code.
- Possible errors:
- MTE_INVALID_PARAMETER - <c>path</c> parameter is invalid path or no language pairs found in the path
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult LoadAllLanguagePairs(const wstring& path)
- {
- translator->LoadAllLanguagePairs( (const MTstring )path.c_str() );
- return translator->GetMTError();
- }
-
-
- /**This function loads the requested subject domain dictionary for the specified
- language pair into memory for the translation engine to use.
- \param langPairID A language pair to load.
- \param path A string that specifies the directory path where the language pair(s)
- are located.
- \param domain The domain ID of the requested domainName
- \param domainName A string that specifies the domain (dictionary) name to load.
- \return Error code.
- Possible errors:
- MTE_INVALID_PARAMETER - Either <c>domainName</c> is "GEN" or internal engine error
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult LoadDomainDictionary(const MTLanguagePair& langPairID, const wstring& path,
- MTDomain& domain, const wstring& domainName )
- {
- translator->LoadDomainDictionary(langPairID.l1, langPairID.l2, (const MTstring )path.c_str(),
- &domain, (const MTstring )domainName.c_str() );
- return translator->GetMTError();
- }
-
- /**This function loads a specified language pair from the specified directory.
- \param langPairID A language pair to load.
- \param path A string that specifies the directory path where the language pair(s)
- are located.
- \return Error code.
- Possible errors:
- MTE_INVALID_PARAMETER - <c>path</c> parameter is invalid path or no language pairs found in the path
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult LoadLanguagePair(const MTLanguagePair& langPairID, const wstring& path)
- {
- translator->LoadLanguagePair(langPairID.l1, langPairID.l2, (const MTstring)path.c_str());
- return translator->GetMTError();
- }
-
- /**This function unloads all previously loaded language pairs, and closes all
- corresponding rule and dictionary files.
- \return Error code.
- Possible errors:
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult UnloadAllLanguagePairs()
- {
- translator->UnloadAllLanguagePairs();
- return translator->GetMTError();
- }
-
- /**function unloads the requested subject domain dictionary for the specified
- language pair. This function also unloads the requested user-created (custom)
- dictionary.
- \result Error code.
- Possible errors:
- MTE_INVALID_PARAMETER - <c>domain</c> is 0 (GEN)
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult UnloadDomainDictionary (const MTLanguagePair& langPairID, MTDomain domain )
- {
- translator->UnloadDomainDictionary(langPairID.l1, langPairID.l2, domain );
- return translator->GetMTError();
- }
-
-
- /**This function unloads the specified source-target language pair and closes the
- corresponding rule and dictionary files.
- \return Error code.
- Possible errors:
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult UnloadLanguagePair(const MTLanguagePair& langPairID)
- {
- translator->UnloadLanguagePair(langPairID.l1, langPairID.l2 );
- return translator->GetMTError();
- }
-
-
- /**This function frees the memory allocated for the machine translation engine and
- restores all resources to the system.
- \result Error code.
- Possible errors: none
- */
- MTResult ReleaseMT( void )
- {
- translator->ReleaseMT();
- translator = 0;
- return MT_OK;
- }
-
- protected:
- MTtranslator2 * translator;
- };
-
-
- //-----------------------------------------------------------------------------*
-
- /** \class MTdictionary3
- Implements some dictionary related functions
- */
- class MTdictionary3 : public MTapi3
- {
- public:
- //! Default constructor
- MTdictionary3(){}
-
-
- //! Destructor
- virtual ~MTdictionary3(){}
-
-
- /**This function creates a new subject domain with its description.
-
- The file extension of custom dictionaries is CDS. The file names begin
- with the language direction; followed by the name you give the domain when
- you create it. For example, if you create a custom dictionary and name it
- "Astronomy" using the English-French language pair, the file names are:
- efastronomy.cds and feastronomy.cds.
-
- \param domainName The name of the new domain.
- \param domainDescription The description of the new domain (optional).
- \return Error code.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_CREATE_DOMAIN_FAILED - <c>domainName</c> is either "user" or "gen" (case insensitive)
- MTE_DOMAIN_ALREADY_EXIST - The domain is already loaded
- MTE_INVALID_LANGUAGE_ID - The inverse direction is not loaded
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult CreateDomain(const wstring & domainName, const wstring &domainDescription)
- {
- translator->CreateDomain((const MTstring)domainName.c_str(), (const MTstring)domainDescription.c_str());
- return translator->GetMTError();
- }
-
- /**This function creates a new lexical entry.
- This lexical entry specifies both the headword and translation of the new
- lexical entry.
- \param translation The lexical entry for creation. This string is in word/attribute
- format and must contain at least a headword and translation field.
- \return Error code.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_INVALID_WORD_ATTRIBUTE - <c>translation</c> missing one of: headword, translation, pofs, morphology, or domain is invalid
- MTE_WORD_ALREADY_EXIST - The translation is already in the dictionary
- MTE_CREATE_WORD_FAILED - Internal engine error
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult CreateTranslation(const wstring & translation)
- {
- translator->CreateTranslation((const MTstring)translation.c_str());
- return translator->GetMTError();
- }
-
- /**This function deletes a lexical entry.
- \param translation The lexical entry for deletion. This string is in word/attribute
- format and must contain a headword and translation field.
- \return Error code.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_INVALID_WORD_ATTRIBUTE - <c>translation</c> missing one of: headword, translation, pofs, morphology, or domain is invalid
- MTE_UNFOUND_DICTIONARY_ENTRY - The translation was not found
- MTE_DELETE_WORD_FAILED - Internal engine error
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult DeleteTranslation(const wstring & translation)
- {
- translator->DeleteTranslation((const MTstring)translation.c_str());
- return translator->GetMTError();
- }
-
- /** This function retrieves a list of possible morphology attributes, or inflection
- patterns, for the uninflected headword.
- \param word The uninflected headword for which morphology attributes are retrieved.
- This string is in word/attribute format and must contain a headword and translation
- field.
- \return Error code.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_INVALID_WORD_ATTRIBUTE - <c>translation</c> missing one of: word, pofs
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTStringArray GetAvailableMorphologies(const wstring & word)
- {
- MTerror nError = MT_OK;
- MTStringArray availableMorphs;
- unsigned nSize = LARGE_SIZE;
- vector <MTChar> morphWords(nSize);
- morphWords[0] = 0;
-
- nError = translator->GetAvailableMorphologies( (const MTstring)word.c_str(), &morphWords[0], &nSize );
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- morphWords = vector<MTChar>(nSize);
- morphWords[0] = 0;
- nError = translator->GetAvailableMorphologies( (const MTstring)word.c_str(), &morphWords[0], &nSize );
- }
-
- //fill the availableMorphs
- if (nError == MT_OK)
- {
- MTstringarray ptr;
- ptr = &morphWords[0];
- unsigned curPos = 0;
- wstring aMorph;
- while( ptr - &morphWords[0] < (int)nSize )
- {
- aMorph = wstring(ptr);
- ptr += aMorph.size();
- ++ptr; //null-terminate current block
- ++ptr;
- availableMorphs.push_back(aMorph);
- }
- }
- return availableMorphs;
- }
-
-
- /**Not implemented
-
- */
- wstring GetHeadword(MTHeadwordIndex headIndex)
- {
- MTerror nError = MT_OK;
- unsigned nSize = FAIR_SIZE;
- vector<MTChar> head(nSize);
- head[0] = 0;
-
- nError = translator->GetHeadword( headIndex, &head[0], &nSize );
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- head = vector<MTChar>(nSize);
- head[0] = 0;
- nError = translator->GetHeadword( headIndex, &head[0], &nSize );
- }
-
- wstring headWord;
- if (nError == MT_OK)
- headWord = wstring(&head[0], nSize);
-
- return headWord;
- }
-
- /**Not implemented
- */
- MTCount GetHeadwordCount()
- {
- MTerror nError = MT_OK;
- unsigned int headCount = 0;
- nError = translator->GetHeadwordCount(&headCount);
- return headCount;
- }
-
- /**Not implemented
- */
- MTHeadwordIndex GetHeadwordIndex(const wstring & head)
- {
- MTerror nError = MT_OK;
- unsigned int headIndex = 0;
- nError = translator->GetHeadwordIndex( (const MTstring)head.c_str(), &headIndex);
- return headIndex;
- }
-
- /**This function retrieves the translation entries for the specified headword.
-
- More information about the headword, such as part of speech and word attributes,
- may be needed to obtain the best translation. The headword parameter must be in
- word/attribute format.
-
- The output array is in word/attribute format and contains translation and headword fields. The
- order of the elements in the array specifies the priority of each translation entry. The first element
- in the array has the highest priority; it is from the highest priority subject domain that contains an
- appropriate headword.
- \param head The translation entries for the specified headword.
- \return The translation entries for the specified headword.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_INVALID_WORD_ATTRIBUTE - <c>translation</c> missing the word attribute
- MTE_UNFOUND_DICTIONARY_ENTRY - The word was not found in the loaded dictionaries.
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTStringArray GetHeadwordTranslations(const wstring & head)
- {
- MTerror nError = MT_OK;
- unsigned nSize = LARGE_SIZE;
- vector<MTChar> headTranslations(nSize);
-
- nError = translator->GetHeadwordTranslations( (const MTstring)head.c_str(), &headTranslations[0], &nSize );
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- headTranslations = vector<MTChar>(nSize);
- headTranslations[0] = 0;
- nError = translator->GetHeadwordTranslations( (const MTstring)head.c_str(), &headTranslations[0], &nSize );
- }
-
- //fill the headTransArray
- MTStringArray headTransArray;
- if (nError == MT_OK)
- {
- MTstringarray ptr;
- ptr = &headTranslations[0];
- unsigned curPos = 0;
- wstring aHeadTran;
- while( ptr - &headTranslations[0] < (int)nSize )
- {
- aHeadTran = wstring(ptr);
- ptr += aHeadTran.size();
- ++ptr; //null-terminate current block
- ++ptr;
- headTransArray.push_back(aHeadTran);
- }
- }
- return headTransArray;
- }
-
- /**This function retrieves the headword that alphabetically follows the specified
- reference word.
- \param head The word used to retrieve the next headword. This string is in
- word/attribute format and must contain a headword and translation field.
- \return The next headword that alphabetically follows the input headword.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- wstring GetNextHeadword(const wstring & head)
- {
- MTerror nError = MT_OK;
- unsigned nSize = FAIR_SIZE;
- vector<MTChar> nextHeadTmp(nSize);
- nextHeadTmp[0] = 0;
-
- nError = translator->GetNextHeadword( (const MTstring)head.c_str(), &nextHeadTmp[0], &nSize );
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- nextHeadTmp = vector<MTChar> (nSize);
- nextHeadTmp[0] = 0;
- nError = translator->GetNextHeadword( (const MTstring)head.c_str(), &nextHeadTmp[0], &nSize );
- }
-
- wstring nextHead = L"";
- if (nError == MT_OK)
- nextHead = wstring(&nextHeadTmp[0], nSize);
-
- return nextHead;
- }
-
- /**This function retrieves the headword that alphabetically precedes the
- specified reference word.
- \param head The reference word used to retrieve the preceding headword. This string
- is in word/attribute format and must contain a headword and translation field.
- \return The previous headword that alphabetically precedes the input headword.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- wstring GetPreviousHeadword(const wstring & head)
- {
- MTerror nError = MT_OK;
- unsigned nSize = FAIR_SIZE;
- vector<MTChar> preHeadTmp(nSize);
- preHeadTmp[0] = 0;
-
- nError = translator->GetPreviousHeadword( (const MTstring)head.c_str(), &preHeadTmp[0], &nSize );
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- preHeadTmp = vector<MTChar> (nSize);
- preHeadTmp[0] = 0;
- nError = translator->GetPreviousHeadword( (const MTstring)head.c_str(), &preHeadTmp[0], &nSize );
- }
-
- wstring preHead = L"";
- if (nError == MT_OK)
- preHead = wstring(&preHeadTmp[0], nSize);
-
- return preHead;
- }
-
-
- /**This function retrieves a list of possible inflected forms, or morphologies,
- of the specified word.
- \param word The word for inflection patterns to be retrieved. This string is in
- word/attribute format and must contain a headword and translation field.
- \return The inflected forms or morphologies of the specified word.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_INVALID_WORD_ATTRIBUTE - <c>translation</c> missing one of: word, pofs or morphology
- MTE_UNFOUND_DICTIONARY_ENTRY - The word does not inflect (i.e. "the")
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTStringArray InflectWord(const wstring & word)
- {
- MTerror nError = MT_OK;
- unsigned nSize = LARGE_SIZE;
- vector <MTchar> inflectWords(nSize);
- inflectWords[0] = 0;
-
- nError = translator->InflectWord( (const MTstring)word.c_str(), &inflectWords[0], &nSize );
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- inflectWords = vector<MTchar>(nSize);
- inflectWords[0] = 0;
- nError = translator->InflectWord( (const MTstring)word.c_str(), &inflectWords[0], &nSize );
- }
-
- //fill the inflectWordArray
- MTStringArray inflectWordArray;
- if (nError == MT_OK)
- {
- MTstringarray ptr;
- ptr = &inflectWords[0];
- unsigned curPos = 0;
- wstring aInflectWord;
- while( ptr - &inflectWords[0] < (int)nSize )
- {
- aInflectWord = wstring(ptr);
- ptr += aInflectWord.size();
- ++ptr; //null-terminate current block
- ++ptr;
- inflectWordArray.push_back(aInflectWord);
- }
- }
-
- return inflectWordArray;
- }
-
- /**This function retrieves a list of lexical headwords that contain translations
- for the uninflected form of the specified word. It performs reverse morphological
- headword lookup using the specified word.
- \param word The word for lookup. This string is in word/attribute format and must
- contain a headword and translation field.
- \return The list of lexical headwords that contain translations for the uninflected
- form of the specified word.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_UNFOUND_DICTIONARY_ENTRY - The word was not found
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTStringArray Lookup (const wstring & word)
- {
- MTerror nError = MT_OK;
- unsigned nSize = LARGE_SIZE;
- vector<MTchar> lookupWords(nSize);
- lookupWords[0] = 0;
-
- nError = translator->Lookup( (const MTstring)word.c_str(), &lookupWords[0], &nSize );
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- lookupWords = vector<MTchar>(nSize);
- lookupWords[0] = 0;
- nError = translator->Lookup( (const MTstring)word.c_str(), &lookupWords[0], &nSize );
- }
-
- //fill the lookupWordArray
- MTStringArray lookupWordArray;
- if (nError == MT_OK)
- {
- MTstringarray ptr;
- ptr = &lookupWords[0];
- unsigned curPos = 0;
- wstring aWord;
- while( ptr - &lookupWords[0] < (int)nSize )
- {
- aWord = wstring(ptr);
- ptr += aWord.size();
- ++ptr; //null-terminate current block
- ++ptr;
- lookupWordArray.push_back(aWord);
- }
- }
-
- return lookupWordArray;
- }
-
- /**This function sets the translation entries for the specified headword.
- \param head The headword for which translations are set. This string must
- be in word/attribute format and must contain a headword field.
- \param transArray Translation entries for the specified headword. Each string
- is in word/attribute format and must contain a translation field.
- \return Error code.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_INVALID_WORD_ATTRIBUTE - <c>head</c> missing word attribute, or in any string from <c>transArray</c>
- one of: translation, pofs, morphology is missing, or domain is invalid
- MTE_SETHEADWORD_FAILED - Internal engine error
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult SetHeadwordTranslations(const wstring & head, const MTStringArray & transArray)
- {
- MTerror nError = MT_OK;
-
- //put all the strings in buffer translations
- vector <MTchar> translations;
- MTStringArray::const_iterator iter = transArray.begin();
- for (; iter != transArray.end(); ++iter)
- {
- copy(iter->begin(), iter->end(), back_inserter(translations));
- translations.push_back('\0');
- }
- translations.push_back('\0');
-
- translator->SetHeadwordTranslations((const MTstring)head.c_str(), &translations[0]);
-
- return translator->GetMTError();
- }
-
-
- /**This function converts the specified Lexical Interchange File (LIF) to a
- dictionary that is compatible with the Barcelona engine.
- \param lifFileName The path and the file name of the LIF file to convert.
- \param cdsFileName The path where the dictionary file should be created.
- When a call is successful, the path contains a converted LIF file with the
- same name as the parameter \a lifFileName with a ô.CDSö extension.
- \param description A string that describes the contents of the dictionary.
- \return Error code.
- Possible errors:
- MTE_UNABLE_TO_CREATE_CDS_FILE = 300; // Could not open a new .cds file
- MTE_UNABLE_TO_OPEN_LIF_FILE = 301; // Unable to open the specified LIF file
- MTE_MISSING_ENGINE_LANGUAGE_FILES = 302; // Missing any one of the linguistic files needed by the engine
- MTE_HEADWORDS_OUT_OF_ORDER = 303; // Headwords are not in alphabetical order - non fatal
- MTE_INVALID_LEMMA_FIELD = 304; // Lemma field in LIF file contans an error
- MTE_INVALID_TRANSLATION_FIELD = 305; // Translation field in LIF file contains an error
- MTE_INVALID_POS_FIELD = 306; // Error in POS field
- MTE_INVALID_ATTRIBUTE_FIELD = 307; // Error in attribute field
- MTE_ENTRY_HAS_NO_GENDER = 308; // For Portuguese only - when attribute field has the "NoArticle" attribute
- MTE_INVALID_COMMENT_FIELD = 309; // Error in comment field
- MTE_INVALID_HEADWORD_FIELD = 310; // Error in headword field
- MTE_INVALID_MORPHOLOGY_FIELD = 311; // Invalid morphology field
- MTE_INVALID_INITIALS_FIELD = 312;
- MTE_INVALID_DATE_FIELD = 313;
- MTE_TRANSLATION_INTEGRITY_ISSUE = 314;
- // */
- MTResult ConvertLIFtoDictionary (const wstring & lifFileName, const wstring & cdsFileName, const wstring & description)
- {
- MTerror nError = MT_OK;
- nError = translator->ConvertLIFtoDictionary( (const MTstring)lifFileName.c_str(),
- (const MTstring)cdsFileName.c_str(),
- (const MTstring)description.c_str() );
- return translator->GetMTError();
- }
- };
-
-
-
-
-
- /** \class MTtranslator3
- Implements translation functions
- */
- class MTtranslator3 : public MTdictionary3
- {
- public:
- //! Default constructor
- MTtranslator3(){}
-
-
- //! Destructor
- virtual ~MTtranslator3(){}
-
-
- /**This function retrieves the array of subject domain identifiers that specify
- the lookup order of the subject domains. The first element in the array has the
- highest priority.
-
- The API obtains the current domain priority dictionary and returns an ordinal
- value that corresponds to the number of dictionary stacks. The stack value indicates
- the priority placement of the dictionaries.
- \param domains Upon completion, each element contains the list of subject domain
- identifiers specifying the prioritized subject domains. Absence of a subject domain
- from this list indicates that the translation engine ignores it.
- \return Error code.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult GetDomainPriority(MTDomainArray & domains)
- {
- MTerror nError = MT_OK;
- unsigned nSize = FAIR_SIZE;
-
- vector<MTDomain> domainArray(nSize, 0);
-
- nError = translator->GetDomainPriority( &domainArray[0], &nSize );
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- domainArray = vector<MTDomain>(nSize, 0);
- nError = translator->GetDomainPriority( &domainArray[0], &nSize );
- }
-
- if (nError == MT_OK)
- copy (domainArray.begin(), domainArray.begin() + nSize, back_inserter(domains));
-
- return translator->GetMTError();
- }
-
-
- /**This function returns the calling threadÆs last-error code value.
- A call to this function resets the threadÆs last-error code value to \a MT_OK.
- \return Error code.
- Possible errors: none
- */
- MTResult GetMTError()
- {
- return translator->GetMTError();
- }
-
-
- /**This function retrieves the identifier of the selected option value for the
- specified option.
- \param option A translation option identifier whose selected value is returned.
- \return The selected option value for the specified option.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_INVALID_OPTION_IDENTIFIER - <c>option</c> parameter is not one of the values reported by <c>GetAvailableOptions</c>
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTOptionValue GetOptionValue (MTOption option)
- {
- MTOptionValue optValue = 0;
- translator->GetOptionValue( option, &optValue );
- return optValue;
- }
-
-
- /**This function retrieves the value of the specified global variable
- \param variable Global variable for which the value is returned
- \return The global variable value
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_INVALID_PARAMETER - <c>variable</c> parameter is not one of the values reported by <c>GetAvailableVariables</c>
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- wstring GetVariableValue(const wstring& variable)
- {
- MTerror nError = MT_OK;
- unsigned nSize = FAIR_SIZE;
-
- vector <MTchar> val(nSize);
- val[0] = 0;
- nError = translator->GetVariableValue((wchar_t*)variable.c_str(), &val[0], &nSize);
-
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- val.resize(nSize);
- val[0] = 0;
- nError = translator->GetVariableValue((wchar_t*)variable.c_str(), &val[0], &nSize);
- }
- if (nError == MT_OK)
- return &val[0];
- else
- return L"";
- }
-
-
- /**This function parses and stores the word(s) from the input text into
- the array of strings.
- \param text The source sentence to be surface parsed.
- \param words Parsed words in source sentence.
- \return Error code.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult ParseWordsFromSentence(const wstring & text, MTStringArray & words)
- {
- MTerror nError = MT_OK;
- unsigned nSize = LARGE_SIZE;
- vector<MTchar> parsedWords(nSize);
- parsedWords[0] = 0;
-
- nError = translator->ParseWordsFromSentence( (const MTstring)text.c_str(), &parsedWords[0], &nSize);
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- parsedWords = vector<MTchar> (nSize);
- parsedWords[0] = 0;
- nError = translator->ParseWordsFromSentence((const MTstring)text.c_str(), &parsedWords[0], &nSize);
- }
-
- //fill the parsedWords
- words.clear();
- if (nError == MT_OK)
- {
- MTstringarray ptr;
- ptr = &parsedWords[0];
- unsigned curPos = 0;
- wstring aWord;
- while( ptr - &parsedWords[0] < (int)nSize )
- {
- aWord = wstring(ptr);
- ptr += aWord.size();
- ++ptr; //null-terminate current block
- ++ptr;
- words.push_back(aWord);
- }
- }
-
- return translator->GetMTError();
- }
-
-
- /** This function tokenizes a sentence in a way that reflects the
- initial level of nodes visible to a linguist. In contrast to ParseWordsFromSentence(),
- this also include lexicon lookup and postprocessing.
- <p> This function must not share memory with the functions in the underlying dlls
- it calls (BarcEng6.dll and BarcelonaEngineWrapper.dll).
- Thus, the vector of strings is passed as a simple byte stream.
- \param text The source sentence to be tokenized.
- \param tokens The tokens of the sentence. This is an output parameter.
- \return Error code. Possible error codes are
- <ul>
- <li> MT_OK: No error occurred
- <li> MTE_NO_TRANSLATOR: No language pair is set
- <li> MTE_BARC_SERVER_ERROR: Internal engine error
- </ul>
- */
- MTResult EngineTokenize( const wstring & text, MTStringArray & tokens)
- {
- MTerror nError = MT_OK;
- unsigned nSize = LARGE_SIZE;
- vector<MTchar> parsedWords( nSize);
- parsedWords[0] = 0;
-
- nError = translator->EngineTokenize( (const MTstring)text.c_str(), &parsedWords[0], &nSize);
- if (nError == MTE_INSUFFICIENT_BUFFER )
- {
- // Provide a vector that is long enough and try again
- parsedWords = vector<MTchar> (nSize);
- parsedWords[0] = 0;
- nError = translator->EngineTokenize( (const MTstring)text.c_str(), &parsedWords[0], &nSize);
- }
-
- // Fill the result tokens
- tokens.clear();
- if (nError == MT_OK)
- {
- MTstringarray ptr;
- ptr = &parsedWords[0];
- unsigned curPos = 0;
- wstring aWord;
- while( ptr - &parsedWords[0] < (int)nSize )
- {
- aWord = wstring(ptr);
- ptr += aWord.size();
- ++ptr; //null-terminate current block
- ++ptr;
- tokens.push_back(aWord);
- }
- }
-
- return translator->GetMTError();
- }
-
-
- /**This function sets the lookup order of the subject domains. This determines
- the order in which the engine references the general dictionary and the subject
- domains. If the same headword appears in more than one dictionary, the engine
- selects the translation that appears in the dictionary with the highest priority.
- \param domains The list of prioritized subject domain identifiers. If you leave
- a subject domain out of the list, it will be disabled.
- \return Error code.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult SetDomainPriority(const MTDomainArray & domains)
- {
- unsigned nSize = domains.size();
- if (nSize == 0)
- return MTE_INVALID_PARAMETER;
-
- MTdomainarray domainArray = (MTdomain*) (&domains[0]);
- translator->SetDomainPriority(domainArray, nSize );
-
- return translator->GetMTError();
- }
-
- /**This function sets the value of the specified translation option.
- \param option A translation option identifier.
- \param optValue A value for the option.
- \return Error code.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_INVALID_OPTION_IDENTIFIER - <c>option</c> parameter is not one of the values reported by <c>GetAvailableOptions</c>
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult SetOptionValue(const MTOption option, MTOptionValue optValue)
- {
- translator->SetOptionValue(option, optValue);
- return translator->GetMTError();
- }
-
-
- /**This function sets the value of the specified global variable
- \param variable Global variable name
- \param value A value for the variable
- \return Error code.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_INVALID_PARAMETER - Internal engine error
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult SetVariableValue(const wstring& variable, const wstring& value)
- {
- translator->SetVariableValue((const MTstring)variable.c_str(), (const MTstring)value.c_str());
- return translator->GetMTError();
- }
-
- /**This function returns an array containing offset/length pairs for the sentences
- contained in the text block or the document being translated.
- \param text The source sentence(s).
- \param offsets A reference to an MTOffsetLengthArray vector where each element
- is a SOffsetLengthEntry struct with two integer fields: offset and len.
- Upon completion, offset contains the offset position (starting from position 0)
- of a parsed sentence; len is the length of a parsed sentence.
- \return Error code.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult SurfaceParseSentences(const wstring & text, MTOffsetLengthArray & offsets)
- {
- MTerror nError = MT_OK;
- unsigned nSize = LARGE_SIZE;
-
- vector<unsigned> offsetArray(nSize, 0);
-
- nError = translator->SurfaceParseSentences( (const MTstring)text.c_str(), &offsetArray[0], &nSize );
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- offsetArray = vector<unsigned> (nSize, 0);
- nError = translator->SurfaceParseSentences( (const MTstring)text.c_str(), &offsetArray[0], &nSize );
- }
-
- offsets.clear();
- for (unsigned i = 0; i < nSize; i += 2)
- {
- SOffsetLengthEntry entry = {offsetArray[i], offsetArray[i + 1]};
- offsets.push_back(entry);
- }
-
- return translator->GetMTError();
- }
-
- /**The most basic translation function of the SDK. It is intended for applications
- that only need to perform translation of a source text.
- \param source The text for translation. This string must be plain text
- (no formatting).
- \param translation Translated text.
- \return Error code.
- Possible errors:
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_EMPTY_SOURCE - <c>source</c> is empty.
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult Translate(const wstring & source, wstring & translation)
- {
- MTerror nError = MT_OK;
- unsigned nSize = LARGE_SIZE;
- vector<MTChar> transResult(nSize);
- transResult[0] = 0;
-
- nError = translator->Translate( (const MTstring)source.c_str(), &transResult[0], &nSize );
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- transResult = vector<MTChar> (nSize);
- transResult[0] = 0;
- nError = translator->Translate( (const MTstring)source.c_str(), &transResult[0], &nSize );
- }
-
- if ( nError == MT_OK)
- translation = &transResult[0];
-
- return translator->GetMTError();
- }
-
- /**This function is intended for applications that need to preserve formatting or
- other non-text information in output. You must provide an array of character runs,
- with the source text. Character runs usually correspond to sequences of consecutive
- text that all share the same attributes. For example, a sequence of three bold words
- in a sentence would be a single character run, and the rest of the sentence would be
- considered one or more additional character run(s). During translation, the engine
- produces a mapping between translated text and the source text. Because translation
- often changes the order and placement of words, the text runs will not appear in the
- same order. As a result, the BackMapArray in C++ is a mapping
- between the translated runs and the source runs.
- \param source The text for translation. This string must be plain text (no formatting).
- \param translation The translated text.
- \param backMapArray A reference to an \a MTBackMapArray vector where each element is
- an \a SBackMapArrayEntry struct with four integer fields: \a targetOffSet,
- \a targetLength, \a sourceOffSet and \a targetLength. Upon completion, \a targetOffset
- contains the 0-based index of the mapped translated word in the translated text,
- \a targetLength shows the length of the mapped translated word, \a sourceOffSet
- contains the 0-based index of the mapped source word in the source text, and
- \a sourceLength shows the length of the mapped source word.
- \return Error code.
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_EMPTY_SOURCE - <c>source</c> is empty.
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult Translate(const wstring & source, wstring & translation, MTBackMapArray &backMapArray)
- {
- MTerror nError = MT_OK;
- unsigned nSize = LARGE_SIZE;
- vector<MTChar> transResult(nSize);
- transResult[0] = 0;
-
- unsigned srcSize = FAIR_SIZE;
- unsigned trgSize = FAIR_SIZE;
- unsigned associateSize = FAIR_SIZE;
-
- //srcArray contains source offset and run
- vector<unsigned> srcArray(srcSize);
- vector<unsigned> trgArray(trgSize);
- vector<unsigned> associateArray (associateSize);
- associateArray[0] = 0;
-
- //associateArray has [trgIndex, srcIndex, trgIndex, srcIndex, ......]
- nError = translator->Translate( (const MTstring)source.c_str(),
- &transResult[0],
- &nSize,
- &srcArray[0],
- &srcSize,
- &trgArray[0],
- &trgSize,
- &associateArray[0],
- &associateSize);
-
-
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- transResult = vector<MTChar>(nSize);
- srcArray = vector<unsigned>(srcSize);
- trgArray = vector<unsigned>(trgSize);
- associateArray = vector<unsigned>(associateSize);
-
- nError = translator->Translate( (const MTstring)source.c_str(),
- &transResult[0],
- &nSize,
- &srcArray[0],
- &srcSize,
- &trgArray[0],
- &trgSize,
- &associateArray[0],
- &associateSize );
- }
-
- if ( nError == MT_OK)
- {
- translation = &transResult[0];
-
- //construct the back mapping array
- backMapArray.clear();
- for(unsigned i = 0; i < associateSize; i += 2)
- {
- SBackMapArrayEntry mapEntry;
- if(associateArray[i] >= 0 && associateArray[i] + 1 < trgSize)
- {
- mapEntry.targetOffset = trgArray[associateArray[i]];
- mapEntry.targetLength = trgArray[associateArray[i] + 1];
- }
-
- if(associateArray[i + 1] >= 0 && associateArray[i + 1] + 1 < srcSize)
- {
- mapEntry.sourceOffset = srcArray[associateArray[i + 1]];
- mapEntry.sourceLength = srcArray[associateArray[i + 1] + 1];
- }
- backMapArray.push_back(mapEntry);
- }
- }
- return translator->GetMTError();
- }
-
- /**This function translates an input sentence. It assumes the input text is a single
- sentence; therefore, the engine skips the sentence surface parsing.
- \param source The source sentence.
- \param translation The translated text.
- \return Error code.
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult TranslateSentence(const wstring & source, wstring & translation)
- {
- MTerror nError = MT_OK;
- unsigned nSize = LARGE_SIZE;
- vector <MTChar> transResult(nSize);
- transResult[0] = 0;
-
- nError = translator->TranslateSentence( (const MTstring)source.c_str(), &transResult[0], &nSize );
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- transResult = vector<MTChar>(nSize);
- transResult[0] = 0;
- nError = translator->TranslateSentence( (const MTstring)source.c_str(), &transResult[0], &nSize );
- }
-
- if ( nError == MT_OK)
- translation = &transResult[0];
-
- return translator->GetMTError();
- }
-
- /**This function translates a sentence and outputs both the translated text and a
- source-target word association mapping. This call assumes that the input text is a
- single sentence; therefore, the engine skips sentence surface parsing.
- \param source The source sentence.
- \param translation The translated sentence.
- \param backMapArray A reference to an \a MTBackMapArray vector where each element is
- an \a SBackMapArrayEntry struct with four integer fields: \a targetOffSet,
- \a targetLength, \a sourceOffSet and \a targetLength. Upon completion, \a targetOffset
- contains the 0-based index of the mapped translated word in the translated text,
- \a targetLength shows the length of the mapped translated word, \a sourceOffSet
- contains the 0-based index of the mapped source word in the source text, and
- \a sourceLength shows the length of the mapped source word.
- \return Error code.
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult TranslateSentence(const wstring & source, wstring & translation, MTBackMapArray &backMapArray)
- {
- MTerror nError = MT_OK;
- unsigned nSize = LARGE_SIZE;
- vector <MTChar> transResult(nSize);
- transResult[0] = 0;
-
- unsigned srcSize = FAIR_SIZE;
- unsigned trgSize = FAIR_SIZE;
- unsigned associateSize = FAIR_SIZE;
-
- //srcArray contains source offset and run
- vector<unsigned> srcArray(srcSize);
- vector<unsigned> trgArray(trgSize);
- vector<unsigned> associateArray (associateSize);
-
-
- //associateArray has [trgIndex, srcIndex, trgIndex, srcIndex, ......]
- //to comply with the Barc Engine Wrapper, have to use this way, although inefficient.
- bool bResizeBuffer = true;
-
- while (bResizeBuffer)
- {
- nError = translator->TranslateSentence( (const MTstring)source.c_str(),
- &transResult[0],
- &nSize,
- &srcArray[0],
- &srcSize,
- &trgArray[0],
- &trgSize,
- &associateArray[0],
- &associateSize);
-
-
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- transResult = vector<MTChar>(nSize);
- srcArray = vector<unsigned>(srcSize);
- trgArray = vector<unsigned>(trgSize);
- associateArray = vector<unsigned>(associateSize);
- continue;
- }
- bResizeBuffer = false;
- }
-
- if ( nError == MT_OK)
- {
- translation = &transResult[0];
-
- //construct the back mapping array
- backMapArray.clear();
- for(unsigned i = 0; i < associateSize; i += 2)
- {
- SBackMapArrayEntry mapEntry;
- if(associateArray[i] >= 0 && associateArray[i] + 1 < trgSize)
- {
- mapEntry.targetOffset = trgArray[associateArray[i]];
- mapEntry.targetLength = trgArray[associateArray[i] + 1];
- }
-
- if(associateArray[i + 1] >= 0 && associateArray[i + 1] + 1 < srcSize)
- {
- mapEntry.sourceOffset = srcArray[associateArray[i + 1]];
- mapEntry.sourceLength = srcArray[associateArray[i + 1] + 1];
- }
- backMapArray.push_back(mapEntry);
- }
- }
-
- return translator->GetMTError();
- }
-
-
- /**This function translates the specified source word from the active source
- language to the active target language. This call retrieves all possible
- translations (headwords) for the word found in the active subject dictionaries.
- \param source The source word for translation. This string requires the word field,
- and must be in word/attribute format.
- \param transArray The translations for the specified word. Each string in this array
- is in word/attribute format and contains a translation field.
- \return Error code.
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_EMPTY_SOURCE - <c>source</c> is empty.
- MTE_INVALID_WORD_ATTRIBUTE - word attribute is missing in <c>source</c> parameter
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult TranslateWord (const wstring & source, MTStringArray & transArray)
- {
- MTerror nError = MT_OK;
- unsigned nSize = LARGE_SIZE;
- vector<MTchar> translations(nSize);
- translations[0] = 0;
-
- nError = translator->TranslateWord( (const MTstring)source.c_str(), &translations[0], &nSize);
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- translations = vector<MTchar>(nSize);
- translations[0] = 0;
- nError = translator->TranslateWord( (const MTstring)source.c_str(), &translations[0], &nSize);
- }
-
- //fill the transArray
- transArray.clear();
- if (nError == MT_OK)
- {
- MTstringarray ptr;
- ptr = &translations[0];
- unsigned curPos = 0;
- wstring aWord;
- while( ptr - &translations[0] < (int)nSize )
- {
- aWord = wstring(ptr);
- ptr += aWord.size();
- ++ptr; //null-terminate current block
- ++ptr;
- transArray.push_back(aWord);
- }
- }
-
- return translator->GetMTError();
- }
-
- /**This function translates the specified source phrase from the active source
- language to the active target language. This call retrieves all possible
- translations (headwords) for the phrase found in the active subject dictionaries.
- \param source The source phrase for translation. This string requires the phrase field,
- and must be in word/attribute format.
- \param transArray The translations for the specified phrase. Each string in this array
- is in word/attribute format and contains a translation field.
- \return Error code.
- MTE_NO_TRANSLATOR - No language pair is set
- MTE_EMPTY_SOURCE - <c>source</c> is empty.
- MTE_INVALID_WORD_ATTRIBUTE - phrase attribute is missing in <c>source</c> parameter
- MTE_BARC_SERVER_ERROR - Internal engine error
- */
- MTResult TranslatePhrase (const wstring & source, MTStringArray & transArray)
- {
- if (source.size() == 0)
- return MTE_EMPTY_SOURCE;
-
- MTerror nError = MT_OK;
- unsigned nSize = LARGE_SIZE;
- vector<MTchar> translations(nSize);
- translations[0] = 0;
-
- nError = translator->TranslatePhrase( (const MTstring)source.c_str(), &translations[0], &nSize);
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- translations = vector<MTchar>(nSize);
- translations[0] = 0;
- nError = translator->TranslatePhrase( (const MTstring)source.c_str(), &translations[0], &nSize);
- }
-
- //fill the transArray
- transArray.clear();
- if (nError == MT_OK)
- {
- MTstringarray ptr;
- ptr = &translations[0];
- unsigned curPos = 0;
- wstring aWord;
- while( ptr - &translations[0] < (int)nSize )
- {
- aWord = wstring(ptr);
- ptr += aWord.size();
- ++ptr; //null-terminate current block
- ++ptr;
- transArray.push_back(aWord);
- }
- }
-
- return translator->GetMTError();
- }
-
- //////////////////////////////////////////////////////////////////////
- // Following is a large group of confidence score functions
- //
-
- /**Returns the names of the existing confidence models. This function loads
- all available DLLs with different confidence score models and queries them
- for CM names. The each DLL which contains a confidence score model will have
- an API which will provide a name of the model this DLL contains.
- \param models [OUT] This is an output parameter. Upon return from the function,
- models will contain the names of the confidence models that are currently
- available in the engine. Each name is a Unicode string. The function gets
- this string from the DLL containing individual CM. If an error occurs during
- the execution of this function, the returned vector will be empty and function
- will return MTE_CM_FAILURE. If there are no DLLs containing confidence score
- models, the vector will be empty and function will return MT_OK.
- \return Error code.
- MT_OK: The function was executed successfully.
- MTE_BARC_SERVER_ERROR: Unexpected error.
- */
- MTResult GetConfidenceModels(MTStringArray &models)
- {
- MTerror nError = MT_OK;
-
- unsigned nSize = LARGE_SIZE;
- vector<MTchar> tmpArray(nSize);
-
- nError = translator->GetConfidenceModels(&tmpArray[0], &nSize);
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- tmpArray = vector<MTchar>(nSize);
- tmpArray[0] = 0;
- nError = translator->GetConfidenceModels(&tmpArray[0], &nSize);
- }
- // transfer the data to output parameter
- models.clear();
- if (nError == MT_OK)
- {
- MTstringarray ptr;
- ptr = &tmpArray[0];
- unsigned curPos = 0;
- wstring aWord;
- while( ptr - &tmpArray[0] < (int)nSize )
- {
- aWord = wstring(ptr);
- ptr += aWord.size();
- ++ptr; //null-terminate current block
- ++ptr;
- models.push_back(aWord);
- }
- }
- return translator->GetMTError();
- }
-
- /**Select a confidence model for future operations.
- Selecting a confidence model is the first step needed in obtaining confidence
- scores. All other SDK functions related to confidence measures except
- MTtranslator3::GetConfidenceMethods operate on the currently selected model.
- During the runtime of an SDK instance, several confidence methods can be
- selected (and activated and executed). Selecting a confidence measure does
- not invalidate or remove the previously selected confidence score model.
- Only one instance of a particular confidence model exists at any given time.
- Thus, it is not possible to use two identical confidence score models with
- differing configuration in the same SDK process.
- \param model A Unicode string identifying the confidence measure to be used.
- This should be identical to one of the names returned in GetConfidenceModels().
- \return Error code.
- MT_OK - The function was executed successfully.
- MTE_INVALID_PARAMETER - The name of the confidence method is invalid. No such
- method exists.
- MTE_BARC_SERVER_ERROR - Unexpected error.
- */
- MTResult SelectConfidenceModel(const wstring &model)
- {
- MTerror nError = MT_OK;
- nError = translator->SelectConfidenceModel((const MTstring)model.c_str());
- return translator->GetMTError();
- }
-
- /**Activate the current confidence model.
- This function activates the previously selected (and possibly configured)
- confidence score model. Activation is a prerequisite to executing a confidence
- measure. The reason for explicitly separating the activation instead of just
- performing initialization upon first use is to allow the implementor to place
- time-consuming initialization code here.
- This function can only be called of a confidence score model was selected earlier.
- This function needs to be called before executing a confidence measure for
- the first time.
- \return Error code
- MT_OK - The function was executed successfully.
- MTE_BARC_SERVER_ERROR - Unexpected error.
- MTE_NO_CM_SELECTED - Confidence measure is not selected.
- */
- MTResult ActivateConfidenceMeasure()
- {
- MTerror nError = MT_OK;
- nError = translator->ActivateConfidenceMeasure();
- return translator->GetMTError();
- }
-
- /**Deactivate the current confidence score model.
- \return Error code
- MT_OK - The function was executed successfully.
- MTE_BARC_SERVER_ERROR - Unexpected error.
- */
- MTResult DeactivateConfidenceMeasure()
- {
- MTerror nError = MT_OK;
- nError = translator->DeactivateConfidenceMeasure();
- return translator->GetMTError();
- }
-
- /**Return the available options for the current confidence measure. Different
- methods may have different list of options. These options will be used to
- configure selected confidence method.
- \param options [OUT]. Upon return from the function options will contain
- a list of all available options for currently selected confidence method.
- \return Error code
- MT_OK - The function was executed successfully.
- MTE_BARC_SERVER_ERROR - Unexpected error.
- MTE_NO_CM_SELECTED - Confidence measure is not selected.
- */
- MTResult GetConfidenceOptions(MTStringArray &options)
- {
- MTerror nError = MT_OK;
- unsigned nSize = LARGE_SIZE;
- vector<MTchar> tmpArray(nSize);
- nError = translator->GetConfidenceOptions(&tmpArray[0], &nSize);
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- tmpArray = vector<MTchar>(nSize);
- tmpArray[0] = 0;
- nError = translator->GetConfidenceOptions(&tmpArray[0], &nSize);
- }
- // transfer the data to output parameter
- options.clear();
- if (nError == MT_OK)
- {
- MTstringarray ptr;
- ptr = &tmpArray[0];
- unsigned curPos = 0;
- wstring aWord;
- while( ptr - &tmpArray[0] < (int)nSize )
- {
- aWord = wstring(ptr);
- ptr += aWord.size();
- ++ptr; //null-terminate current block
- ++ptr;
- options.push_back(aWord);
- }
- }
- return translator->GetMTError();
- }
-
- /**Return the available values for a confidence measure option. Every option
- is represented as a string. It is a responsibility of particular confidence
- score model to convert the string value of the option to an appropriate type.
- \param option [IN]. Name of the option. This must be one of the options
- returned in GetConfidenceOptions call.
- \param values [OUT]. All possible values which can be assigned to the option.
- If values cannot be enumerated, this vector contains 0 elements. In this case
- any string value can be assigned to the option.
- \return Error code
- MT_OK - The function was executed successfully.
- MTE_BARC_SERVER_ERROR - Unexpected error.
- MTE_NO_CM_SELECTED - Confidence measure is not selected.
- MTE_INVALID_PARAMETER - The name of the option is invalid. No such option exists.
- */
- MTResult GetConfidenceOptionValues(const wstring &option, MTStringArray &values)
- {
- MTerror nError = MT_OK;
- unsigned nSize = LARGE_SIZE;
- vector<MTchar> tmpArray(nSize);
- nError = translator->GetConfidenceOptionValues(
- (const MTstring)option.c_str(), &tmpArray[0], &nSize);
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- tmpArray = vector<MTchar>(nSize);
- tmpArray[0] = 0;
- nError = translator->GetConfidenceOptionValues(
- (const MTstring)option.c_str(), &tmpArray[0], &nSize);
- }
- // transfer the data to output parameter
- values.clear();
- if (nError == MT_OK)
- {
- MTstringarray ptr;
- ptr = &tmpArray[0];
- unsigned curPos = 0;
- wstring aWord;
- while( ptr - &tmpArray[0] < (int)nSize )
- {
- aWord = wstring(ptr);
- ptr += aWord.size();
- ++ptr; //null-terminate current block
- ++ptr;
- values.push_back(aWord);
- }
- }
- return translator->GetMTError();
- }
-
- /**Get the type of the option. Options are defined by individual confidence
- score model. They may contain file names, numerical or string values etc.
- In order to assign some value to the option the client application should know
- what kind of values each individual option can accept.
- \param option [IN]. Name of the option. This must be one of the options returned
- in GetConfidenceOptions call.
- \param type [OUT]. One of predefined types. All available types are listed
- in enumeration:
- enum
- {StringOption = 0,
- NumericalOption = 1,
- BooleanOptin = 2,
- FileNameOption = 3};
- Regardless of the optionÆs type the option may accept only string values.
- It is responsibility of the selected model to convert the option into
- appropriate type and validate it.
- \return Error code
- MT_OK - The function was executed successfully.
- MTE_BARC_SERVER_ERROR - Unexpected error.
- MTE_NO_CM_SELECTED - Confidence measure is not selected.
- MTE_INVALID_PARAMETER - The name of the option is invalid. No such option exists.
- */
- MTResult GetConfidenceOptionType(const wstring &option, int &type)
- {
- MTerror nError = MT_OK;
- nError = translator->GetConfidenceOptionType(
- (const MTstring)option.c_str(), type);
- return translator->GetMTError();
- }
-
- /**Get the current value of a confidence measure option.
- \param option [IN]. Name of the option. This must be one of the options
- returned in GetConfidenceOptions call.
- \param value [OUT]. The current value of the option converted to
- the string. Even if the option is number or Boolean the value can be
- passed between caller and confidence score API only as a string.
- \return Error code
- MT_OK - The function was executed successfully.
- MTE_BARC_SERVER_ERROR - Unexpected error.
- MTE_NO_CM_SELECTED - Confidence measure is not selected.
- MTE_INVALID_PARAMETER - The name of the option is invalid. No such option exists.
- */
- MTResult GetConfidenceOptionValue(const wstring &option, wstring &value)
- {
- MTerror nError = MT_OK;
- unsigned nSize = LARGE_SIZE;
- vector<MTchar> tmpArray(LARGE_SIZE);
- nError = translator->GetConfidenceOptionValue(
- (const MTstring)option.c_str(), &tmpArray[0], &nSize);
- if (nError == MTE_INSUFFICIENT_BUFFER ) // insufficient buffer
- {
- tmpArray = vector<MTchar>(nSize);
- tmpArray[0] = 0;
- nError = translator->GetConfidenceOptionValue(
- (const MTstring)option.c_str(), &tmpArray[0], &nSize);
- }
- if (nError == MT_OK)
- {
- value = &tmpArray[0];
- }
- return translator->GetMTError();
- }
-
- /**Set the value of a confidence score model option. The option will be assigned
- with string value. The currently selected confidence method will convert
- the value to an appropriate type (see GetConfidenceOptionType), validate
- the value and check if the value can be used. E.g. if option is a file name
- it will check if the file is present; if option is number it will check if
- the entered value falls within acceptable number range, etc.
- \param option [IN]. The name of the option. This should be identical
- to one of the options returned in GetConfidenceOptions().
- \param value [IN]. The value of the option. The string value of the option.
- This parameter is always string even if the option can accept only numerical
- values. The currently selected confidence score will cast the value
- to appropriate type and perform the validation.
- \return Error code
- MT_OK - The function was executed successfully.
- MTE_BARC_SERVER_ERROR - Unexpected error.
- MTE_NO_CM_SELECTED - Confidence measure is not selected.
- MTE_INVALID_PARAMETER - The name of the option is invalid. No such option exists.
- MTE_INVALID_VALUE: The value cannot be used.
- */
- MTResult SetConfidenceOptionValue(const wstring &option, const wstring &value)
- {
- MTerror nError = MT_OK;
- nError = translator->SetConfidenceOptionValue(
- (const MTstring)option.c_str(), (const MTstring)value.c_str());
- return translator->GetMTError();
- }
-
- /**Return the possible range of scores for the current confidence measure.
- This function can be called after selected confidence score method is activated.
- The range can be different for different confidence score methods.
- There is no way to estimate the possible values before the method
- is selected, configured and activated. The meaning of return parameters may be
- also different for different methods. E.g. some method may return minValue = 0
- and maxValue = 1 meaning that result can be anywhere between 0 and 1,
- like 0.5 or 0.8 etc; another method may return same values meaning they are
- FALSE and TRUE and result can be only 0 or 1. The meaning of min and max
- value will be provided in the documentation for every available confidence
- score method.
- \param minValue [OUT]. Contains smallest value which can be returned
- by function GetConfidenceScore().
- \param maxValue [OUT]. Contains biggest value which can be returned
- by function GetConfidenceScore()
- \return Error code
- MT_OK - The function was executed successfully.
- MTE_BARC_SERVER_ERROR - Unexpected error.
- MTE_NO_CM_SELECTED - Confidence measure is not selected.
- */
- MTResult GetConfidenceRange(double &minValue, double &maxValue)
- {
- MTerror nError = MT_OK;
- nError = translator->GetConfidenceRange(minValue, maxValue);
- return translator->GetMTError();
- }
- /**
- Produce the confidence score for a sentence pair basing on currently
- selected model. Before calling this function the confidence score model
- has to be selected, configured and activated. This function may use the engine
- data therefore it guarantees to return correct data only if it is called
- immediately after translation and before any other engine calls. Details
- of implementation depend on particular confidence score method and
- should be described in method documentation.
- \param source [IN]. Source sentence
- \param translation [IN]. Translated sentence
- \param score [OUT]. Contains the resulting confidence score
- \return Error code
- MT_OK - The function was executed successfully.
- MTE_BARC_SERVER_ERROR - Unexpected error.
- MTE_NO_CM_SELECTED - Confidence measure is not selected.
- */
- MTResult GetConfidenceScore(const wstring &source, const wstring &translation, double &score)
- {
- MTerror nError = MT_OK;
- nError = translator->GetConfidenceScore(
- (const MTstring)source.c_str(), (const MTstring)translation.c_str(), score);
- return translator->GetMTError();
- }
- //
- // End of confidence score methods
- ///////////////////////////////////////////////////////////////////
- };
-
-
-
-
-
- #ifdef MTAPI_EXPORT
- #undef MTAPI_EXPORT
- #define MTAPI_EXPORT __declspec(dllexport)
- #else
- #define MTAPI_EXPORT __declspec(dllexport)
- #endif
-
-
-
- #ifndef MTAPI_LANG_ENGLISH
- #define MTAPI_LANG_ENGLISH 0x00000009 // English
- #endif
-
- #ifndef MTAPI_LANG_FRENCH
- #define MTAPI_LANG_FRENCH 0x0000000C // French
- #endif
-
- #ifndef MTAPI_LANG_GERMAN
- #define MTAPI_LANG_GERMAN 0x00000007 // German
- #endif
-
- #ifndef MTAPI_LANG_ITALIAN
- #define MTAPI_LANG_ITALIAN 0x00000010 // Italian
- #endif
-
- #ifndef MTAPI_LANG_PORTUGUESE
- #define MTAPI_LANG_PORTUGUESE 0x00000016 // Portuguese
- #endif
-
- #ifndef MTAPI_LANG_SPANISH
- #define MTAPI_LANG_SPANISH 0x0000000a // Spanish
- #endif
-
- #ifndef MTAPI_LANG_JAPANESE
- #define MTAPI_LANG_JAPANESE 0x00000011 // Japanese
- #endif
-
- #ifndef MTAPI_LANG_NEUTRAL
- #define MTAPI_LANG_NEUTRAL 0x00000000 // Neutral
- #endif
-
-
- // following are defines for every API call.
- const short ISCOUNTAVAILABLEINTERFACELANGUAGES = 1;
- const short ISCOUNTAVAILABLELANGUAGEPAIRS = 2;
- const short ISCOUNTAVAILABLEOPTIONS = 3;
- const short ISCOUNTAVAILABLEOPTIONVALUES = 4;
- const short ISCOUNTAVAILABLEPARTSOFSPEECH = 5;
- const short ISCOUNTAVAILABLESUBJECTDOMAINS = 6;
- const short ISCREATEDOMAIN = 7;
- const short ISCREATETRANSLATION = 8;
- const short ISDELETETRANSLATION = 9;
- const short ISGETACTIVELANGUAGEPAIR = 10;
- const short ISGETAVAILABLEINTERFACELANGUAGES = 11;
- const short ISGETAVAILABLELANGUAGEPAIRS = 12;
- const short ISGETAVAILABLEMORPHOLOGIES = 13;
- const short ISGETAVAILABLEOPTIONS = 14;
- const short ISGETAVAILABLEOPTIONVALUES = 15;
- const short ISGETAVAILABLEPARTSOFSPEECH = 16;
- const short ISGETAVAILABLESUBJECTDOMAINS = 17;
- const short ISGETDOMAINPRIORITY = 19;
- const short ISGETHEADWORDTRANSLATIONS = 21;
- const short ISGETLANGUAGEDESCRIPTION = 22;
- const short ISGETNEXTHEADWORD = 23;
- const short ISGETOPTIONDESCRIPTION = 24;
- const short ISGETOPTIONVALUE = 25;
- const short ISGETOPTIONVALUEDESCRIPTION = 26;
- const short ISGETPARTOFSPEECHDESCRIPTION = 27;
- const short ISGETPREVIOUSHEADWORD = 28;
- const short ISGETSUBJECTDOMAINDESCRIPTION = 29;
- const short ISINFLECTWORD = 31;
- const short ISLOADALLLANGUAGEPAIRS = 32;
- const short ISLOADCUSTOMDICTIONARY = 33;
- const short ISLOADDOMAINDICTIONARY = 34;
- const short ISLOADLANGUAGEPAIR = 35;
- const short ISLOOKUP = 36;
- const short ISPARSEWORDSFROMSENTENCE = 37;
- const short ISRELEASEMT = 39;
- const short ISSETACTIVELANGUAGEPAIR = 40;
- const short ISSETDOMAINPRIORITY = 41;
- const short ISSETHEADWORDTRANSLATIONS = 42;
- const short ISSETOPTIONVALUE = 43;
- const short ISSURFACEPARSESENTENCES = 44;
- const short ISTRANSLATE = 49;
- const short ISTRANSLATEBACKMAP = 50;
- const short ISTRANSLATESENTENCE = 52;
- const short ISTRANSLATESENTENCEBACKMAP = 53;
- const short ISTRANSLATEWORD = 54;
- const short ISUNLOADALLLANGUAGEPAIRS = 55;
- const short ISUNLOADCUSTOMDICTIONARY = 56;
- const short ISUNLOADDOMAINDICTIONARY = 57;
- const short ISUNLOADLANGUAGEPAIR = 58;
-
- const short ISGETHEADWORD = 62;
- const short ISGETHEADWORDCOUNT = 63;
- const short ISGETHEADWORDINDEX = 64;
- const short ISCONVERTLIFTODICTIONARY = 65;
-
- const short ISCOUNTAVAILABLEVARIABLES = 66;
- const short ISGETAVAILABLEVARIABLES = 67;
- const short ISGETVARIABLEVALUE = 68;
- const short ISSETVARIABLEVALUE = 69;
- const short ISTRANSLATEPHRASE = 70;
- const short ISENGINETOKENIZE = 71;
- // <confidence score functions>
- const short ISGETCONFIDENCEMODELS = 72;
- const short ISSELECTCONFIDENCEMODEL = 73;
- const short ISACTIVATECONFIDENCEMEASURE = 74;
- const short ISDEACTIVATECONFIDENCEMEASURE = 75;
- const short ISGETCONFIDENCEOPTIONS = 76;
- const short ISGETCONFIDENCEPOTIONVALUES = 77;
- const short ISGETCONFIDENCEOPIONTYPE = 78;
- const short ISGETCONFIDENCEOPTIONVALUE = 79;
- const short ISSETCONFIDENCEOPTIONVALUE = 80;
- const short ISGETCONFIDENCERANGE = 81;
- const short ISGETCONFIDENCESCORE = 82;
- // </confidence score functions>
-
- //extern "C" MTAPI_EXPORT MTtranslator3 * CreateTranslator();
- //extern "C" MTAPI_EXPORT void DeleteTranslator(MTtranslator3 *);
-
- //typedef MTtranslator3 * (MTAPI_CALL *fnCreateTranslator)();
- //typedef void (MTAPI_CALL *fnDeleteTranslator)(MTtranslator3 *);
-
-
- } // end namespace MTCPlusPlusAPI
-
-
- #endif // MTCPlusPlusAPI_H
-