Home modules.gotpike.org
Username: Password: [Create Account]
[Forgot Password?]

Modules

ADT
Database
GTK2
GUI
IP
PiJAX
Public
Sql
Stdio
Subversion
System
Tools
Xosd
lua
v4l2
wx

Recent Changes

Public.Parser.XML2 1.50
Public.ZeroMQ 1.1
Public.Template.Mustache 1.0
Public.Protocols.XMPP 1.4
Sql.Provider.jdbc 1.0

Popular Downloads

Public.Parser.JSON2 1.0
Public.Parser.JSON 0.2
GTK2 2.23
Public.Web.FCGI 1.8
Public.Parser.XML2 1.48


Module Information
Public.Tools.Language.Hyphenate
Viewing contents of Public_Tools_Language_Hyphenate-1.0/hyphenate.cmod

/*! @module Public
 */

/*! @module Tools
 */

/*! @module Language
 */

#define _GNU_SOURCE

#include "hyphenate_config.h"
#include "util.h"
#include "altlinux/hyphen.h"
#include "altlinux/csutil.h"


/*! @class Hyphenate
 */

PIKECLASS Hyphenate
{

typedef struct
{
    HyphenDict * dict;
} HYPHENATE_OBJECT_DATA;

CVAR HYPHENATE_OBJECT_DATA   *object_data;


/*! @decl void create(string appname)
 *!   Creates a new Public.Sample.Sample object
 *!
 */
PIKEFUN void create(string dictionary)
{
  HyphenDict * hd;
  char * dn;
  dn = strdup(dictionary->str);
  hd = hnj_hyphen_load(dn);

  if(!hd)
  {
    Pike_error("unable to load hyphenation dictionary.\n");
  }

  THIS->object_data->dict = hd;

  pop_n_elems(args);
}

PIKEFUN string hyphenate(string word)
{
int k, n, i, j, c;

int  nHyphCount;
char *hyphens;
char *lcword;
char *hyphword;

k = word->len;
//  *(word + k - 1) = '\0';

  /* set aside some buffers to hold lower cased */
  /* and hyphen information */
  lcword = (char *) malloc(k+1);
  hyphens = (char *)malloc(k+5);
  enmkallsmall(lcword,word->str,THIS->object_data->dict->cset);
//printf("hyphenating\n");
  /* first remove any trailing periods */
  n = k-1;
  while((n >=0) && (lcword[n] == '.')) n--;
  n++;

  /* now actually try to hyphenate the word */
  if (n > 0) {
    if (hnj_hyphen_hyphenate(THIS->object_data->dict, lcword, n-1, hyphens))
    {
        free(hyphens);
        free(lcword);
		pop_n_elems(args);
        Pike_error("hyphenation error\n");
       }


       /* now backfill hyphens[] for any removed periods */
       for (c = n; c < k; c++) hyphens[c] = '0';
       hyphens[k] = '\0';

       /* now create a new char string showing hyphenation positions */
       /* count the hyphens and allocate space for the new hypehanted string */
       nHyphCount = 0;
       for (i = 0; i < n; i++)
          if (hyphens[i]&1)
             nHyphCount++;
       hyphword = (char *) malloc(k+1+nHyphCount);
       j = 0;
      for (i = 0; i < n; i++) {
          hyphword[j++] = word->str[i];
          if (hyphens[i]&1)
          {
              hyphword[j++] = '-';
          }
}
	       hyphword[j] = '\0';
	
		   pop_n_elems(args);
	       push_text(hyphword);
//printf("done\n");
	       free(hyphens);
	       free(lcword);
	       free(hyphword);
//	    }
		}
}


INIT
{
    HYPHENATE_OBJECT_DATA * dta = 
	(HYPHENATE_OBJECT_DATA*)malloc(sizeof(HYPHENATE_OBJECT_DATA));
    if (!dta)
        Pike_error("init_sample: Out of memory!\n");

    THIS->object_data = dta;
}

EXIT 
{
  if(THIS->object_data)
  {
    free(THIS->object_data);
  }
}

}

/*! @endclass
 */

/*! @endmodule
 */

/*! @endmodule
 */

/*! @endmodule
 */




gotpike.org | Copyright © 2004 - 2019 | Pike is a trademark of Department of Computer and Information Science, Linköping University