firts commit

This commit is contained in:
2025-10-24 10:25:29 +09:00
commit ec77d8bc29
49 changed files with 134191 additions and 0 deletions

View File

@@ -0,0 +1,743 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using System.Collections.Generic;
namespace KART_API.Services
{
using CString = IntPtr;
using KiwiHandle = IntPtr;
using KiwiBuilderHandle = IntPtr;
using KiwiResHandle = IntPtr;
using KiwiWsHandle = IntPtr;
using KiwiTypoHandle = IntPtr;
using KiwiJoinerHandle = IntPtr;
internal class Utf8String : IDisposable
{
IntPtr iPtr;
public IntPtr IntPtr { get { return iPtr; } }
public int BufferLength { get { return iBufferSize; } }
int iBufferSize;
public Utf8String(string aValue)
{
if (aValue == null)
{
iPtr = IntPtr.Zero;
}
else
{
byte[] bytes = Encoding.UTF8.GetBytes(aValue);
iPtr = Marshal.AllocHGlobal(bytes.Length + 1);
Marshal.Copy(bytes, 0, iPtr, bytes.Length);
Marshal.WriteByte(iPtr, bytes.Length, 0);
iBufferSize = bytes.Length + 1;
}
}
public void Dispose()
{
if (iPtr != IntPtr.Zero)
{
Marshal.FreeHGlobal(iPtr);
iPtr = IntPtr.Zero;
}
}
}
internal class Utf16String : IDisposable
{
IntPtr iPtr;
public IntPtr IntPtr { get { return iPtr; } }
public int BufferLength { get { return iBufferSize; } }
int iBufferSize;
public Utf16String(string aValue)
{
if (aValue == null)
{
iPtr = IntPtr.Zero;
}
else
{
byte[] bytes = new UnicodeEncoding().GetBytes(aValue);
iPtr = Marshal.AllocHGlobal(bytes.Length + 2);
Marshal.Copy(bytes, 0, iPtr, bytes.Length);
Marshal.WriteByte(iPtr, bytes.Length, 0);
Marshal.WriteByte(iPtr, bytes.Length + 1, 0);
iBufferSize = bytes.Length + 2;
}
}
public void Dispose()
{
if (iPtr != IntPtr.Zero)
{
Marshal.FreeHGlobal(iPtr);
iPtr = IntPtr.Zero;
}
}
}
internal class Utf8StringArray : IDisposable
{
IntPtr iPtr, bPtr;
public IntPtr IntPtr { get { return bPtr; } }
public Utf8StringArray(string[] aValue)
{
if (aValue == null)
{
iPtr = IntPtr.Zero;
}
else
{
int totalLength = 0;
byte[][] pool = new byte[aValue.Length][];
for (int i = 0; i < aValue.Length; i++)
{
pool[i] = Encoding.UTF8.GetBytes(aValue[i]);
totalLength += pool[i].Length + 1;
}
iPtr = Marshal.AllocHGlobal(totalLength);
bPtr = Marshal.AllocHGlobal(IntPtr.Size * pool.Length);
int offset = 0;
for (int i = 0; i < aValue.Length; i++)
{
Marshal.Copy(pool[i], 0, iPtr + offset, pool[i].Length);
Marshal.WriteByte(iPtr, pool[i].Length, 0);
Marshal.WriteIntPtr(bPtr, IntPtr.Size * i, iPtr + offset);
offset += pool[i].Length + 1;
}
}
}
public void Dispose()
{
if (iPtr != IntPtr.Zero)
{
Marshal.FreeHGlobal(iPtr);
Marshal.FreeHGlobal(bPtr);
iPtr = IntPtr.Zero;
bPtr = IntPtr.Zero;
}
}
}
public class KiwiException : Exception
{
public KiwiException()
{
}
public KiwiException(string message) : base(message)
{
}
public KiwiException(string message, Exception inner) : base(message, inner)
{
}
}
public struct Result
{
public Token[] morphs;
public float prob;
}
public struct ExtractedWord
{
public string word;
public float score, posScore;
public int freq;
}
internal class KiwiCAPI
{
private const string dll_name = "kiwi.dll";
public static IntPtr LoadDll(string path)
{
var is64 = IntPtr.Size == 8;
var subfolder = "\\bin_" + (is64 ? "x64\\" : "x86\\");
return LoadLibrary(path + subfolder + dll_name);
}
[StructLayout(LayoutKind.Sequential)]
public struct TokenInfo
{
public uint chrPosition; /* 시작 위치(UTF16 문자 기준) */
public uint wordPosition; /* 어절 번호(공백 기준)*/
public uint sentPosition; /* 문장 번호*/
public uint lineNumber; /* 줄 번호*/
public ushort length; /* 길이(UTF16 문자 기준) */
public byte _tag; /* 품사 태그 */
public byte senseId; /* 의미 번호 */
public float score; /* 해당 형태소의 언어모델 점수 */
public float typoCost; /* 오타가 교정된 경우 오타 비용. 그렇지 않은 경우 0 */
public uint typoFormId; /* 교정 전 오타의 형태에 대한 정보 (typoCost가 0인 경우 의미 없음) */
public uint pairedToken; /* SSO, SSC 태그에 속하는 형태소의 경우 쌍을 이루는 반대쪽 형태소의 위치(-1인 경우 해당하는 형태소가 없는 것을 뜻함) */
public uint subSentPosition; /* 인용부호나 괄호로 둘러싸인 하위 문장의 번호. 1부터 시작. 0인 경우 하위 문장이 아님을 뜻함 */
}
[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int CReader(int id, IntPtr buf, IntPtr userData);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int CReceiver(int id, IntPtr kiwi_res, IntPtr userData);
// global functions
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern CString kiwi_version();
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern CString kiwi_error();
// builder functions
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern KiwiBuilderHandle kiwi_builder_init(CString modelPath, int maxCache, int options);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_builder_close(KiwiBuilderHandle handle);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_builder_add_word(KiwiBuilderHandle handle, CString word, CString pos, float score);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_builder_load_dict(KiwiBuilderHandle handle, CString dictPath);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern KiwiWsHandle kiwi_builder_extract_words_w(KiwiBuilderHandle handle, CReader reader, IntPtr userData, int minCnt, int maxWordLen, float minScore, float posThreshold);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern KiwiWsHandle kiwi_builder_extract_add_words_w(KiwiBuilderHandle handle, CReader reader, IntPtr userData, int minCnt, int maxWordLen, float minScore, float posThreshold);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern KiwiHandle kiwi_builder_build(KiwiBuilderHandle handle, KiwiTypoHandle typos, float typo_cost_threshold);
// analyzer functions
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_get_option(KiwiHandle handle, int option);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern void kiwi_set_option(KiwiHandle handle, int option, int value);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern float kiwi_get_option_f(KiwiHandle handle, int option);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern void kiwi_set_option_f(KiwiHandle handle, int option, float value);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern KiwiResHandle kiwi_analyze_w(KiwiHandle handle, IntPtr text, int topN, int matchOptions);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern KiwiResHandle kiwi_analyze(KiwiHandle handle, IntPtr text, int topN, int matchOptions);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_analyze_mw(KiwiHandle handle, CReader reader, CReceiver receiver, IntPtr userData, int topN, int matchOptions);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_analyze_m(KiwiHandle handle, CReader reader, CReceiver receiver, IntPtr userData, int topN, int matchOptions);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_close(KiwiHandle handle);
// result management functions
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_res_size(KiwiResHandle result);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern float kiwi_res_prob(KiwiResHandle result, int index);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_res_word_num(KiwiResHandle result, int index);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr kiwi_res_form_w(KiwiResHandle result, int index, int num);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr kiwi_res_tag_w(KiwiResHandle result, int index, int num);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr kiwi_res_form(KiwiResHandle result, int index, int num);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr kiwi_res_tag(KiwiResHandle result, int index, int num);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_res_position(KiwiResHandle result, int index, int num);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_res_length(KiwiResHandle result, int index, int num);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_res_word_position(KiwiResHandle result, int index, int num);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_res_sent_position(KiwiResHandle result, int index, int num);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern ref TokenInfo kiwi_res_token_info(KiwiResHandle result, int index, int num);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_res_close(KiwiResHandle result);
// word management functions
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_ws_size(KiwiWsHandle result);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr kiwi_ws_form_w(KiwiWsHandle result, int index);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern float kiwi_ws_score(KiwiWsHandle result, int index);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_ws_freq(KiwiWsHandle result, int index);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern float kiwi_ws_pos_score(KiwiWsHandle result, int index);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_ws_close(KiwiWsHandle result);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern KiwiTypoHandle kiwi_typo_init();
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern KiwiTypoHandle kiwi_typo_get_basic();
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_typo_add(KiwiTypoHandle typo, IntPtr orig, int origSize, IntPtr error, int errorSize, float cost, int condition);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_typo_close(KiwiTypoHandle typo);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern KiwiJoinerHandle kiwi_new_joiner(KiwiHandle handle, int lmSearch);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_joiner_add(KiwiJoinerHandle handle, CString form, CString tag, int option);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern CString kiwi_joiner_get(KiwiJoinerHandle handle);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern CString kiwi_joiner_get_w(KiwiJoinerHandle handle);
[DllImport(dll_name, CallingConvention = CallingConvention.Cdecl)]
public static extern int kiwi_joiner_close(KiwiJoinerHandle handle);
public static Result[] ToResult(KiwiResHandle kiwiresult)
{
int resCount = kiwi_res_size(kiwiresult);
if (resCount < 0) throw new KiwiException(Marshal.PtrToStringAnsi(kiwi_error()));
Result[] ret = new Result[resCount];
for (int i = 0; i < resCount; ++i)
{
int num = kiwi_res_word_num(kiwiresult, i);
ret[i].morphs = new Token[num];
for (int j = 0; j < num; ++j)
{
ret[i].morphs[j].form = Marshal.PtrToStringUni(kiwi_res_form_w(kiwiresult, i, j));
ret[i].morphs[j].tag = Marshal.PtrToStringUni(kiwi_res_tag_w(kiwiresult, i, j));
var ti = kiwi_res_token_info(kiwiresult, i, j);
ret[i].morphs[j].chrPosition = ti.chrPosition;
ret[i].morphs[j].wordPosition = ti.wordPosition;
ret[i].morphs[j].sentPosition = ti.sentPosition;
ret[i].morphs[j].lineNumber = ti.lineNumber;
ret[i].morphs[j].length = ti.length;
ret[i].morphs[j].senseId = ti.senseId;
ret[i].morphs[j].score = ti.score;
ret[i].morphs[j].typoCost = ti.typoCost;
ret[i].morphs[j].typoFormId = ti.typoFormId;
ret[i].morphs[j].pairedToken = ti.pairedToken;
ret[i].morphs[j].subSentPosition = ti.subSentPosition;
}
ret[i].prob = kiwi_res_prob(kiwiresult, i);
}
return ret;
}
public static ExtractedWord[] ToExtractedWord(KiwiWsHandle kiwiresult)
{
int resCount = kiwi_ws_size(kiwiresult);
if (resCount < 0) throw new KiwiException(Marshal.PtrToStringAnsi(kiwi_error()));
ExtractedWord[] ret = new ExtractedWord[resCount];
for (int i = 0; i < resCount; ++i)
{
ret[i].word = Marshal.PtrToStringUni(kiwi_ws_form_w(kiwiresult, i));
ret[i].score = kiwi_ws_score(kiwiresult, i);
ret[i].posScore = kiwi_ws_pos_score(kiwiresult, i);
ret[i].freq = kiwi_ws_freq(kiwiresult, i);
}
return ret;
}
}
public struct Token
{
public string form;
public string tag;
public uint chrPosition; /* 시작 위치(UTF16 문자 기준) */
public uint wordPosition; /* 어절 번호(공백 기준)*/
public uint sentPosition; /* 문장 번호*/
public uint lineNumber; /* 줄 번호*/
public ushort length; /* 길이(UTF16 문자 기준) */
public byte senseId; /* 의미 번호 */
public float score; /* 해당 형태소의 언어모델 점수 */
public float typoCost; /* 오타가 교정된 경우 오타 비용. 그렇지 않은 경우 0 */
public uint typoFormId; /* 교정 전 오타의 형태에 대한 정보 (typoCost가 0인 경우 의미 없음) */
public uint pairedToken; /* SSO, SSC 태그에 속하는 형태소의 경우 쌍을 이루는 반대쪽 형태소의 위치(-1인 경우 해당하는 형태소가 없는 것을 뜻함) */
public uint subSentPosition; /* 인용부호나 괄호로 둘러싸인 하위 문장의 번호. 1부터 시작. 0인 경우 하위 문장이 아님을 뜻함 */
}
public enum Option
{
IntegrateAllomorph = 1 << 0,
LoadDefaultDict = 1 << 1,
LoadTypoDict = 1 << 2,
MaxUnkFormSize = 0x8002,
SpaceTolerance = 0x8003,
CutOffThreshold = 0x9001,
UnkFormScoreScale = 0x9002,
UnkFormScoreBias = 0x9003,
SpacePenalty = 0x9004,
TypoCostWeight = 0x9005,
}
public enum ModelType
{
KNLM = 0x0000,
SBG = 0x0100,
}
public enum MatchWord
{
Url = 1 << 0,
Email = 1 << 1,
Hashtag = 1 << 2,
Mention = 1 << 3,
Serial = 1 << 4,
All = Url | Email | Hashtag | Mention | Serial,
NormalizeCoda = 1 << 16,
JoinNounPrefix = 1 << 17,
JoinNounSuffix = 1 << 18,
JoinVerbSuffix = 1 << 19,
JoinAdjSuffix = 1 << 20,
JoinAdvSuffix = 1 << 21,
SplitComplex = 1 << 22,
ZCoda = 1 << 23,
}
public class KiwiLoader
{
private static IntPtr dllHandle = IntPtr.Zero;
public static string GetDefaultPath()
{
var myPath = new Uri(typeof(KiwiCAPI).Assembly.CodeBase).LocalPath;
string path = Path.GetDirectoryName(myPath);
return path;
}
public static bool LoadDll(string path = null)
{
if (dllHandle != IntPtr.Zero) return true;
if (path == null)
{
path = GetDefaultPath();
}
return (dllHandle = KiwiCAPI.LoadDll(path)) != IntPtr.Zero;
}
}
public class TypoTransformer
{
public KiwiTypoHandle inst;
public TypoTransformer(bool useBasicSet = false)
{
inst = useBasicSet ? KiwiCAPI.kiwi_typo_get_basic() : KiwiCAPI.kiwi_typo_init();
}
public int Add(string[] orig, string[] error, float cost, int condition)
{
if (inst == KiwiCAPI.kiwi_typo_get_basic())
{
throw new InvalidOperationException("basic typo object cannot be modified!");
}
return KiwiCAPI.kiwi_typo_add(inst, new Utf8StringArray(orig).IntPtr, orig.Length, new Utf8StringArray(error).IntPtr, error.Length, cost, condition);
}
~TypoTransformer()
{
if (inst != null && inst != KiwiCAPI.kiwi_typo_get_basic())
{
KiwiCAPI.kiwi_typo_close(inst);
}
}
}
public class KiwiBuilder
{
public delegate string Reader(int id);
private KiwiBuilderHandle inst;
private Reader reader;
private Tuple<int, string> readItem;
static KiwiBuilder()
{
if (KiwiLoader.LoadDll()) return;
if (KiwiLoader.LoadDll(KiwiLoader.GetDefaultPath() + "\\..")) return;
if (KiwiLoader.LoadDll(KiwiLoader.GetDefaultPath() + "\\..\\..")) return;
}
private static KiwiCAPI.CReader readerInst = (int id, IntPtr buf, IntPtr userData) =>
{
GCHandle handle = (GCHandle)userData;
KiwiBuilder ki = handle.Target as KiwiBuilder;
if (ki.readItem?.Item1 != id)
{
ki.readItem = new Tuple<int, string>(id, ki.reader(id));
}
if (buf == IntPtr.Zero)
{
return ki.readItem.Item2?.Length ?? 0;
}
KiwiCAPI.CopyMemory(buf, new Utf16String(ki.readItem.Item2).IntPtr, (uint)ki.readItem.Item2.Length * 2);
return 0;
};
public KiwiBuilder(string modelPath, int numThreads = 0, Option options = Option.LoadDefaultDict | Option.LoadTypoDict | Option.IntegrateAllomorph, ModelType modelType = ModelType.KNLM)
{
inst = KiwiCAPI.kiwi_builder_init(new Utf8String(modelPath).IntPtr, numThreads, (int)options | (int)modelType);
if (inst == IntPtr.Zero) throw new KiwiException(Marshal.PtrToStringAnsi(KiwiCAPI.kiwi_error()));
}
public int AddWord(string word, string pos, float score)
{
int ret = KiwiCAPI.kiwi_builder_add_word(inst, new Utf8String(word).IntPtr, new Utf8String(pos).IntPtr, score);
if (ret < 0) throw new KiwiException(Marshal.PtrToStringAnsi(KiwiCAPI.kiwi_error()));
return ret;
}
public int LoadDictionary(string dictPath)
{
int ret = KiwiCAPI.kiwi_builder_load_dict(inst, new Utf8String(dictPath).IntPtr);
if (ret < 0) throw new KiwiException(Marshal.PtrToStringAnsi(KiwiCAPI.kiwi_error()));
return ret;
}
public ExtractedWord[] ExtractWords(Reader reader, int minCnt = 5, int maxWordLen = 10, float minScore = 0.1f, float posThreshold = -3)
{
GCHandle handle = GCHandle.Alloc(this);
this.reader = reader;
readItem = null;
KiwiWsHandle ret = KiwiCAPI.kiwi_builder_extract_words_w(inst, readerInst, (IntPtr)handle, minCnt, maxWordLen, minScore, posThreshold);
handle.Free();
if (inst == IntPtr.Zero) throw new KiwiException(Marshal.PtrToStringAnsi(KiwiCAPI.kiwi_error()));
ExtractedWord[] words = KiwiCAPI.ToExtractedWord(ret);
KiwiCAPI.kiwi_ws_close(ret);
return words;
}
public ExtractedWord[] ExtractAddWords(Reader reader, int minCnt = 5, int maxWordLen = 10, float minScore = 0.1f, float posThreshold = -3)
{
GCHandle handle = GCHandle.Alloc(this);
this.reader = reader;
readItem = null;
KiwiWsHandle ret = KiwiCAPI.kiwi_builder_extract_add_words_w(inst, readerInst, (IntPtr)handle, minCnt, maxWordLen, minScore, posThreshold);
handle.Free();
if (inst == IntPtr.Zero) throw new KiwiException(Marshal.PtrToStringAnsi(KiwiCAPI.kiwi_error()));
ExtractedWord[] words = KiwiCAPI.ToExtractedWord(ret);
KiwiCAPI.kiwi_ws_close(ret);
return words;
}
public Kiwi Build()
{
KiwiHandle ret = KiwiCAPI.kiwi_builder_build(inst, (IntPtr)null, 0);
if (ret == IntPtr.Zero) throw new KiwiException(Marshal.PtrToStringAnsi(KiwiCAPI.kiwi_error()));
return new Kiwi(ret);
}
public Kiwi Build(TypoTransformer typo, float typoCostThreshold = 2.5f)
{
KiwiHandle ret = KiwiCAPI.kiwi_builder_build(inst, typo.inst, typoCostThreshold);
if (ret == IntPtr.Zero) throw new KiwiException(Marshal.PtrToStringAnsi(KiwiCAPI.kiwi_error()));
return new Kiwi(ret);
}
~KiwiBuilder()
{
if (inst != IntPtr.Zero)
{
if (KiwiCAPI.kiwi_builder_close(inst) < 0) throw new KiwiException(Marshal.PtrToStringAnsi(KiwiCAPI.kiwi_error()));
}
}
}
public class KiwiJoiner
{
private KiwiJoinerHandle inst;
public KiwiJoiner(KiwiJoinerHandle _inst)
{
inst = _inst;
}
public string Get()
{
return Marshal.PtrToStringUni(KiwiCAPI.kiwi_joiner_get_w(inst));
}
public void Add(string form, string tag, int option = 1)
{
if (KiwiCAPI.kiwi_joiner_add(inst, new Utf8String(form).IntPtr, new Utf8String(tag).IntPtr, option) < 0) throw new KiwiException(Marshal.PtrToStringAnsi(KiwiCAPI.kiwi_error()));
}
~KiwiJoiner()
{
if (inst != IntPtr.Zero)
{
if (KiwiCAPI.kiwi_joiner_close(inst) < 0) throw new KiwiException(Marshal.PtrToStringAnsi(KiwiCAPI.kiwi_error()));
}
}
}
public class Kiwi
{
public delegate string Reader(int id);
public delegate int Receiver(int id, Result[] res);
private KiwiHandle inst;
private Reader reader;
private Receiver receiver;
private Tuple<int, string> readItem;
static Kiwi()
{
if (KiwiLoader.LoadDll()) return;
if (KiwiLoader.LoadDll(KiwiLoader.GetDefaultPath() + "\\..")) return;
if (KiwiLoader.LoadDll(KiwiLoader.GetDefaultPath() + "\\..\\..")) return;
}
private static KiwiCAPI.CReader readerInst = (int id, IntPtr buf, IntPtr userData) =>
{
GCHandle handle = (GCHandle)userData;
Kiwi ki = handle.Target as Kiwi;
if (ki.readItem?.Item1 != id)
{
ki.readItem = new Tuple<int, string>(id, ki.reader(id));
}
if (buf == IntPtr.Zero)
{
return ki.readItem.Item2?.Length ?? 0;
}
KiwiCAPI.CopyMemory(buf, new Utf16String(ki.readItem.Item2).IntPtr, (uint)ki.readItem.Item2.Length * 2);
return 0;
};
private static KiwiCAPI.CReceiver receiverInst = (int id, KiwiResHandle kiwi_res, IntPtr userData) =>
{
GCHandle handle = (GCHandle)userData;
Kiwi ki = handle.Target as Kiwi;
return ki.receiver(id, KiwiCAPI.ToResult(kiwi_res));
};
public Kiwi(KiwiHandle _inst)
{
inst = _inst;
}
public static string Version()
{
return Marshal.PtrToStringAnsi(KiwiCAPI.kiwi_version());
}
public Result[] Analyze(string text, int topN = 1, MatchWord matchOptions = MatchWord.All)
{
KiwiResHandle res = KiwiCAPI.kiwi_analyze_w(inst, new Utf16String(text).IntPtr, topN, (int)matchOptions);
if (inst == IntPtr.Zero) throw new KiwiException(Marshal.PtrToStringAnsi(KiwiCAPI.kiwi_error()));
Result[] ret = KiwiCAPI.ToResult(res);
KiwiCAPI.kiwi_res_close(res);
return ret;
}
public void AnalyzeMulti(Reader reader, Receiver receiver, int topN = 1, MatchWord matchOptions = MatchWord.All)
{
GCHandle handle = GCHandle.Alloc(this);
this.reader = reader;
this.receiver = receiver;
readItem = null;
int ret = KiwiCAPI.kiwi_analyze_mw(inst, readerInst, receiverInst, (IntPtr)handle, topN, (int)matchOptions);
handle.Free();
if (ret < 0) throw new KiwiException(Marshal.PtrToStringAnsi(KiwiCAPI.kiwi_error()));
}
public KiwiJoiner NewJoiner(bool lmSearch = true)
{
var h = KiwiCAPI.kiwi_new_joiner(inst, lmSearch ? 1 : 0);
if (h == IntPtr.Zero) throw new KiwiException(Marshal.PtrToStringAnsi(KiwiCAPI.kiwi_error()));
return new KiwiJoiner(h);
}
public bool IntegrateAllomorph
{
get { return KiwiCAPI.kiwi_get_option(inst, (int)Option.IntegrateAllomorph) != 0; }
set { KiwiCAPI.kiwi_set_option(inst, (int)Option.IntegrateAllomorph, value ? 1 : 0); }
}
public int MaxUnkFormSize
{
get { return KiwiCAPI.kiwi_get_option(inst, (int)Option.MaxUnkFormSize); }
set { KiwiCAPI.kiwi_set_option(inst, (int)Option.MaxUnkFormSize, value); }
}
public int SpaceTolerance
{
get { return KiwiCAPI.kiwi_get_option(inst, (int)Option.SpaceTolerance); }
set { KiwiCAPI.kiwi_set_option(inst, (int)Option.SpaceTolerance, value); }
}
public float CutOffThreshold
{
get { return KiwiCAPI.kiwi_get_option_f(inst, (int)Option.CutOffThreshold); }
set { KiwiCAPI.kiwi_set_option_f(inst, (int)Option.CutOffThreshold, value); }
}
public float UnkFormScoreScale
{
get { return KiwiCAPI.kiwi_get_option_f(inst, (int)Option.UnkFormScoreScale); }
set { KiwiCAPI.kiwi_set_option_f(inst, (int)Option.UnkFormScoreScale, value); }
}
public float UnkFormScoreBias
{
get { return KiwiCAPI.kiwi_get_option_f(inst, (int)Option.UnkFormScoreBias); }
set { KiwiCAPI.kiwi_set_option_f(inst, (int)Option.UnkFormScoreBias, value); }
}
public float SpacePenalty
{
get { return KiwiCAPI.kiwi_get_option_f(inst, (int)Option.SpacePenalty); }
set { KiwiCAPI.kiwi_set_option_f(inst, (int)Option.SpacePenalty, value); }
}
public float TypoCostWeight
{
get { return KiwiCAPI.kiwi_get_option_f(inst, (int)Option.TypoCostWeight); }
set { KiwiCAPI.kiwi_set_option_f(inst, (int)Option.TypoCostWeight, value); }
}
~Kiwi()
{
if (inst != IntPtr.Zero)
{
if (KiwiCAPI.kiwi_close(inst) < 0) throw new KiwiException(Marshal.PtrToStringAnsi(KiwiCAPI.kiwi_error()));
}
}
}
}

View File

@@ -0,0 +1,82 @@
using System;
using KART_API.Utils;
namespace KART_API.Services
{
public class LogService
{
static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(LogService));
/// <summary>
/// 로그인 로그 기록
/// </summary>
/// <param name="userEmail">User</param>
public static void insertLoginLog(string userEmail)
{
MSDAL msdal = new MSDAL();
var connection = msdal.ConnectReport();
try
{
string sqlString = " INSERT INTO dbo.TB_LOGIN_LOG(M_USER, L_IP) VALUES('" + userEmail + "', '" + Common.GetRemoteIP() + "'); ";
msdal.ExecuteSQL(sqlString, connection);
msdal.Disconnect(connection);
}
catch (Exception ex)
{
logger.Fatal("Log > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
}
finally
{
msdal.Disconnect(connection);
}
}
public static void InsertSiteLog(string mkey, string projectCode, string logTitle, string logContent)
{
MSDAL msdal = new MSDAL();
var connection = msdal.ConnectReport();
try
{
string logString = "{\"projectCode\":\"" + projectCode + "\", \"LOGTITLE\":\"" + logTitle + "\", \"LOGCONTENT\":" + logContent + "}";
string userIP = Common.GetRemoteIP();
string insertString = "insert into TB_SITE_LOG(M_USER, S_PAGE_INFO, S_IP) values ('" + mkey + "','" + logString + "','" + userIP + "')";
msdal.ExecuteSQL(insertString, connection);
}
catch (Exception ex)
{
logger.Fatal("Log > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
}
finally
{
msdal.Disconnect(connection);
}
}
/// <summary>
/// Visit Log 작성
/// </summary>
/// <param name="mkey">User Key</param>
/// <param name="logContent">로그 내용</param>
public static void InsertVisitLog(string mkey, string projectCode, string logContent)
{
MSDAL msdal = new MSDAL();
var connection = msdal.ConnectReport();
try
{
string userIP = Common.GetRemoteIP();
string insertString = "insert into TB_VISIT_LOG(M_USER, V_PAGE_INFO, V_PKEY, V_IP) values ('" + mkey + "','" + logContent + "','" + projectCode + "','" + userIP + "')";
msdal.ExecuteSQL(insertString, connection);
}
catch (Exception ex)
{
logger.Fatal("Log > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
}
finally
{
msdal.Disconnect(connection);
}
}
}
}

View File

@@ -0,0 +1,375 @@
using System;
using System.Linq;
using System.Drawing;
using OfficeOpenXml;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OfficeOpenXml.Style;
using KART_API.Utils;
using Svg;
namespace KART_API.Services
{
public class MakeExcelService
{
public byte[] getTableExcel(string dataString, string isTurn)
{
JArray tableArray = Common.GetJArrayFromString(dataString);
string contentString = "목차";
string tableString = "테이블";
ExcelPackage ExcelPkg = new ExcelPackage();
ExcelWorksheet wsSheet1 = ExcelPkg.Workbook.Worksheets.Add(contentString);
ExcelWorksheet wsSheet2 = ExcelPkg.Workbook.Worksheets.Add(tableString);
wsSheet1.Cells.Style.Font.Name = "맑은 고딕";
wsSheet1.Cells.Style.Font.Size = 10;
wsSheet1.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
wsSheet1.Cells.Style.WrapText = false;
wsSheet2.Cells.Style.Font.Name = "맑은 고딕";
wsSheet2.Cells.Style.Font.Size = 9;
wsSheet2.Cells.Style.WrapText = true;
for (int row = 1; row <= (tableArray.Count + 4); row++)
{
wsSheet1.Row(row).Height = 20;
}
////1,1부터 시작
using (ExcelRange Rng = wsSheet1.Cells[1, 1, 4, 2])
{
Rng.Value = "목차";
Rng.Merge = true;
Rng.Style.Font.Size = 25;
Rng.Style.Font.Bold = true;
Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
Rng.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
Rng.Style.Font.Color.SetColor(Color.FromArgb(0xFFFFFF));
Rng.Style.Fill.PatternType = ExcelFillStyle.Solid;
Rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(0x333333));
}
using (ExcelRange Rng = wsSheet1.Cells[5, 1, (5 + tableArray.Count-1), 2])
{
Rng.Style.Border.Top.Style = ExcelBorderStyle.Hair;
Rng.Style.Border.Bottom.Style = ExcelBorderStyle.Hair;
Rng.Style.Border.Right.Style = ExcelBorderStyle.Hair;
Rng.Style.Border.Left.Style = ExcelBorderStyle.Hair;
Rng.Style.Border.BorderAround(ExcelBorderStyle.Thin);
}
int tableRow = 1;
for(int i = 0; i < tableArray.Count; i++)
{
var tableJson = tableArray[i];
if (String.IsNullOrEmpty(tableJson["body"].ToString()) == false)
{
///목차
string titleString = string.Empty;
if (String.IsNullOrEmpty(Common.GetNullString(tableJson["vname"])))
{
titleString = Common.StripHTML(tableJson["head"].ToString());
}
else
{
titleString = "<표 " + tableJson["vname"] + ">" + " " + Common.StripHTML(tableJson["head"].ToString());
}
titleString = titleString.Replace("<br />", "");
var titleCell = wsSheet1.Cells[(5 + i), 1, (5 + i), 1];
var linkCell = wsSheet1.Cells[(5 + i), 2, (5 + i), 2];
titleCell.Value = titleString;
Uri url = new Uri("#'" + tableString + "'!A" + tableRow, UriKind.Relative);
linkCell.Value = "Go";
linkCell.Hyperlink = url;
linkCell.Style.Font.UnderLine = true;
linkCell.Style.Font.Color.SetColor(Color.Blue);
///목차
///테이블
JArray tableBody = Common.GetJArrayFromString(tableJson["body"].ToString());
if (tableJson["qtype"].ToString() == "single" && isTurn == "1")
{
for(int brow = 0; brow < tableBody.Count; brow++)
{
string valueString = string.Empty;
if(brow == 0)
{
valueString = "계||";
}
else
{
valueString = "100.0";
}
JObject tempRow = tableBody[brow] as JObject;
tempRow.Add("add_0", valueString);
tableBody[brow] = tempRow;
}
}
using (ExcelRange Rng = wsSheet2.Cells[tableRow, 1, tableRow, tableBody.First.SelectTokens("*").Count()])
{
Rng.Value = titleString;
Rng.Merge = true;
Rng.Style.Font.Bold = true;
tableRow++;
}
int meanPoint = 0;
int tableStartRow = tableRow;
int startRowSpan = 0;
int startColSpan = 0;
for (int row = 0; row < tableBody.Count; row++)
{
var bodyRow = tableBody[row].Values();
int col = 1;
foreach (var bodyCell in bodyRow)
{
var cellDetail = bodyCell.ToString().Split('|');
if (cellDetail.Length == 3)
{
int rowSpan = Convert.ToInt32(Common.GetNullToZero(cellDetail[1]));
int colSpan = Convert.ToInt32(Common.GetNullToZero(cellDetail[2]));
if (rowSpan > 0)
{
rowSpan = rowSpan - 1;
}
if (colSpan > 0)
{
colSpan = colSpan - 1;
}
if (row == 0 && col == 1)
{
startRowSpan = rowSpan;
startColSpan = colSpan;
}
using (ExcelRange Rng = wsSheet2.Cells[tableRow, col, tableRow + rowSpan, col + colSpan])
{
if(rowSpan > 0 || colSpan > 0)
{
Rng.Merge = true;
}
Rng.Style.Border.BorderAround(ExcelBorderStyle.Thin);
Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
if(row > startRowSpan - 1 && col < startColSpan + 2)
{
Rng.Style.VerticalAlignment = ExcelVerticalAlignment.Top;
}
else
{
Rng.Style.VerticalAlignment = ExcelVerticalAlignment.Bottom;
}
}
}
using (ExcelRange Rng = wsSheet2.Cells[tableRow, col, tableRow, col])
{
if (cellDetail[0] == "Mean")
{
if (isTurn != "1")
{
meanPoint = tableRow;
}
else
{
meanPoint = col;
}
}
Rng.Style.Border.Left.Style = ExcelBorderStyle.Thin;
Rng.Value = Common.StripHTML(cellDetail[0]);
}
col++;
}
tableRow++;
}
int tableMidRow = tableStartRow + startRowSpan;
int tableMidCol = 1 + startColSpan;
int tableEndRow = tableStartRow + tableBody.Count - 1;
int tableEndCol = tableBody.First.SelectTokens("*").Count();
if (String.IsNullOrEmpty(tableJson["wcr"].ToString()) == false)
{
var svgDoc = SvgDocument.FromSvg<SvgDocument>(tableJson["wcr"].ToString());
svgDoc.CustomAttributes["fill"] = "white";
SvgRectangle rect = new SvgRectangle();
rect.Fill = new SvgColourServer(Color.White);
rect.Width = svgDoc.Width;
rect.Height = svgDoc.Height;
svgDoc.Children.Insert(0, rect);
var bitmap = svgDoc.Draw();
insertPicture(wsSheet2, bitmap, tableStartRow, 1 + tableEndCol);
}
wsSheet2.Cells[tableStartRow, 1, tableMidRow, tableMidCol].Style.Border.BorderAround(ExcelBorderStyle.Thick);
wsSheet2.Cells[tableStartRow, tableMidCol + 1, tableMidRow, tableEndCol].Style.Border.BorderAround(ExcelBorderStyle.Thick);
wsSheet2.Cells[tableMidRow + 1, 1, tableEndRow, tableMidCol].Style.Border.BorderAround(ExcelBorderStyle.Thick);
wsSheet2.Cells[tableMidRow + 1, tableMidCol + 1, tableEndRow, tableEndCol].Style.Border.BorderAround(ExcelBorderStyle.Thick);
for (int row = tableMidRow + 1; row <= tableEndRow; row++)
{
for (int col = tableMidCol + 1; col <= tableEndCol; col++)
{
using (ExcelRange Rng = wsSheet2.Cells[row, col])
{
if (Common.IsNumeric(Rng.Value.ToString()))
{
if (Rng.Value.ToString().IndexOf(".") < 0)
{
///사례수
Rng.Value = "(" + Rng.Value + ")";
}
else if (isTurn != "1" && row == meanPoint || isTurn == "1" && col == meanPoint)
{
Rng.Style.Numberformat.Format = "0.00";
Rng.Value = Convert.ToDouble(Rng.Value);
}
else
{
Rng.Style.Numberformat.Format = "0.0";
Rng.Value = Convert.ToDouble(Rng.Value);
}
}
else
{
Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
}
}
}
}
///테이블
tableRow++;
}
}
wsSheet1.Column(1).AutoFit();
wsSheet1.View.FreezePanes(5, 1);
return ExcelPkg.GetAsByteArray();
}
public byte[] getQuotaExcel(string dataString)
{
ExcelPackage ExcelPkg = new ExcelPackage();
JArray tableArray = Common.GetJArrayFromString(dataString);
for (int i = 0; i < tableArray.Count; i++)
{
var tableJson = tableArray[i];
string titleString = Common.StripHTML(tableJson["head"].ToString());
JArray tableBody = Common.GetJArrayFromString(tableJson["body"].ToString());
ExcelWorksheet wsSheet = ExcelPkg.Workbook.Worksheets.Add(titleString);
wsSheet.Cells.Style.Font.Name = "맑은 고딕";
wsSheet.Cells.Style.Font.Size = 9;
//wsSheet.Cells.Style.WrapText = true;
int tableRow = 1;
using (ExcelRange Rng = wsSheet.Cells[tableRow, 1, tableRow, tableBody.First.SelectTokens("*").Count()])
{
Rng.Value = titleString;
Rng.Merge = true;
Rng.Style.Font.Bold = true;
Rng.Style.Font.Size = 14;
Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
Rng.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
tableRow++;
}
wsSheet.Row(1).Height = 30;
int tableStartRow = tableRow;
int startRowSpan = 0;
int startColSpan = 0;
for (int row = 0; row < tableBody.Count; row++)
{
var bodyRow = tableBody[row].Values();
int col = 1;
foreach (var bodyCell in bodyRow)
{
var cellDetail = bodyCell.ToString().Split('|');
if (cellDetail.Length == 3)
{
int rowSpan = Convert.ToInt32(Common.GetNullToZero(cellDetail[1]));
int colSpan = Convert.ToInt32(Common.GetNullToZero(cellDetail[2]));
if (rowSpan > 0)
{
rowSpan = rowSpan - 1;
}
if (colSpan > 0)
{
colSpan = colSpan - 1;
}
if (row == 0 && col == 1)
{
startRowSpan = rowSpan;
startColSpan = colSpan;
}
using (ExcelRange Rng = wsSheet.Cells[tableRow, col, tableRow + rowSpan, col + colSpan])
{
if (rowSpan > 0 || colSpan > 0)
{
Rng.Merge = true;
}
Rng.Style.Border.BorderAround(ExcelBorderStyle.Thin);
Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
Rng.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
}
}
using (ExcelRange Rng = wsSheet.Cells[tableRow, col, tableRow, col])
{
string cellValue = Common.StripHTML(cellDetail[0]);
if (tableRow > tableStartRow + startRowSpan && col > 1 + startColSpan)
{
cellValue = cellValue.Substring(0, cellValue.IndexOf("/", cellValue.IndexOf("/") + 1));
}
Rng.Style.Border.Left.Style = ExcelBorderStyle.Thin;
Rng.Value = Common.StripHTML(cellValue);
Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
Rng.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
}
col++;
}
tableRow++;
}
int tableMidRow = tableStartRow + startRowSpan;
int tableMidCol = 1 + startColSpan;
int tableEndRow = tableStartRow + tableBody.Count - 1;
int tableEndCol = tableBody.First.SelectTokens("*").Count();
wsSheet.Cells[tableStartRow, 1, tableMidRow, tableMidCol].Style.Border.BorderAround(ExcelBorderStyle.Thick);
wsSheet.Cells[tableStartRow, tableMidCol + 1, tableMidRow, tableEndCol].Style.Border.BorderAround(ExcelBorderStyle.Thick);
wsSheet.Cells[tableMidRow + 1, 1, tableEndRow, tableMidCol].Style.Border.BorderAround(ExcelBorderStyle.Thick);
wsSheet.Cells[tableMidRow + 1, tableMidCol + 1, tableEndRow, tableEndCol].Style.Border.BorderAround(ExcelBorderStyle.Thick);
wsSheet.Cells[wsSheet.Dimension.Address].AutoFitColumns();
}
return ExcelPkg.GetAsByteArray();
}
public void insertPicture(ExcelWorksheet worksheet, Bitmap imageData, int cellY, int cellX)
{
string imgName = cellY + "_" + cellX;
var picture = worksheet.Drawings.AddPicture(imgName, imageData);
picture.SetSize(1000, 512);
picture.SetPosition(cellY - 1, 30, cellX - 1, 0);
}
}
}

View File

@@ -0,0 +1,367 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Newtonsoft.Json.Linq;
using System.Collections.ObjectModel;
using KART_API.Utils;
using Microsoft.IdentityModel.Tokens;
namespace KART_API.Services
{
public class NlpService
{
static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(NlpService));
public class AnalyzeResult
{
public int Id { get; set; }
public string Input { get; set; }
public List<List<Token>> Result { get; set; }
public AnalyzeResult(int Id, string Input, List<List<Token>> Result)
{
this.Id = Id;
this.Input = Input;
this.Result = Result;
}
public AnalyzeResult(int Id, string Input, List<Token> Result)
{
this.Id = Id;
this.Input = Input;
this.Result = new List<List<Token>>();
this.Result.Add(Result);
}
}
public class WordTag
{
public string Text { get; set; } = string.Empty;
public string Tag { get; set; } = string.Empty;
public WordTag(string inText, string inTag)
{
Text = inText;
Tag = inTag;
}
}
public class WordCloudTag
{
public string Text { get; set; } = string.Empty;
//public string Tag { get; set; } = string.Empty;
public string Count { get; set; } = string.Empty;
public WordCloudTag(string inText, string inTag, string inCount)
{
Text = inText;
//Tag = inTag;
Count = inCount;
}
}
public string getWordTag(string projectCode, string qid, JArray dataJson)
{
string jsonData = String.Empty;
try
{
List<string> textDataList = new List<string>();
var vArrayList = dataJson.Where(x => x.Value<JArray>(qid).IsNullOrEmpty() == false).Select(x => x.Value<JArray>(qid).Children<JObject>().Values<string>("o"));
foreach (var vArray in vArrayList)
{
textDataList.AddRange(vArray);
}
List<AnalyzeResult> resultData = WordNLP(textDataList, projectCode, qid);
string[] tags = getTags(projectCode, qid);
//var positive = resultData.Select(x => x.Result.Where(y => y.Where(z => z.tag == "VCP").Count() > 0).ToList()).Where(x => x.Count > 0).ToList();
//var negative = resultData.Select(x => x.Result.Where(y => y.Where(z => z.tag == "VCN").Count() > 0).ToList()).Where(x => x.Count > 0).ToList();
//////여기 손봐서 업그레이드 해야함
List<WordTag> wordList =new List<WordTag>();
foreach (AnalyzeResult result in resultData)
{
foreach(List<Token> tokenList in result.Result)
{
foreach (Token token in tokenList)
{
if(Array.IndexOf(tags, token.tag) > -1 && (token.tag.StartsWith("N") || token.form.Length > 1) || token.tag == "SL")
{
wordList.Add(new WordTag(token.form, token.tag));
}
}
}
}
//////여기 손봐서 업그레이드 해야함
List<WordCloudTag> cloudTagList = makeTagTable(wordList, 100);
cloudTagList.Insert(0, new WordCloudTag("Text", "Tag", "Count"));
jsonData = Common.GetJsonFromObject(cloudTagList);
}
catch (Exception ex)
{
logger.Fatal(this.GetType().Name + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
}
return jsonData;
}
public List<AnalyzeResult> WordNLP(List<string> textDataList, string projectCode = "", string qid = "")
{
ObservableCollection<AnalyzeResult> resultData = new ObservableCollection<AnalyzeResult>();//결과???
try
{
Kiwi instKiwi;//키위 라이브러리
ModelType modelType = ModelType.SBG;//분석 모델(근거리 관계까지 추론 - 느림)
//KiwiCS.ModelType modelType = KiwiCS.ModelType.KNLM;//분석 모델(근거리 관계만 추론 - 빠름)
bool useTypoCorrection = false;//오타 수정(오래 걸림 잘 안되는듯?)
var builder = new KiwiBuilder(Common.MapPath("NlpModel/"), 0, Option.LoadDefaultDict | Option.LoadTypoDict, modelType);
builder = AddWords(builder, projectCode, qid); ////단어 추가
if (useTypoCorrection)
{
instKiwi = builder.Build(new TypoTransformer(true));////오타 수정 세팅
}
else
{
instKiwi = builder.Build();////오타 수정 세팅
}
int mode = 1;//어절:0/문장:1/줄:2/전체:3
int topN = 1;
instKiwi.IntegrateAllomorph = false;////이형태 통합
////아/어, 았/었 등의 이형태를 통합할 지 선택합니다.
MatchWord match = 0;
if (false) match |= MatchWord.NormalizeCoda;////종성우선 현상 정규화
////'먹었엌ㅋㅋ'처럼 연속하는 종성이 따라붙어 오분석이 나는 경우를 정규화합니다.
if (false) match |= MatchWord.Url;////url을 포함
if (false) match |= MatchWord.Email;////이메일을 포함
if (false) match |= MatchWord.Hashtag;////해시태그를 포함(#)
if (false) match |= MatchWord.Mention;////맨션을 포함(@+영어)
if (false) match |= MatchWord.Serial;////시리얼(일련번호)
if (false) match |= MatchWord.JoinNounPrefix;////명사+접두사
if (false) match |= MatchWord.JoinNounSuffix;////명사+접두사
if (false) match |= MatchWord.JoinVerbSuffix;////동사 파생접사
if (false) match |= MatchWord.JoinAdjSuffix;////형용사 파생 접사
foreach (string textData in textDataList)
{
foreach (var r in AnalyzeText(instKiwi, textData, mode, match, topN))
{
resultData.Add(r);
}
}
}
catch (Exception ex)
{
logger.Fatal(this.GetType().Name + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
}
return resultData.ToList();
}
private KiwiBuilder AddWords(KiwiBuilder builder, string projectCode, string qid)
{
//builder.AddWord("사랑", "NNP", 90);//// 이런식으로 추가
DataTable addWordTable = getAddWords(projectCode, qid);
foreach(DataRow addWordRow in addWordTable.Rows)
{
try
{
builder.AddWord(addWordRow["WORD_TEXT"].ToString(), addWordRow["WORD_TAG"].ToString(), Convert.ToInt32(addWordRow["WORD_SCORE"]));
}
catch(Exception ex)
{
}
}
// <단일 형태소를 추가하는 경우>
// (형태) \t (품사태그) \t (점수)
// * (점수)는 생략시 0으로 처리됩니다.
// 예) 키위 NNP -5.0
//
// <이미 존재하는 형태소의 이형태를 추가하는 경우>
// (이형태) \t (원형태소/품사태그) \t (점수)
// * (점수)는 생략시 0으로 처리됩니다.
// 예) 기위 키위/NNG -3.0
//
// <기분석 형태를 추가하는 경우>
// (형태) \t (원형태소/품사태그 + 원형태소/품사태그 + ...) \t (점수)
// * (점수)는 생략시 0으로 처리됩니다.
// 예) 사겼다 사귀/VV + 었/EP + 다/EF -1.0
//
// 현재는 공백을 포함하는 다어절 형태를 등록할 수 없습니다.
//
// <규칙 기반의 변형된 이형태를 추가하는 경우>
// (형태 규칙)$ \t (변형된 형태/품사태그) \t (점수)
// 예) 요$ 용/EF -5
return builder;
}
/// <summary>
/// 텍스트 분석기
/// </summary>
/// <param name="text">input</param>
/// <param name="mode">어절/문장/줄/전체</param>
/// <param name="match">옵션</param>
/// <param name="topN">결과 개수 몇개 까지?</param>
/// <returns>?</returns>
private IEnumerable<AnalyzeResult> AnalyzeText(Kiwi instKiwi, string text, int mode, MatchWord match, int topN = 1)
{
if (mode == 2)
{
int rid = 0;
foreach (var line in text.Trim().Split('\n'))
{
var trimmed = line.Trim();
var res = instKiwi.Analyze(trimmed, topN, match);
List<List<Token>> s = new List<List<Token>>();
foreach (var r in res)
{
if (r.morphs.Length == 0) continue;
s.Add(new List<Token>(r.morphs));
}
if (trimmed.Length == 0) continue;
yield return new AnalyzeResult(++rid, trimmed, s);
}
}
else if (mode == 3)
{
var res = instKiwi.Analyze(text, topN, match);
List<List<Token>> s = new List<List<Token>>();
foreach (var r in res)
{
if (r.morphs.Length == 0) continue;
s.Add(new List<Token>(r.morphs));
}
yield return new AnalyzeResult(1, text.Trim(), s);
}
else
{
var r = instKiwi.Analyze(text, 1, match)[0];
int wp = 0, sp = 0, cp = 0, rid = 0, c = 0;
List<Token> s = new List<Token>();
foreach (var m in r.morphs)
{
if (mode == 0 ? (wp != m.wordPosition || sp != m.sentPosition) : (sp != m.sentPosition))
{
yield return new AnalyzeResult(++rid, text.Substring(cp, (int)m.chrPosition - cp).Trim(), s);
cp = (int)m.chrPosition;
c = 0;
s = new List<Token>();
}
s.Add(m);
wp = (int)m.wordPosition;
sp = (int)m.sentPosition;
}
if (s.Count > 0)
{
yield return new AnalyzeResult(++rid, text.Substring(cp), s);
}
}
}
public string[] getTags(string projectCode, string qid)
{
List<string> tags = new List<string>();
MSDAL msdal = new MSDAL();
var connection = msdal.ConnectReport();
try
{
string sql = string.Empty;
sql += " SELECT DISTINCT [WORD_TAG] " + Environment.NewLine;
sql += " FROM( " + Environment.NewLine;
sql += " SELECT [WORD_TAG] FROM[REPORT_KON].[dbo].[REPORT_NLP_TAG] WHERE projectCode = 'ALL' " + Environment.NewLine;
sql += " UNION " + Environment.NewLine;
sql += " SELECT [WORD_TAG] FROM[REPORT_KON].[dbo].[REPORT_NLP_TAG] WHERE projectCode = '" + projectCode + "' AND QID = 'ALL' " + Environment.NewLine;
sql += " UNION " + Environment.NewLine;
sql += " SELECT [WORD_TAG] FROM[REPORT_KON].[dbo].[REPORT_NLP_TAG] WHERE projectCode = '" + projectCode + "' AND QID = '" + qid + "' " + Environment.NewLine;
sql += " ) TAGS " + Environment.NewLine;
DataTable tagTable = msdal.GetDataTable(sql, connection);
foreach (DataRow row in tagTable.Rows)
{
tags.Add(row["WORD_TAG"].ToString());
}
}
catch (Exception ex)
{
logger.Fatal(this.GetType().Name + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
}
finally
{
msdal.Disconnect(connection);
}
return tags.ToArray();
}
public DataTable getAddWords(string projectCode, string qid)
{
DataTable addWordsTable = new DataTable();
MSDAL msdal = new MSDAL();
var connection = msdal.ConnectReport();
try
{
string sql = string.Empty;
sql += " SELECT DISTINCT [WORD_TAG], [WORD_TEXT], [WORD_SCORE] " + Environment.NewLine;
sql += " FROM( " + Environment.NewLine;
sql += " SELECT [WORD_TAG], [WORD_TEXT], 10 as [WORD_SCORE] FROM [REPORT_KON].[dbo].[REPORT_NLP_WORD] WHERE projectCode = 'ALL' " + Environment.NewLine;
sql += " UNION " + Environment.NewLine;
sql += " SELECT [WORD_TAG], [WORD_TEXT], 50 as [WORD_SCORE] FROM [REPORT_KON].[dbo].[REPORT_NLP_WORD] WHERE projectCode = '" + projectCode + "' AND QID = 'ALL' " + Environment.NewLine;
sql += " UNION " + Environment.NewLine;
sql += " SELECT [WORD_TAG], [WORD_TEXT], 100 as [WORD_SCORE] FROM [REPORT_KON].[dbo].[REPORT_NLP_WORD] WHERE projectCode = '" + projectCode + "' AND QID = '" + qid + "' " + Environment.NewLine;
sql += " ) WORDS " + Environment.NewLine;
addWordsTable = msdal.GetDataTable(sql, connection);
}
catch (Exception ex)
{
logger.Fatal(this.GetType().Name + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
}
finally
{
msdal.Disconnect(connection);
}
return addWordsTable;
}
/// <summary>
/// 단어 사전을 태그화 시키는 모듈
/// </summary>
/// <param name="wordDic">단어 사전</param>
/// <returns>단어, 카운트 테이블</returns>
public List<WordCloudTag> makeTagTable(List<WordTag> wordDic, int maxCount)
{
List<WordCloudTag> cloudTagList = new List<WordCloudTag>();
var distinctDic = wordDic.Select(x => new { x.Text, x.Tag}).Distinct().ToList();
foreach (var word in distinctDic)
{
int tagCount = wordDic.Where(x => x.Text == word.Text && x.Tag == word.Tag).Count();
cloudTagList.Add(new WordCloudTag(word.Text, word.Tag, tagCount.ToString()));
}
cloudTagList = cloudTagList.OrderByDescending(x => Convert.ToInt32(x.Count)).Take(maxCount).ToList();
return cloudTagList;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,192 @@
using Accord;
using KART_API.Utils;
using Microsoft.Extensions.FileSystemGlobbing;
using Microsoft.Extensions.FileSystemGlobbing.Internal;
using System.Data.SqlTypes;
using System.Text.RegularExpressions;
namespace KART_API.Services
{
public class RegexFilterService
{
static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(RegexFilterService));
Regex fullSyntaxRegex = new Regex(@"\[(UK|uk)\:\d+\]+(?![^\[]*\])(\[.*?\])*\s*(\[.*?\])*\s*[=<>!]+\s*(?:[\d\/\,]*("")*)");
Regex questionUniqueKeyRegex = new Regex(@"\[(UK|uk)\:\d+\]");
Regex questionIdRegex = new Regex(@"\b([A-Za-z]\w+)+(?![^\[]*\])");
Regex inValueRegex = new Regex(@"\(\s*(\d+\s*(,\s*\d+\s*)*)\)");
Regex positiveOrRegex = new Regex(@"(?<!=|!|<|>)=\s*((?:\d+\/?)+)(?![^, ]*\\,)"); //-> S_V in (1,2,3) or in (1)
Regex positiveNullRegex = new Regex(@"((?<!!=|!|<|>)=\s*""+)"); //-> S_V is null
Regex negativeOrRegex = new Regex(@"(\!\=\s*\d+(?:\,\d+)+)"); //-> (S_V != 1 or S_V != 2 or S_V != 3)
Regex negativeNullRegex = new Regex(@"(\!\=\s*""+)"); //-> S_V is not null
Regex greaterLessRegex = new Regex(@"([<>])=?");
Regex operatorValueRegex = new Regex(@"[=<>!]+\s*(?:[\d\/\,]*("")*)");
Regex valueRegex = new Regex(@"\d+");
public string makeFilterString(string projectCode, string filter) {
bool isQ_UK = true;
filter = filter.ToUpper();
//QUK[L1][T2][M1][R1]
/////filterString이 Q|로 시작하면 QUK가 아닌 QID로 작성된 필터문
if (filter.IndexOf("Q|") == 0)
{
isQ_UK = false;
filter = filter.Replace("Q|", "");
}
while (filter.IndexOf(" ") > -1)
{
filter = filter.Replace(" ", " ");
}
filter = filter.Replace("< >", "<>");
filter = filter.Replace("! =", "!=");
filter = filter.Replace("IN", "=");
filter = filter.Replace("NOT IN", "!=");
filter = filter.Replace("IS NULL", "=\"\"");
filter = filter.Replace("IS NOT NULL", "!=\"\"");
filter = inValueToValueString(filter);
string[] reservedTargetArray = new string[] { "t", "m", "r", "l" };
foreach (Match fullsyntax in fullSyntaxRegex.Matches(filter))
{
List<string> fullSyntaxJoiner = new List<string>();
string questionUK = "";
string fullsyntaxString = fullsyntax.Value;
if (isQ_UK)
{
foreach (Match qukMatch in questionUniqueKeyRegex.Matches(fullsyntaxString))
{
questionUK = Regex.Replace(qukMatch.Value.ToString().Trim(), @"\D", "");
break;
}
}
else
{
foreach (Match qukMatch in questionIdRegex.Matches(fullsyntaxString))
{
questionUK = getQuestionUK(projectCode, qukMatch.Value.ToString().Trim());
break;
}
}
foreach(string reservedTarget in reservedTargetArray)
{
Regex reservedPattern = new Regex(@"\s*\[\s*("+reservedTarget+"|"+reservedTarget.ToUpper()+@")[\d\/\s]+\s*\]\s*");
foreach(Match reservedMatch in reservedPattern.Matches(fullsyntaxString))
{
fullSyntaxJoiner.Add("JSON_CONTAINS(DT.ANS_DATA -> '\\$.\"" + questionUK + "\"[*].\"" + reservedTarget + "\"',json_array(" + inValueString(reservedMatch.Value) + "))");
}
}
foreach (Match operatorValueMatch in operatorValueRegex.Matches(fullsyntaxString))
{
string matchString = operatorValueMatch.Value.ToString().Trim();
fullSyntaxJoiner.Add(replaceLogicValueString(matchString, questionUK));
}
filter = filter.Replace(fullsyntaxString, Environment.NewLine + "(" + String.Join(" AND" + Environment.NewLine, fullSyntaxJoiner) + ")");
}
string filterString = " AND ";
filterString += filter;
return filterString;
}
private string getQuestionUK(string projectCode, string questionID)
{
string questionUK = questionID;
MYDAL mydal = new MYDAL();
var connection = mydal.ConnectNewssak();
try
{
string sqlString = "SELECT Q_UK FROM question where PRJ_CD = '" + projectCode + "' and Q_ID = '" + questionID + "'";
var returnTable = mydal.GetDataTable(sqlString, connection);
if (returnTable != null)
{
questionUK = returnTable.Rows[0][0].ToString();
}
}
catch (Exception ex)
{
logger.Fatal(this.GetType().Name + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
}
finally
{
mydal.Disconnect(connection);
}
return questionUK;
}
private String replaceLogicValueString(String operatorValueString, String questionUK)
{
///////Range에 대한 것은 추후에 추가 하도록 하자.
String returnString = "";
if (positiveOrRegex.IsMatch(operatorValueString))
{
returnString = "JSON_OVERLAPS(DT.ANS_DATA -> '$.\"" + questionUK + "\"[*].\"s\"',json_array(" + inValueString(operatorValueString) + "))";
}
else if (positiveNullRegex.IsMatch(operatorValueString))
{
returnString = "DT.ANS_DATA -> '$.\"" + questionUK + "\"[*].\"s\"' is null";
}
else if (negativeOrRegex.IsMatch(operatorValueString))
{
returnString = " ( JSON_CONTAINS(DT.ANS_DATA -> '$.\"" + questionUK + "\"[*].\"s\"',json_array(" + inValueString(operatorValueString) + ")) = false OR DT.ANS_DATA -> '$.\"" + questionUK + "\"[*].\"s\"' is null ) ";
}
else if (negativeNullRegex.IsMatch(operatorValueString))
{
returnString = "DT.ANS_DATA -> '$.\"" + questionUK + "\"[*].\"s\"' is not null";
}
else
{
Match greaterLessMatcher = greaterLessRegex.Match(operatorValueString);
if (greaterLessMatcher.Success)
{
String greatLessString = greaterLessMatcher.Value;
returnString = "EXISTS(SELECT 1 FROM JSON_TABLE( DT.ANS_DATA -> '$.\"" + questionUK + "\"', '$[*]' COLUMNS(s INT PATH '$.s')) AS jt WHERE jt.s " + greatLessString + " " + inValueString(operatorValueString) + ") = 1";
}
}
return returnString;
}
private string inValueToValueString(string filter)
{
foreach(Match inValue in inValueRegex.Matches(filter))
{
filter = filter.Replace(inValue.Value, inValue.Value.Replace("(", "").Replace(")", "").Replace(",", "/"));
}
return filter;
}
private string inValueString(string originString)
{
List<string> valueString = new List<string>();
foreach(Match valueMatch in valueRegex.Matches(originString))
{
valueString.Add(valueMatch.Value);
}
return String.Join(",", valueString);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,252 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace KART_API.Services
{
public class StatisticsService
{
/// <summary>
/// Sum값 반환
/// </summary>
/// <param name="doubleList">실수 List</param>
/// <returns>Sum값</returns>
public static double getSum(List<double> doubleList)
{
return doubleList.Sum();
}
/// <summary>
/// Max값 반환
/// </summary>
/// <param name="doubleList">실수 List</param>
/// <returns>Max값</returns>
public static double getMax(List<double> doubleList)
{
return doubleList.Max();
}
/// <summary>
/// Min값 반환
/// </summary>
/// <param name="doubleList">실수 List</param>
/// <returns>Min값</returns>
public static double getMin(List<double> doubleList)
{
return doubleList.Min();
}
/// <summary>
/// 표준편차값 반환
/// </summary>
/// <param name="doubleList">실수 List</param>
/// <returns>표준편차값</returns>
public static double getStandardDeviation(List<double> doubleList)
{
double returnValue = 0.0;
double average = doubleList.Average();
double varianceSum = 0;
foreach (double value in doubleList)
{
varianceSum += (value) * (value);
}
double varianceAverage = varianceSum / doubleList.Count;
returnValue = Math.Sqrt(varianceAverage - (average * average));
return returnValue;
}
/// <summary>
/// 최빈값 반환
/// </summary>
/// <param name="doubleList">실수 List</param>
/// <returns>최빈값</returns>
public static double getMode(List<double> doubleList)
{
double returnValue = 0.0;
returnValue = doubleList.GroupBy(v => v)
.OrderByDescending(g => g.Count())
.First()
.Key;
return returnValue;
}
/// <summary>
/// 중위수 반환
/// </summary>
/// <param name="doubleList">실수 List</param>
/// <returns>중위수</returns>
public static double getMedian(List<double> doubleList)
{
double returnValue = 0.0;
int size = doubleList.Count;
int mid = size / 2;
doubleList.Sort();
returnValue = (size % 2 != 0) ? doubleList[mid] : (doubleList[mid] + doubleList[mid - 1]) / 2;
return returnValue;
}
/// <summary>
/// WilcoxonSignedRanks 구조
/// </summary>
public class WilcoxonSignedRanks
{
public double minusRankMean { get; set; } = 0.0;
public double plusRankMean { get; set; } = 0.0;
public double minusRankSum { get; set; } = 0.0;
public double plusRankSum { get; set; } = 0.0;
public double minRankSum { get; set; } = 0.0;
public double sampleSize { get; set; } = 0.0;
public double zValue { get; set; } = 0.0;
public double pValue { get; set; } = 0.0;
}
/// <summary>
/// WilcoxonSignedRank에 사용할 구조
/// </summary>
public class WSRList
{
public double xValue1 { get; set; } = 0.0;
public double xValue2 { get; set; } = 0.0;
public double diff { get; set; } = 0.0;
public double absDiff { get; set; } = 0.0;
}
/// <summary>
/// WilcoxonSignedRank에서 사용할 순위 구조
/// </summary>
public class RankList
{
public double value { get; set; } = 0.0;
public double rank { get; set; } = 0.0;
public double frequency { get; set; } = 0.0;
}
/// <summary>
/// WSRList 제작
/// </summary>
/// <param name="inTable">DataTable</param>
/// <param name="columName1">value1 Column Name</param>
/// <param name="columName2">value2 Column Name</param>
/// <returns>WSRList의 List</returns>
static public List<WSRList> makeWSRList(DataTable inTable, string columName1, string columName2)
{
List<WSRList> wsrList = new List<WSRList>();
foreach (DataRow inRow in inTable.Rows)
{
StatisticsService.WSRList wsrListItem = new StatisticsService.WSRList();
wsrListItem.xValue1 = Convert.ToDouble(inRow[columName1]);
wsrListItem.xValue2 = Convert.ToDouble(inRow[columName2]);
wsrList.Add(wsrListItem);
}
return wsrList;
}
/// <summary>
/// WilcoxonSignedRanksTest
/// </summary>
/// <param name="inTable">DataTable</param>
/// <param name="columName1">value1 Column Name</param>
/// <param name="columName2">value2 Column Name</param>
/// <returns>wilcoxonSignedRanks</returns>
static public WilcoxonSignedRanks getWilcoxonSignedRanksTest(DataTable inTable, string columName1 = "X1", string columName2 = "X2")
{
WilcoxonSignedRanks wilcoxonSignedRanks = new WilcoxonSignedRanks();
List<WSRList> wsrList = makeWSRList(inTable, columName1, columName2);
#region 차이, 차이 절댓값 계산
for (int i = 0; i < wsrList.Count; i++)
{
wsrList[i].diff = wsrList[i].xValue2 - wsrList[i].xValue1;
wsrList[i].absDiff = Math.Abs(wsrList[i].diff);
}
#endregion
#region 차이가 0인 샘플 삭제
wsrList = wsrList.Where(x => x.diff != 0.0).ToList();
#endregion
#region rank 기입
List<double> absDiffList = wsrList.Select(x => x.absDiff).Distinct().OrderBy(x => x).ToList();
List<RankList> rankList = new List<RankList>();
double accumulateCount = 0.0;
double frequencySum = 0.0;
for (int i = 0; i < absDiffList.Count(); i++)
{
double xValue = absDiffList[i];
double xCount = wsrList.Where(x => x.absDiff == xValue).ToList().Count();
double frequency = (Math.Pow(xCount, 3) - xCount) / 48;
RankList rankListItem = new RankList();
rankListItem.value = xValue;
rankListItem.rank = (accumulateCount + 1) + ((xCount - 1) / 2);
rankListItem.frequency = frequency;
rankList.Add(rankListItem);
frequencySum += frequency;
accumulateCount += xCount;
}
#endregion
#region 계산
double sampleSize = wsrList.Count;
double plusCount = 0.0;
double plusRankSum = 0.0;
double minusCount = 0.0;
double minusRankSum = 0.0;
double minRankSum = 0.0;
double denominator = 0.0; // 분모
double numerator = 0.0; //분자
for (int i = 0; i < wsrList.Count; i++)
{
double xRank = rankList.Where(x => x.value == wsrList[i].absDiff).Select(x => x.rank).ToList()[0];
if (wsrList[i].diff > 0)
{
plusCount++;
plusRankSum += xRank;
}
else if (wsrList[i].diff < 0)
{
minusCount++;
minusRankSum += xRank;
}
}
if (plusRankSum > minusRankSum)
{
minRankSum = minusRankSum;
}
else
{
minRankSum = plusRankSum;
}
denominator = Math.Sqrt(((sampleSize * (sampleSize + 1) * (2 * sampleSize + 1)) / 24) - frequencySum);
numerator = minRankSum - (sampleSize * (sampleSize + 1)) / 4;
#endregion
wilcoxonSignedRanks.plusRankSum = plusRankSum;
wilcoxonSignedRanks.plusRankMean = plusRankSum / plusCount;
wilcoxonSignedRanks.minusRankSum = minusRankSum;
wilcoxonSignedRanks.minusRankMean = minusRankSum / minusCount;
wilcoxonSignedRanks.minRankSum = minRankSum;
wilcoxonSignedRanks.sampleSize = sampleSize;
wilcoxonSignedRanks.zValue = numerator / denominator;
wilcoxonSignedRanks.pValue = Accord.Math.Special.Erfc(Math.Abs(wilcoxonSignedRanks.zValue) / Math.Sqrt(2));
return wilcoxonSignedRanks;
}
}
}

View File

@@ -0,0 +1,128 @@
using System;
using System.Data;
using System.Linq;
using KART_API.Dto;
using KART_API.Utils;
namespace KART_API.Services
{
public class URIService
{
static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(URIService));
/// <summary>
/// 단축 URL 반환
/// </summary>
/// <param name="realURI">실제 URL</param>
/// <returns>단축 URL</returns>
public static string GetURIKey(string realURI, int mode, string skey, string projectTitle, string okey)
{
string returnString = string.Empty;
MSDAL msdal = new MSDAL();
var connection = msdal.ConnectReport();
try
{
DataTable urlKeyTable = msdal.GetDataTable("select * from REPORT_URL with(nolock) where REAL_URL = '" + realURI + "' and SKEY ='"+ skey + "' and URL_OWNER ='" + okey + "' and IS_PAGE_ONLY="+mode, connection);
if (urlKeyTable.Rows.Count > 0)
{
returnString = urlKeyTable.Rows[0]["URL_KEY"].ToString();
}
else
{
DataTable urlKeysTable = msdal.GetDataTable("select URL_KEY from REPORT_URL with(nolock) ", connection);
string randomString = string.Empty;
do
{
randomString = Common.GetRandomString();
}
while (urlKeysTable.AsEnumerable().Where(dr => dr.Field<string>("URL_KEY") == randomString).ToList().Count > 0);
//while ((from DataRow dr in urlKeysTable.Rows where dr.Field<string>("URL_KEY") == randomString select dr).ToList().Count > 0);
string insertString = "insert into REPORT_URL(URL_KEY, REAL_URL, IS_PAGE_ONLY, SKEY, PROJECT_TITLE, URL_OWNER, LANDING_CNT) values ('" + randomString + "','" + realURI + "'," + mode + ",'" + skey + "','" + projectTitle + "','" + okey + "', 0)";
msdal.ExecuteSQL(insertString, connection);
returnString = randomString;
}
}
catch (Exception ex)
{
logger.Fatal("URI" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
}
finally
{
msdal.Disconnect(connection);
}
return returnString;
}
/// <summary>
/// 실제 URL 반환
/// </summary>
/// <param name="urlKey">단축 URL의 Key값</param>
/// <param name="isCount">카운트 Update 여부</param>
/// <returns>실제 URL</returns>
public static DataTable GetRealURL(string urlKey, bool isCount)
{
DataTable urlTable = new DataTable();
MSDAL msdal = new MSDAL();
var connection = msdal.ConnectReport();
try
{
urlTable = GetUriInfo(urlKey);
if (urlTable.Rows.Count > 0)
{
if (isCount)
{
string insertString = "update a set a.LANDING_CNT = a.LANDING_CNT + 1 from REPORT_URL a where URL_KEY = '" + urlKey + "'";
msdal.ExecuteSQL(insertString, connection);
}
}
}
catch (Exception ex)
{
logger.Fatal("URI" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
}
finally
{
msdal.Disconnect(connection);
}
return urlTable;
}
public static DataTable GetUriInfo(string urlKey)
{
DataTable uriInfo = new DataTable();
MSDAL msdal = new MSDAL();
var connection = msdal.ConnectReport();
try
{
uriInfo = msdal.GetDataTable("SELECT [URL_KEY],[REAL_URL],[IS_PAGE_ONLY],[SKEY], '' as [VER],[PROJECT_TITLE],[URL_OWNER] FROM REPORT_URL WHERE [URL_KEY] collate Korean_Wansung_CS_AS = '" + urlKey + "'", connection);
if (uriInfo.Rows.Count > 0)
{
projectCodeInfo projectCodeInfo = new projectCodeInfo(uriInfo.Rows[0]["SKEY"].ToString());
Common.SetEnableDataTableForEdit(ref uriInfo,0);
uriInfo.Columns["VER"].MaxLength = 50;
uriInfo.Rows[0]["VER"] = projectCodeInfo.ver;
}
}
catch (Exception ex)
{
logger.Fatal("URI" + " > " + System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message);
}
finally
{
msdal.Disconnect(connection);
}
return uriInfo;
}
}
}

View File

@@ -0,0 +1,92 @@
using System.Data;
using System.Data.SqlTypes;
using KART_API.Utils;
namespace KART_API.Services
{
public class UserService
{
static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(UserService));
/// <summary>
/// Login
/// </summary>
/// <param name="userEmail">ID</param>
/// <param name="userPW">PW</param>
/// <returns>User 정보</returns>
public DataTable userLogin(string userEmail, string userPW)
{
MSDAL msdal = new MSDAL();
var connection = msdal.ConnectIntra();
string sqlString = " select p_no, p_user_mail as mail, p_name as member_name, p_depart_now + p_team_now as team, '' as member_redirect ";
sqlString += " , CASE WHEN p_depart_now + p_team_now='M101' AND p_job_now = '02' THEN 1 ELSE 2 END as is_kri ";
sqlString += " from project2.dbo.V_PERSON ";
sqlString += " where p_resign = '0' and p_pass_new is not null ";
sqlString += " and (p_user_mail = '" + userEmail + "' or p_user_mail = '" + userEmail + "@kric.com') and p_pass_new COLLATE Korean_Wansung_CS_AS = '" + userPW + "'";
DataTable resultTable = msdal.GetDataTable(sqlString, connection);
msdal.Disconnect(connection);
if (resultTable.Rows.Count < 1)
{
connection = msdal.ConnectReport();
sqlString = " SELECT 99999999 as p_no, member_mail as mail, member_name as member_name, 'CLIENT' as team, member_redirect, is_kri ";
sqlString += " FROM REPORT_MEMBER ";
sqlString += " where is_use = 1 and member_mail = '" + userEmail + "' and member_pw = '" + userPW + "' ";
resultTable = msdal.GetDataTable(sqlString, connection);
msdal.Disconnect(connection);
}
return resultTable;
}
public static string userNumber(string userEmail)
{
string userNumber = "";
if (Common.IsNumeric(userEmail))
{
userNumber = userEmail;
}
else
{
MSDAL msdal = new MSDAL();
var connection = msdal.ConnectIntra();
string sqlString = " select p_no ";
sqlString += " from project2.dbo.V_PERSON ";
sqlString += " where p_resign = '0' and p_pass_new is not null ";
sqlString += " and (p_user_mail = '" + userEmail + "' or p_user_mail = '" + userEmail + "@kric.com')";
DataTable resultTable = msdal.GetDataTable(sqlString, connection);
msdal.Disconnect(connection);
if (resultTable.Rows.Count > 0)
{
userNumber = resultTable.Rows[0][0].ToString();
}
}
return userNumber;
}
public static string getDinIDs()
{
string returnString = string.Empty;
MSDAL msdal = new MSDAL();
var connection = msdal.ConnectIntra();
DataTable resultTable = msdal.GetDataTable("SELECT STRING_AGG(''''+p_user_mail+'''',',') as DinIDs FROM [project2].[dbo].[person] WHERE p_depart_now='S' and p_team_now in ('101','201')", connection);
msdal.Disconnect(connection);
if(resultTable.Rows.Count > 0)
{
returnString = resultTable.Rows[0][0].ToString();
}
return returnString;
}
}
}