示例#1
0
        static void Main(string[] args)
        {
            // Establish the remote endpoint for the socket.
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);

            sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sock.Bind(ipep);
            sock.Listen(0);
            while (true)
            {
                sock2 = sock.Accept();
                //read requset from socket
                byte[] msg   = new byte[1024];
                int    recv  = sock2.Receive(msg);
                string input = Encoding.ASCII.GetString(msg, 0, recv);
                Console.Write("get : {0}", input);
                Req req = JsonConvert.DeserializeObject <Req>(input);
                //initalized paramters - should be data from client
                string        malletPath       = @"c:\mallet";
                string        dataPath         = req.path;
                string        resultMalletFile = "keys.mallet";
                string        resultTxtFile    = "keys.txt";
                string        DataOfFile       = "topics.txt";
                int           numTopics        = req.topics;
                List <string> flags            = req.flags;
                //begin - extract zip files
                dataPath = ExtractFolder(dataPath);
                Thread t = null;
                // if ruj the parser
                if (req.tagger == 1)
                {
                    sock2.Close();
                    t = new Thread(new ThreadStart(ThreadExample.ThreadProc));
                    t.Start();
                    toknizer(dataPath);
                    System.Threading.Thread.Sleep(30000);
                    parser2(dataPath);
                }
                IExternal cmd    = new CmdWindows(malletPath);
                MalletOpr mallet = new MalletOpr(cmd);

                //run - crate mallet file
                mallet.CreateMalletFile(dataPath, resultMalletFile, flags);
                System.Threading.Thread.Sleep(10000);
                //run LDA
                mallet.RunTopics(numTopics, resultMalletFile, resultTxtFile, DataOfFile);
                System.Threading.Thread.Sleep(88000);
                //convert results to Json
                List <Topic> getTopics  = mallet.getTopics();
                string       jsonString = JsonConvert.SerializeObject(getTopics);
                byte[]       msg1       = Encoding.UTF8.GetBytes(jsonString);
                //write to socket
                if (t != null)
                {
                    t.Abort();
                    System.Threading.Thread.Sleep(8000);
                    sock2 = sock.Accept();
                }
                int bytes = WriteToClient(msg1, sock2);
                // Release the socket.
                sock2.Shutdown(SocketShutdown.Both);
                sock2.Close();
                sock2 = sock.Accept();
                List <File> files       = mallet.getTopicsForFiles();
                string      jsonString2 = JsonConvert.SerializeObject(files);
                byte[]      msg2        = Encoding.UTF8.GetBytes(jsonString2);
                int         bytes2      = WriteToClient(msg2, sock2);
                sock2.Close();
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            // Establish the remote endpoint for the socket.
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);
            Socket     sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            sock.Bind(ipep);
            sock.Listen(0);
            Socket sock2 = sock.Accept();

            //byte[] msg1 = null;
            //  byte[] msg2=null;
            //    String jsonString = "";
            //      String jsonString2 = "";
            //        while (true)
            //        {

            //read requset from socket
            byte[] msg   = new byte[1024];
            int    recv  = sock2.Receive(msg);
            string input = Encoding.ASCII.GetString(msg, 0, recv);

            Console.Write("get : {0}", input);
            //  if (!(input.Equals("more_details")))
            // {
            Req req = JsonConvert.DeserializeObject <Req>(input);
            //initalized paramters - should be data from client
            string        malletPath       = @"c:\mallet";
            string        dataPath         = req.path; //@"C:\mallet\sample-data\web\news.rar";
            string        resultMalletFile = "keys.mallet";
            string        resultTxtFile    = "keys.txt";
            string        DataOfFile       = "topics.txt";
            int           numTopics        = req.topics; //  10;
            List <string> flags            = req.flags;  // new List<string>();

            //flags.Add("keep-sequence");
            //flags.Add("remove-stopwords");
            //flags.Add("stoplist-file heb.txt");

            //begin
            dataPath = ExtractFolder(dataPath);
            IExternal cmd    = new CmdWindows(malletPath);
            MalletOpr mallet = new MalletOpr(cmd);

            //run
            mallet.CreateMalletFile(dataPath, resultMalletFile, flags);
            System.Threading.Thread.Sleep(30000);
            mallet.RunTopics(numTopics, resultMalletFile, resultTxtFile, DataOfFile);
            System.Threading.Thread.Sleep(60000);

            //convert to Json
            List <Topic> getTopics = mallet.getTopics();
            List <File>  files     = mallet.getTopicsForFiles();

            string jsonString  = JsonConvert.SerializeObject(getTopics);
            string jsonString2 = JsonConvert.SerializeObject(files);


            byte[] msg1 = Encoding.ASCII.GetBytes(jsonString);
            byte[] msg2 = Encoding.ASCII.GetBytes(jsonString2);

            //    int bytes = WriteToClient(msg1, sock2);
            //      Console.Write("send msg1: {0}  length is : {1}", jsonString, bytes);
            //    System.Threading.Thread.Sleep(10000);

            //}
            //  else
            // {
            //byte[] msg3 = new byte[1024];
            //int recv2 = sock2.Receive(msg3);
            //int bytes2 = WriteToClient(msg2, sock2);
            //Console.Write("send msg2 : {0} length is :{1} ", jsonString2, bytes2);
            //}

            //  List<String> data = new List<string>();
            //  data.Add(jsonString);
            // data.Add(jsonString2);
            Res data  = new Res(jsonString);
            Res data2 = new Res(jsonString2);

            String jsonData1 = JsonConvert.SerializeObject(data);
            String jsonData2 = JsonConvert.SerializeObject(data2);


            Res2   data3    = new Res2(jsonData2, jsonData1);
            String jsonData = JsonConvert.SerializeObject(data3);


            //write to socket
            //     byte[] msg5 = Encoding.ASCII.GetBytes(jsonData);
            byte[] msg6   = Encoding.ASCII.GetBytes(jsonData);
            int    bytes  = WriteToClient(msg6, sock2);
            int    bytes2 = WriteToClient(null, sock2);

            Console.Write("send : {0}", jsonData);
            Console.ReadKey();



            // Release the socket.
            //sender.Shutdown(SocketShutdown.Both);
            //sender.Close();
            //}
        }