⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 segtag.pas

📁 解码器是基于短语的统计机器翻译系统的核心模块
💻 PAS
字号:
(*
* SEGTAG.PAS  -  Interfaces: SegTag from mandel** Copyright (C) 2006 by Yidong Chen <ydchen@xmu.edu.cn>Institute of Artificial Intelligence, Xiamen University* Begin       : 09/18/2006* Last Change : 09/18/2006** This program is free software; you can redistribute it and/or* modify it under the terms of the GNU Lesser General Public* License as published by the Free Software Foundation; either* version 2.1 of the License, or (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the* GNU General Public License for more details.** You should have received a copy of the GNU Lesser General Public* License along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.*)
UNIT SEGTAG;

INTERFACE

PROCEDURE Init(strSegTagPath: STRING; bToSegTraditional: Boolean=False);
FUNCTION SegSent(strInput: STRING): STRING;
PROCEDURE CleanUp;

IMPLEMENTATION

USES
  Windows, SysUtils, Classes;

CONST
  DLL_SEGTAG='segtag.dll';

  PROC_SetTraditional='set_traditional';
  FUNC_InitSeg='init_seg';
  PROC_SetTagMode='set_tagmode';
  FUNC_SegSent='seg_sent';
  PROC_SetSegTagDir='set_segtag_dir';

TYPE
  //From segtag.dll
  TTagmode = (tag_normal, tag_no, tag_neon, tag_only);
  TProcSetTraditional=PROCEDURE; STDCALL;
  TFuncInitSeg=FUNCTION(pcErrMsg: PChar): Boolean; STDCALL;
  TProcSetTagMode=PROCEDURE(tmNew: TTagMode); STDCALL;
  TFuncSegSent=FUNCTION(pwInput: PWideChar; iLen: Integer; pOutBuf: PChar; iBufLen: Integer): Boolean; STDCALL;
  TProcSetSegTagDir=PROCEDURE(pDir: PChar); STDCALL;

  TIWrap=CLASS
  PRIVATE
    FbisInitOK: Boolean;
    bisSegTagInitOK: Boolean;

    hSegTag: Cardinal;

    procSetTraditional:TProcSetTraditional;
    funcInitSeg:TFuncInitSeg;
    procSetTagMode:TProcSetTagMode;
    funcSegSent:TFuncSegSent;
    procSetSegTagDir: TProcSetSegTagDir;
  PUBLIC
    PROPERTY isInitOK: Boolean READ FbisInitOK;

    CONSTRUCTOR Create(strSegTagPath: STRING; bToSegTraditional: Boolean=False);
    FUNCTION SegSent(strInput: STRING): STRING;
    DESTRUCTOR Destroy; OVERRIDE;
  END;

VAR iwSegTag: TIWrap;

{$IFDEF CONSOLE}
PROCEDURE PrintErrMsg(strErrMsg: STRING);
BEGIN
  WriteLn(strErrMsg);
END;
{$ELSE}
PROCEDURE PrintErrMsg(strErrMsg: STRING);
BEGIN
  MessageBox(0,PChar(strErrMsg),'出错',MB_OK);
END;
{$ENDIF}

PROCEDURE DLLMissing(strDLLPathName: STRING; VAR bisInitOK: Boolean);
VAR strTemp: STRING;
BEGIN
  strTemp:='动态链接库'+strDLLPathName+'丢失,请与提供商联系!';
  IF bisInitOK THEN PrintErrMsg(strTemp);
  bisInitOK:=False;
END;

PROCEDURE FuncNotExported(strDLLPathName, strFuncName: STRING; VAR bisInitOK: Boolean);
VAR strTemp: STRING;
BEGIN
  strTemp:='动态链接库'+strDLLPathName+'中的'+strFuncName+'未导出,请与提供商联系!';
  IF bisInitOK THEN PrintErrMsg(strTemp);
  bisInitOK:=False;
END;

PROCEDURE InitFailed(strModule: STRING; VAR bisInitOK: Boolean);
VAR strTemp: STRING;
BEGIN
  strTemp:=strModule+'模块初始化失败,请与提供商联系!';
  IF bisInitOK THEN PrintErrMsg(strTemp);
  bisInitOK:=False;
END;

CONSTRUCTOR TIWrap.Create(strSegTagPath: STRING; bToSegTraditional: Boolean=False);
VAR strTemp: STRING; strErrMsg: STRING;
BEGIN
  INHERITED Create;

  FbisInitOK:=True;

(* Initialize all the data member to zero. -- start *)
  hSegTag:=0;

  bisSegTagInitOK:=False;

  procSetTraditional:=NIL;
  funcInitSeg:=NIL;
  procSetTagMode:=NIL;
  funcSegSent:=NIL;
  procSetSegTagDir:=NIL;
  funcSegSent:=NIL;
  procSetSegTagDir:=NIL;
(* Initialize all the data member to zero. -- end *)

  strTemp:=strSegTagPath+DLL_SEGTAG;
  hSegTag:=LoadLibrary(PChar(strTemp));

  //...load functions.

  IF hSegTag<>0 THEN
    BEGIN
      procSetTraditional:=TProcSetTraditional(GetProcAddress(hSegTag,PChar(PROC_SetTraditional)));
      IF @procSetTraditional=NIL THEN FuncNotExported(strSegTagPath+DLL_SEGTAG,PROC_SetTraditional, FbisInitOK);
      funcInitSeg:=TFuncInitSeg(GetProcAddress(hSegTag,PChar(FUNC_InitSeg)));
      IF @funcInitSeg=NIL THEN FuncNotExported(strSegTagPath+DLL_SEGTAG,FUNC_InitSeg, FbisInitOK);
      procSetTagMode:=TProcSetTagMode(GetProcAddress(hSegTag,PChar(PROC_SetTagMode)));
      IF @procSetTagMode=NIL THEN FuncNotExported(strSegTagPath+DLL_SEGTAG,PROC_SetTagMode, FbisInitOK);
      funcSegSent:=TFuncSegSent(GetProcAddress(hSegTag,PChar(FUNC_SegSent)));
      IF @funcSegSent=NIL THEN FuncNotExported(strSegTagPath+DLL_SEGTAG,FUNC_SegSent, FbisInitOK);
      procSetSegTagDir:=TProcSetSegTagDir(GetProcAddress(hSegTag,PChar(PROC_SetSegTagDir)));
      IF @procSetSegTagDir=NIL THEN FuncNotExported(strSegTagPath+DLL_SEGTAG,PROC_SetSegTagDir, FbisInitOK);
    END
  ELSE DLLMissing(strSegTagPath+DLL_SEGTAG,FbisInitOK);

  //...init every modules.

  IF (@funcInitSeg<>NIL) THEN
    BEGIN
      IF bToSegTraditional THEN procSetTraditional;
      procSetSegTagDir(PChar(strSegTagPath));
      SetLength(strErrMsg, 257);
      bisSegTagInitOK:=(funcInitSeg(@strErrMsg[1]));
      IF NOT bisSegTagInitOK THEN InitFailed('分词模块',FbisInitOK);
      procSetTagMode(tag_no);
    END;
END;

CONST
  INITLEN=127;
  MAXLEN=131071;
  MAGICLEN=127;

FUNCTION TIWrap.SegSent(strInput: STRING): STRING;
VAR
  pwSource: WideString;
  pcTarget: PChar;
  iLen: Integer;
  bTemp: Boolean;
BEGIN
  IF NOT bisSegTagInitOK THEN BEGIN Result:=''; Exit; END;

  pwSource := strInput;
  iLen:=INITLEN;
  //if seg_sent(PWideChar(pwSource), length(p), buf, sizeof(buf)) then
  bTemp:=False;
  pcTarget:=NIL;
  WHILE iLen<MAXLEN DO
    BEGIN
      ReallocMem(pcTarget,iLen);
      FillChar(pcTarget^,iLen,#0);
      IF NOT funcSegSent(PWideChar(pwSource),Length(pwSource),pcTarget,iLen) THEN iLen:=iLen+MAGICLEN
      ELSE BEGIN bTemp:=True; Break; END;
    END;
  IF NOT bTemp THEN
    BEGIN
      FreeMem(pcTarget);
      Result:='';
    END
  ELSE
    BEGIN
      SetLength(Result,StrLen(pcTarget));
      Move(pcTarget^,Result[1],StrLen(pcTarget));
      FreeMem(pcTarget);
    END;
END;

DESTRUCTOR TIWrap.Destroy;
BEGIN
  IF hSegTag<>0 THEN FreeLibrary(hSegTag);
  INHERITED Destroy;
END;

PROCEDURE Init(strSegTagPath: STRING; bToSegTraditional: Boolean=False);
BEGIN
	iwSegTag:=TIWrap.Create(strSegTagPath, bToSegTraditional);
END;

FUNCTION SegSent(strInput: STRING): STRING;
BEGIN
	Result:=iwSegTag.SegSent(strInput);
END;

PROCEDURE CleanUp;
BEGIN
	iwSegTag.Free;
END;

END.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -