;词条拆分字根生成
split_wubizg(input){
	global DB
	If (input="")
		Return []
	else
	{
		if strlen(input)==1
		{
			If DB.gettable("SELECT A_Key FROM EtymologyChr WHERE aim_chars = '" input "'", Result){
				Result_ := Result.Rows[1,1]
			}
			Return RegExReplace(Result_,"\〔|\〕","")
		} else if strlen(input)==2{
			loop % 2
			{
				If DB.gettable("SELECT A_Key,B_Key FROM EtymologyPhrase WHERE aim_chars = '" SubStr(input,a_index,1) "'", Result){
					Result_part :=Result.Rows[1,1] . Result.Rows[1,2]
				}
				Result_ .=Result_part
			}
			Return Result_
		} else if strlen(input)==3{
			loop % 3
			{
				If DB.gettable("SELECT A_Key,B_Key FROM EtymologyPhrase WHERE aim_chars = '" SubStr(input,a_index,1) "'", Result){
					if a_index=3
						Result_part := Result.Rows[1,1] . Result.Rows[1,2]
					else
						Result_part := Result.Rows[1,1]
				}
				Result_ .=Result_part
			}
			Return Result_
		} else if strlen(input)>3{
			loop % 4
			{
				if a_index=4
					SQL_chars :=SubStr(input,0,1)
				else
					SQL_chars :=SubStr(input,a_index,1)
				If DB.gettable("SELECT A_Key,B_Key FROM EtymologyPhrase WHERE aim_chars = '" SQL_chars "'", Result){
					Result_part := Result.Rows[1,1]
				}
				Result_ .=Result_part
			}
			Return Result_
		}
	}
}

;快捷置顶
set_top(list_num){
	global
	local selectvalue, Result
	If (list_num>ListNum)||(list_num=0)
		Return
	selectvalue:=srf_for_select_Array[list_num+ListNum*waitnum,1]
	selectvalue :=RegExReplace(selectvalue,"\#\〔.+","")
	new_Array :=[]
	loop % srf_for_select_Array.Length()
	{
		if (a_index<(list_num+ListNum*waitnum))
		{
			new_Array[a_index+1,1]:=srf_for_select_Array[a_index,1]
		}
		else if (a_index>(list_num+ListNum*waitnum))
		{
			new_Array[a_index,1]:=srf_for_select_Array[a_index,1]
		}
		else if (a_index=(list_num+ListNum*waitnum))
		{
			new_Array[1,1]:=srf_for_select_Array[a_index,1]
		}
	}
	if (lianx<>"on"&&Wubi_Schema ~="chaoji|ci")
	{
		count_2 :=0
		loop % srf_for_select_Array.Length()
		{
			SQL :="DELETE FROM " (Wubi_Schema ~="i)ci"?"ci":(Wubi_Schema ~="i)zi"?"":"chaoji")) " WHERE aim_chars ='" srf_for_select_Array[a_index,1] "' and A_Key ='" srf_all_Input "';"
			DB.Exec(SQL)
		}
		loop % new_Array.Length()
		{
			SQL_:="INSERT INTO " (Wubi_Schema ~="i)ci"?"ci":(Wubi_Schema ~="i)zi"?"":"chaoji")) " VALUES('" new_Array[a_index,1] "','" srf_all_Input "')"
			if DB.Exec(SQL_)>0
			{
				count_2++
			}
		}
		if count_2>0
			TrayTip,, 置顶成功！置顶词条为「%srf_all_Input%-%selectvalue%」
	}
	else
		TrayTip,, 置顶失败，不符合条件！
}

;简体转繁体
set_trad_mode(chars){
	global DB
	If (chars="")
		Return []
	else
	{
		If DB.gettable("SELECT A_Key FROM s2t WHERE aim_chars = '" chars "'", Result){
			if Result.Rows[1,1]
			{
				Return Result.Rows[1,1]
			}
			else
			{
				loop % strlen(chars)
				{
					If DB.gettable("SELECT A_Key FROM s2t WHERE aim_chars = '" SubStr(chars,a_index,1) "'", Result){
						If Result.Rows[1,1]
							Result_1 :=Result.Rows[1,1]
						else
							Result_1 :=SubStr(chars,a_index,1)
					}
					Result_ .=Result_1
				}
				Return Result_
			}
		}
	}
}

