Chinese Word Segmentation is too troublesome, and some Chinese Word Segmentation components are also good, but I still feel troublesome to maintain the dictionary myself. I tried to use scws Chinese word segmentation to directly call his API, I don't need to do anything. Thank you for that.
If you don't talk much about it, just serve it directly.
1 /// <summary>
2 // use scws for Chinese Word Segmentation
3 // 1638988@gmail.com
4 /// </Summary>
5 /// <Param name = "str"> string to be segmented </param>
6 /// <returns> result of Word Segmentation separated by spaces </returns>
7 public static string segment (string Str)
8 {
9 system. Text. stringbuilder sb = new system. Text. stringbuilder ();
10 try
11 {
12 string S = string. empty;
13 system. net. cookiecontainer = new system. net. cookiecontainer ();
14 // convert the submitted string data to a byte array
15 byte [] postdata = system. text. encoding. ASCII. getbytes ("Data =" + system. web. httputility. urlencode (STR) + "& respond = JSON & charset = utf8 & ignore = Yes & duality = No & traditional = No & multi = 0 ");
16
17 // set related parameters for submission
18 system. net. httpwebrequest request = system. net. webrequest. Create ("http://www.ftphp.com/scws/api.php") as system. net. httpwebrequest;
19 request. method = "Post ";
20 request. keepalive = false;
21 request. contenttype = "application/X-WWW-form-urlencoded ";
22 request. cookiecontainer = cookiecontainer;
23 request. contentlength = postdata. length;
24
25 // submit request data
26 system. Io. Stream outputstream = request. getrequeststream ();
27 outputstream. Write (postdata, 0, postdata. Length );
28 outputstream. Close ();
29
30 // receive the returned page
31 system. net. httpwebresponse response = request. getresponse () as system. net. httpwebresponse;
32 system. Io. Stream responsestream = response. getresponsestream ();
33 system. Io. streamreader reader = new system. Io. streamreader (responsestream, system. Text. encoding. getencoding ("UTF-8 "));
34 string val = reader. readtoend ();
35
36 newtonsoft. JSON. LINQ. jobject Results = newtonsoft. JSON. LINQ. jobject. parse (VAL );
37 foreach (VAR item in results ["Words"]. Children ())
38 {
39 newtonsoft. JSON. LINQ. jobject word = newtonsoft. JSON. LINQ. jobject. parse (item. tostring ());
40 sb. append (word ["word"]. tostring () + "");
41}
42}
43 catch
44 {
45}
46
47 return sb. tostring ();
48}