
/*=============================================================================================
	author:ZhaoJun
	version:1.2
	email:2732810281@qq.com
=============================================================================================*/


//严格模式
/*-------------------------------------------------------------------------------------------*/	
'use strict';
/*-------------------------------------------------------------------------------------------*/
    
// 回复初始操作
/*-------------------------------------------------------------------------------------------*/	
	class cert
	{
		static init(url=$ExtendOther.word.part_url)
		{
			if(typeof url == 'string')
			{
				cert.part(url)
			}
			else if(Array.isArray(url))
			{
				for (let index = 0; index < url.length; index++) 
				{
					cert.part(url[index])	
				}
			}
		}

		static part(url,max=0)
		{
			//max最长的词，的长度
			//被解析文件格式：
			// 猪儿虫
			// 你
			// 是
			// 一个
			$ExtendOther.word.part_max=max;
			const fs=require("fs");
			if(!fs.existsSync(url)) return false;
			var contentText = fs.readFileSync(url,'utf-8');
			contentText=contentText.split(/\n/);
			contentText.forEach(function(v)
			{
				if(v.length>max)
				{
					max=v.length;
					$ExtendOther.word.part_max=max;
				}
				if(v=v.trim()) $ExtendOther.word.part.add(v);
			});
			contentText=null;
		}

		static split(str,max=$ExtendOther.word.part_max?$ExtendOther.word.part_max:0)
		{
			//max最长的词，的长度
			let Gstr=[];
			let count=str.length;
			let i;
			for(i=0;i<count;i++)
			{
				let ii;
				let length;//最长的词，有多长
				let tag=undefined;
				let ccount=count-i;
				if(ccount<max)
				{
					length=ccount;
				}
				else
				{
					length=max;
				}
				for(ii=length;ii>0;ii--)
				{
					tag=str.substr(i,ii);
					if($ExtendOther.word.part.has(tag))
					{
						i=i+ii-1;
						break;
					}		
				}
				if(tag != undefined)
				{
					Gstr.push(tag);	
				}
			}	
			return Gstr		
		}

		static splitAll(str)
		{
			let strArr=[]
			let reg=/([\p{Number}|\p{Punctuation}|\p{Script=Greek}]+)/ug

			let strResult
			let strIndex=0
			while(strResult=reg.exec(str))
			{
				strArr=strArr.concat(cert.split(str.substring(strIndex,strResult.index)))
				strArr.push(strResult[1])
				strIndex=strResult.index+strResult[1].length
			}
			return strArr.concat(cert.split(str.slice(strIndex)))
		}

		static levenshtein(source, target) 
		{
			var sou_length = source.length + 1;
			var tar_length = target.length + 1;
		
			if (sou_length == 1) 
			{
				return tar_length - 1;
			}
			if (tar_length == 1) 
			{
				return sou_length - 1;
			}
		
			var matrix  = new Array();
			var row = new Array();
			for (let index = 0; index < tar_length; index++)
			{
				row[index] = index;
			}
			matrix[0] = row;
			for (let index = 1; index < sou_length; index++) 
			{
				var row = new Array();
				row[0] = index;
				for (let j = 1; j < tar_length; j++) 
				{
					row[j] = 0;
				}
				matrix[index] = row;
			}
		
			for (let index = 1; index < sou_length; index++) 
			{
				var sou_char = source[index-1];
				for (let j = 1; j < tar_length; j++) 
				{
					var tar_char = target[j-1];
					var cost;
					if (sou_char === tar_char) 
					{
						cost = 0;
					} else 
					{
						cost = 1;
					}
					var above = matrix[index-1][j]+1;
					var left = matrix[index][j-1]+1;
					var diag = matrix[index-1][j-1]+cost;
					var value = Math.min(above, left, diag);
					matrix[index][j] = value;
				}
			}
			return matrix[sou_length - 1][tar_length - 1];
		}

		static levenshteinPer(source, target)
		{
			let len=cert.levenshtein(source, target)
			let per=1-(len/Math.max(source.length,target.length))
			return {len:len,per:per}
		}
	}
/*-------------------------------------------------------------------------------------------*/    
module.exports=cert;