Пример #1
0
        public string Label(string[] lines)
        {
            int i = 0;

            WapitiNative.gets_cb gets_cb = () => i >= lines.Length ? null : lines[i++];

            var sb = new StringBuilder();

            WapitiNative.write_cb write_cb = (ptr, length) =>
            {
                byte[] strbuf = new byte[length];
                Marshal.Copy(ptr, strbuf, 0, length);
                var data = Encoding.UTF8.GetString(strbuf);

                WapitiNative.xfree(ptr);
                sb.Append(data);
            };

            var iol = WapitiNative.iol_new_interop(gets_cb, write_cb);

            WapitiNative.tag_label(this.Model, iol);
            WapitiNative.iol_free_interop(iol);

            return(sb.ToString());
        }
Пример #2
0
        public static WapitiModel Load(string[] lines)
        {
            int i = 0;

            WapitiNative.gets_cb gets_cb = () => i >= lines.Length ? null : lines[i++];

            var iol = WapitiNative.iol_new_interop(gets_cb, null);
            var rdr = WapitiNative.rdr_new(iol, true);
            var mdl = WapitiNative.mdl_new(rdr);

            WapitiNative.mdl_load(mdl);
            return(new WapitiModel(mdl));
        }
Пример #3
0
        public string Train(Opt trainOpt, string[] modelLines, string[] patternLines)
        {
            int model_idx = 0;

            WapitiNative.gets_cb model_gets_cb = () => model_idx >= modelLines.Length ? null : modelLines[model_idx++];

            int pattern_idx = 0;

            WapitiNative.gets_cb pattern_gets_cb = () => pattern_idx >= patternLines.Length ? null : patternLines[pattern_idx++];

            var sb = new StringBuilder();

            WapitiNative.write_cb write_cb = (ptr, length) =>
            {
                byte[] strbuf = new byte[length];
                Marshal.Copy(ptr, strbuf, 0, length);
                var data = Encoding.UTF8.GetString(strbuf);

                WapitiNative.xfree(ptr);
                sb.Append(data);
            };

            var iol_model   = WapitiNative.iol_new_interop(model_gets_cb, write_cb);
            var iol_pattern = WapitiNative.iol_new_interop(pattern_gets_cb, null);

            var optPtr = Marshal.AllocHGlobal(Marshal.SizeOf <Opt>());

            Marshal.StructureToPtr(trainOpt, optPtr, false);

            int result = WapitiNative.train(optPtr, iol_model, iol_pattern);

            if (result != 0)
            {
                throw new Exception($"training failed :: {result}");
            }

            Marshal.FreeHGlobal(optPtr);
            WapitiNative.iol_free_interop(iol_model);
            WapitiNative.iol_free_interop(iol_pattern);

            return(sb.ToString());
        }