Use scws Chinese Word Segmentation in Asp.net/C #

Source: Internet
Author: User

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}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.