;自造词条编码生成
get_en_code(chars){
	global DB
	If (chars="")
		Return []
	else
	{
		if strlen(chars)==1
		{
			If DB.gettable("SELECT A_Key FROM EN_Chr WHERE aim_chars = '" chars "'", Result){
				Result_ := Result.Rows[1,1]
			}
			Return Result_
		}
		else if strlen(chars)==2
		{
			loop % 2
			{
				If DB.gettable("SELECT A_Key FROM EN_Chr WHERE aim_chars= '" SubStr(chars,a_index,1) "'", Result){
					Result_part := SubStr(Result.Rows[1,1],1,2)
				}
				Result_ .=Result_part
			}
			Return Result_
		}
		else if strlen(chars)==3
		{
			loop % 3
			{
				If DB.gettable("SELECT A_Key FROM EN_Chr WHERE aim_chars = '" SubStr(chars,a_index,1) "'", Result){
					If a_index=3
						Result_part := SubStr(Result.Rows[1,1],1,2)
					else
						Result_part := SubStr(Result.Rows[1,1],1,1)
				}
				Result_ .=Result_part
			}
			Return Result_
		}
		else if strlen(chars)>3
		{
			loop % 4
			{
				if a_index=4
					SQL_chars :=SubStr(chars,0,1)
				else
					SQL_chars :=SubStr(chars,a_index,1)
				If DB.gettable("SELECT A_Key FROM EN_Chr WHERE aim_chars = '" SQL_chars "'", Result){
					Result_part := SubStr(Result.Rows[1,1],1,1)
				}
				Result_ .=Result_part
			}
			Return Result_
		}
		else
		{
			Return chars
			
		}
	}
}

