C_N-gram - アールメカブ

アールメカブ


C_N-gram

Programming

とりあえず,こんなプログラムを書いてみた

#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <string.h>
#include <limits.h>
#include <locale>
#include <map>
#include <iostream>
using namespace std;
int main (int argc, char *argv[]){
  char  input [1024];
 /* ワイド文字列 : 日本語文字数  + 1 */
 wchar_t  wbuf[1240];
 /* マルチバイトUTF-8 で漢字は3バイト,一文字取るなら 3 + 1 */
 char  str1  [MB_LEN_MAX + 1]; // [4] = { 0 };
 char str2   [MB_LEN_MAX + 1]; // [4] = { 0 };
 char gram2 [7]  = { 0 };
 int i, test = 0;
 FILE *fp;
 char * p;

 map<string, int> m1;
 map<string, int>::iterator pa; 
 setlocale(LC_ALL,"");
 printf("file = %s\n", argv[1]);
 if((fp = fopen(argv[1], "r")) == NULL){
   printf("file not found\n");
	return(1);
 }else{
	while(!feof(fp)){
	  if(fgets(input, 1024, fp) != NULL){
		p = strchr( input, '\n' );
		/* 改行文字があった場合 */
		if ( p != NULL )
		  {
			/* 改行文字を終端文字に置き換える */
			*p = '\0';
		  }

		printf("size of input= %lu\n",  sizeof(input));
		printf("strlen of input= %lu\n",  strlen(input));  
		if(strlen(input) > 0){
		  printf("%s\n", input);
		  /* マルチバイト文字列をワイド文字列に変換*/
		  mbstowcs(wbuf, input,  strlen(input));
		  printf("strlen of wbuf = %lu\n", wcslen(wbuf));  
		  while(test < wcslen(wbuf)  ){
			/* ワイド文字列をマルチバイト文字列に変換*/
			i = wctomb(str1,wbuf[test]);
			str1 [i] = '\0';
			cout << "str1 = " <<  i << " = " << str1 << endl;
			i =	wctomb(str2,wbuf[test+1]);
			str2 [i] = '	\0';
			cout << "str2 = " <<  i << " = " << str2 << endl;
			sprintf(gram2, "%s%s",str1, str2);
			/* Windows minGWでは wsprintf とする必要がある */
			
			pa = m1.find(gram2);
			//出てきた文字ペアは既にマップにあるか?
			if(pa != m1.end()){
			  pa->second =  pa->second + 1;
			  //二つ目の数値を加算
			}
			else{// マップにないなら,新規にマップに追加
			  m1.insert(make_pair(gram2, 1));
			  // 1 は 1個目と言う意味
			}
			test++;
		  }
	    }// while_2
	  }// _if 
	} //_while_1
	pa = m1.begin();
	while(pa != m1.end()){ 
	  printf("%s = %d\n", (pa->first).c_str(), (pa->second) ); 
	  pa++;
	}
 }//_else
}
 
Link: Programming(4985d) Programming_C(5436d)
Last-modified: 2012-01-06 (金) 11:13:02 (4494d)