;造词结果保存
Save_word(chars){
	global
	If (chars="")
		Return []
	else
	{
		chars :=RegExReplace(RegExReplace(chars,"(`n){2,}","`n"),"^\s+|^`n|^\t|\s+$|`n$|\t$","")
		count_1 :=0
		loop,parse,chars,`n
		{
			if A_LoopField ~="="
			{
				Chars_L :=RegExReplace(RegExReplace(A_LoopField,"^\s+|^`n|^\t|\s+$|`n$|\t$",""),"\=.+","")
				Chars_R :=RegExReplace(RegExReplace(A_LoopField,"^\s+|^`n|^\t|\s+$|`n$|\t$",""),".+\=","")
				if DB.GetTable("SELECT A_Key FROM ci WHERE aim_chars ='" Chars_R "' and A_Key='" Chars_L "';",Result)
					if Result.RowCount<1&&Chars_L ~="[a-y]"&&DB.Exec("INSERT INTO ci VALUES('" Chars_R "','" Chars_L "');")>0
						count_1++
			}
			else
			{
				mb_code :=get_en_code(A_LoopField)
				If mb_code
				{
					if DB.GetTable("SELECT A_Key FROM ci WHERE aim_chars ='" A_LoopField "' and A_Key='" mb_code "';",Result)
						if Result.RowCount<1&&DB.Exec("INSERT INTO ci VALUES('" A_LoopField "','" mb_code "');")>0
							count_1++
				}
			}
		}
		Return count_1
	}
}

;「`」引导精准造词
format_word(input){
	global
	If (input="")
		Return []
	citiao_arr:=[],count:=0
	loop,parse,input,``
	{
		count++
		SQL :="SELECT aim_chars FROM zi WHERE A_Key = '" A_LoopField "'" 
		If DB.GetTable(SQL, Result)
		{
			if (Result.Rows[1,1]<>"")
				Return Result.Rows
		}
	}
}

;精准造词选词
Select_add(list_num){
	global
	local selectvalue
	If (list_num>ListNum)||(list_num=0)
		Return
	selectvalue:=srf_for_select_Array[list_num+ListNum*waitnum,1]
	count_ci:=count_ci+1
	select_value[count_ci]:=selectvalue
	Select_result .=select_value[count_ci]
	if (count_ci <=srf_input.Length()+1){
		select_arr[1]:=Select_result
		Gosub srf_tooltip_fanye
	}

}

;词条提取
get_word(input, cikuname){
	global
	If (input="")
		Return []
	If (cikuname~="chaoji|zi|ci|labal")
	{
		lianx :=""
		if input ~="^z"
		{
			lianx :="on"
			SQL :="SELECT aim_chars,C_Key,D_Key FROM pinyin WHERE REPLACE(A_key,' ','') ='" RegExReplace(input,"^z|'","") "' ORDER BY B_Key DESC;"
			If DB.GetTable(SQL, Result)
				Return Result.Rows
		}else if input ~="^[a-y]+"{
			SQL :="SELECT aim_chars FROM " (cikuname="chaoji"?"chaoji":(cikuname="ci"?"ci":"zi")) " WHERE A_Key = '" input "'" 
			If DB.GetTable(SQL, Result)
			{
				if (Result.Rows[1,1]=""&&strlen(input)>1&&Prompt_Word ~="on"||Result.Rows[1,1]=""&&strlen(input)<4&&Prompt_Word ~="on")
				{
					lianx :="on"
					SQL :="SELECT aim_chars FROM " (cikuname="chaoji"?"chaoji":(cikuname="ci"?"ci":"zi")) " WHERE A_key LIKE '" input "_'"
					If DB.GetTable(SQL, Result){
						if Trad_Mode~="off"{
								loop, % Result.RowCount
									Result.Rows[a_index,2]:=split_wubizg(Result.Rows[a_index,1])
						}
						else
						{
							loop, % Result.RowCount
							{
								if set_trad_mode(Result.Rows[a_index,1])
									Result_ :=set_trad_mode(Result.Rows[a_index,1])
								Result_part_all .=RegExReplace(Result_,"\s+","`n") "`n"
							}
							loop,parse,Result_part_all,`n
							{
								if A_LoopField
								{
									Result.Rows[a_index,1] :=A_LoopField
									Result.Rows[a_index,2]:=split_wubizg(A_LoopField)
								}
							}
						}	Result_ :=Result_part_all:=""
					}
				}
				else
				{
					if Trad_Mode~="off"{
						loop, % Result.RowCount
							Result.Rows[a_index,2]:=split_wubizg(Result.Rows[a_index,1])
					}
					else
					{
						lianx :="on"
						loop, % Result.RowCount
						{
							if set_trad_mode(Result.Rows[a_index,1])
								Result_ :=set_trad_mode(Result.Rows[a_index,1])
							Result_part_all .=RegExReplace(Result_,"\s+","`n") "`n"
						}
						loop,parse,Result_part_all,`n
						{
							if A_LoopField
							{
								Result.Rows[a_index,1] :=A_LoopField
								Result.Rows[a_index,2]:=split_wubizg(A_LoopField)
							}
						}
						Result_ :=Result_part_all:=""
				}}
			}
			Return Result.Rows
		}
	}
}

;选词
srf_select(list_num){
	global
	local selectvalue, Result, Index, yhnum, tt, Match
	If (list_num>ListNum)||(list_num=0)
		Return
	selectvalue:=srf_for_select_Array[list_num+ListNum*waitnum,1]
	if selectvalue~="\#\〔"
	{
		Gosub % RegExReplace(selectvalue,"\#\〔.+","")
	}else{
		if Initial_Mode ~="on"
		{
			clipboard := selectvalue
			;clipboard := StringUpper(selectvalue, "T")
			send ^v
			updateRecent(selectvalue)    ;写入历史记录
			if RegExReplace(srf_all_Input,"^\``","")~="\``"{
				Save_word(selectvalue)
			}
		}
		else
		{
			SendInput % selectvalue
			;SendInput % StringUpper(selectvalue, "T")
			updateRecent(selectvalue)    ;写入历史记录
			if RegExReplace(srf_all_Input,"^\``","")~="\``"{
				Save_word(selectvalue)
			}
	}}
	Gosub srf_value_off
	srf_for_select_Array :=select_arr:=srf_input:=[], select_sym:=0, srf_for_select_for_tooltip :=Result_arr:=Select_result:="",count_ci:=1
}

;快捷删词
Delete_Word(list_num){
	global
	local selectvalue, Result, Index, yhnum, tt, Match
	If (list_num>ListNum)||(list_num=0)
		Return
	selectvalue:=srf_for_select_Array[list_num+ListNum*waitnum,1]
	selectvalue :=RegExReplace(selectvalue,"\#\〔.+","")
	SQL :="DELETE FROM " (Wubi_Schema ~="ci"?"ci":"chaoji") " WHERE aim_chars ='" selectvalue "' and A_Key ='" srf_all_Input "';"
	if srf_all_Input ~="[a-y]"&&lianx<>"on"&&Wubi_Schema ~="ci|chaoji"{
		if DB.Exec(SQL)>0
			TrayTip,, 「%selectvalue%-%srf_all_Input%」删除成功
		else
			TrayTip,, 条件不成立，删除失败！
	}
	else
		TrayTip,, 条件不成立，删除失败！
}

;划词反查词条读音提取
get_single_py(chars){
	global
	If (chars="")
		Return []
	else if (chars ~="\d+|[a-z]+")
		Return []
	else
	{
		SQL_ :="SELECT A_Key FROM PY WHERE aim_chars = '" chars "'"
		if DB.gettable(SQL_,Result){
			if Result.Rows[1,1]
				Return Result.Rows[1,1]
		}
	}
}

;划词反查处理
Tip_rvlk(chars){
	global DB
	rev_code :=RegExReplace(chars, "\d+|[a-zA-Z]{1,}|\s+|`n|\~|~[a-zA-Z]+", "")
	len_code :=wStrLen(rev_code)
	loop %len_code%
	{
		SQL :="SELECT A_Key FROM EtymologyChr WHERE aim_chars = '" SubStr(rev_code, A_index, 1) "'" 
		If DB.GetTable(SQL, Result)
		{
			if Result.Rows[1,1]
				rvlk_ :=SubStr(rev_code, A_index, 1) . Result.Rows[1,1] . "(" . get_en_code(SubStr(rev_code, A_index, 1)) . "·" . get_single_py(SubStr(rev_code, A_index, 1)) ")"
		}
		rvlk_all .= rvlk_ . "`n"
	}
	return rvlk_all
}

cut_word(chars_1,chars_2){
	global
	chars_1 :=RegExReplace(chars_1,"`n","")
	chars_2 :=RegExReplace(chars_2,"`n","")
	if strlen(chars_1)=1
	{
		Return chars_2
	}
	else if strlen(chars_1)=2
	{
		loop % strlen(chars_1)
		{
			Result_1 .= "※" . SubStr(RegExReplace(chars_2,"〔|\〕|\※|\s+|`n",""),1,4)
		}
		Result :=chars_1 . "〔" . Result_1 . "※〕"
		Return Result
	}
	else if strlen(chars_1)=3
	{
		loop % strlen(chars_1)
		{
			if a_index =3
				Result_2 .=SubStr(RegExReplace(chars_2,"〔|\〕|\※|\s+|`n",""),1,4)
			else
				Result_2 .=SubStr(RegExReplace(chars_2,"〔|\〕|\※|\s+|`n",""),1,2)
			Result_1 .= "※" . Result_2
		}
		Result :=chars_1 . "〔" . Result_1 . "※〕"
		Return Result
	}
	else if strlen(chars_1)>3
	{
		loop 4
		{
			if a_index =4
				Result_2 .=SubStr(RegExReplace(chars_2,"〔|\〕|\※|\s+|`n",""),0,2)
			else
				Result_2 .=SubStr(RegExReplace(chars_2,"〔|\〕|\※|\s+|`n",""),1,2)
			Result_1 .= "※" . Result_2
		}
		Result :=chars_1 . "〔" . Result_1 . "※〕"
		Return Result
	}
}

;英文词提取
prompt_enword(input){
	global
	If (input="")
		Return []
	SQL :="SELECT aim_chars FROM encode WHERE aim_chars LIKE '" input "%' ORDER BY A_Key DESC;"
	If DB.GetTable(SQL, Result){
		if !Result.Rows[1,1]
			Result.Rows[1,1]:=input,Result.Rows[2,1]:=StringUpper(input),Result.Rows[3,1]:=StringUpper(input,"T")
		Return Result.Rows
	}
}

;~键反查拼音
prompt_pinyin(input){
	global DB
	If (input="")
		Return []
	input:=RegExReplace(input,"~","")
	prompt_result:=get_word(input,Wubi_Schema)
	prompt_all:=[],count:=0
	if prompt_result[1,1]{
		loop % prompt_result.Length()
		{
			result_:=prompt_result[a_index,1]
			if (get_single_py(result_)&&strlen(result_)=1){
				count++
				prompt_all[count,1]:=result_ A_space get_single_py(result_)
		}}
		Return prompt_all
	}
}

;标签提取
prompt_label(input){
	global DB
	If (input="")
		Return []
	SQL :="SELECT C_key,D_Key FROM label WHERE B_Key ='" RegExReplace(input,"/","") "'"
	If DB.GetTable(SQL, Result){
		if Result.Rows[1,1]
			Return Result.Rows
	}
}

;特殊符号提取
prompt_symbols(input){
	global
	If (input="")
		Return []
	SQL :="SELECT A_key FROM symbols WHERE aim_chars ='" input "'"
	DB.GetTable(SQL_, Result_label)
	If DB.GetTable(SQL, Result)
		Result_label:=prompt_label(input)
		if Result.Rows[1,1]&&Result_label[1,1]{
			Result_:=Result.Rows[1,1]
			loop % Result_label.Length(){
				if Result_label[a_index,1]~="i)^A\_"{
					sj :=Result_label[a_index,1]
					loop,parse,sj,%A_space%
					{
						if not A_LoopField~="[a-z\_]"
							sj_:=A_LoopField
						else
							sj_:=%A_LoopField%
						_sj .=sj_
					}
					Result.Rows[a_index,1]:=_sj Result_label[a_index,2]
					_sj:=sj_:=
				}
				else
					Result.Rows[a_index,1]:=Result_label[a_index,1] Result_label[a_index,2]
			}
			loop,parse,Result_,`,
					Result.Rows[Result_label.Length()+a_index,1]:=A_LoopField
		}
		else if !Result.Rows[1,1]&&Result_label[1,1]{
			loop % Result_label.Length(){
				if Result_label[a_index,1]~="i)^A\_"{
					sj :=Result_label[a_index,1]
					loop,parse,sj,%A_space%
					{
						if not A_LoopField~="[a-z\_]"
							sj_:=A_LoopField
						else
							sj_:=%A_LoopField%
						_sj .=sj_
					}
					Result.Rows[a_index,1]:=_sj Result_label[a_index,2]
					_sj:=sj_:=
				}
				else
					Result.Rows[a_index,1]:=Result_label[a_index,1] Result_label[a_index,2]
			}
		}
		else if Result.Rows[1,1]&&!Result_label[1,1]{
			Result_:=Result.Rows[1,1]
			loop,parse,Result_,`,
				Result.Rows[a_index,1]:=A_LoopField
		}
		else
		{
			if strlen(input)>1&&srf_all_Input~="[a-zA-Z]"{
				if srf_all_Input~="i)zzsj|zzrq|zznl"{
					Result.Rows:=srf_all_Input~="i)zznl"?Get_LunarDate():(srf_all_Input~="i)zzrq"?Get_Date():Get_Time())
				}else{
					Result.Rows[1,1]:=RegExReplace(input,"^/","")
					Result.Rows[2,1]:=StringUpper(RegExReplace(input,"^/",""),"T")
					Result.Rows[3,1]:=StringUpper(RegExReplace(input,"^/",""))
				}
			}else if strlen(input)>1&&srf_all_Input~="[0-9]+"{
				Result.Rows:=numTohz(RegExReplace(input,"^/",""))
			}
		}
	Return Result.Rows
}

;模糊词提取
get_prompt_word(input, cikuname){
	global
	If (input="")
		Return []
	if strlen(input)>1&&strlen(input)<4
		SQL :="SELECT aim_chars FROM " (cikuname="chaoji"?"chaoji":(cikuname="ci"?"ci":"zi")) " WHERE A_key LIKE '" input "_'"
	else
		SQL :="SELECT aim_chars FROM " (cikuname="chaoji"?"chaoji":(cikuname="ci"?"ci":"zi")) " WHERE A_key = '" input "'"
	If DB.GetTable(SQL, Result)
	{
		loop, % Result.RowCount
			Result.Rows[a_index,2]:=split_wubizg(Result.Rows[a_index,1])
	}
	Return Result.Rows
}

;创建表
Create_Ci(DB,Name)
{
	global
	If !Name
		Return
	SQL:="DROP TABLE IF EXISTS " (Wubi_Schema ~="i)ci"?"ci":(Wubi_Schema ~="i)zi"?"zi":"chaoji")) ";"
	DB.Exec(SQL)
	DB.Exec("BEGIN TRANSACTION;")
	_SQL := _SQL := "CREATE TABLE " (Wubi_Schema ~="i)ci"?"ci":(Wubi_Schema ~="i)zi"?"zi":"chaoji")) " ('aim_chars' TEXT,'A_Key' TEXT);"
	DB.Exec(_SQL)
	DB.Exec("COMMIT TRANSACTION;VACUUM;")
}

;创建label表
Create_label(DB,Name){
	global
	If !Name
		Return
	DB.Exec("DROP TABLE IF EXISTS label;")
	DB.Exec("BEGIN TRANSACTION;")
	_SQL = CREATE TABLE label ("A_Key" INTEGER PRIMARY KEY AUTOINCREMENT,"B_Key" TEXT,"C_Key" TEXT,"D_Key" TEXT);
	DB.Exec(_SQL)
	DB.Exec("COMMIT TRANSACTION;VACUUM;")
}

;创建encode&&symbols表
Create_En(DB,Name){
	global
	If !Name
		Return
	bd~="i)En"?(DB.Exec("DROP TABLE IF EXISTS encode;")):(DB.Exec("DROP TABLE IF EXISTS symbols;"))
	DB.Exec("BEGIN TRANSACTION;")
	If bd~="i)En"
		_SQL = CREATE TABLE encode ("aim_chars" TEXT,"A_Key" INTEGER);
	else
		_SQL = CREATE TABLE symbols ("aim_chars" TEXT,"A_Key" TEXT);
	DB.Exec(_SQL)
	DB.Exec("COMMIT TRANSACTION;VACUUM;")
}
