Source code for determining the rise and fall and extracting the stock name

Source: Internet
Author: User

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Net;
Using System. IO;
Using System. Xml;

/* Title: Call the Sina word segmentation service Algorithm
* Author: Jia haogao
* Time:
* Usage: Set sentence to the text to be segmented. Then run the command.
* Return value structure: obtains the location and part-of-speech id of each token.
* Definition: "location division" indicates the position at the end of the token. For example, if the first token is "tomorrow", the location is 2, and the second is "yes ", then its location is 3.
**/
Namespace CSPostData
{
Class Program
{

Class token {
Int position; // divide the position
Int id; // part of speech
String lexicon; // words

Public int getPos (){
Return position;
}
Public string getLexicon (){
Return lexicon;
}
Public token (int p, int I, string le ){
Position = p; id = I; lexicon = le;
}

}
Static void Main (string [] args)
{

Encoding encoding = Encoding. GetEncoding ("UTF-8 ");
Console. WriteLine ("The Stock database is being constructed ...... ");
List <string> stockNames = new List <string> ();
List <string> stockIDs = new List <string> ();
StreamReader ssr = new StreamReader ("stocks.txt", encoding );
String tuple;
While (tuple = ssr. ReadLine ())! = Null)
{
Int stat = 0;
String sname = "", sid = "";
Foreach (char ch in tuple)
{
If (ch = '(')
{
Stat = 1;
Continue;
}
Else if (ch = ')')
{
Stat = 0;
Continue;
}
Switch (stat)
{
Case 0:
Sname + = ch;
Break;
Case 1:
Sid + = ch;
Break;
}
}
StockNames. Add (sname );
StockIDs. Add (sid );
}
Ssr. Close ();
Console. WriteLine ("successful! ");

// Word the article and determine the rise/fall and the name of the stock to be selected
Int fid = 4398;
For (; fid <= 6108; fid ++ ){
StreamReader fsr = new StreamReader ("data \" + fid + ". txt", encoding); // The document to be segmented
Console. Write ("Please wait patiently. The text is being segmented... \ n ");

// Read the string
String sentence = fsr. ReadToEnd ();
Fsr. Close ();

Stream outstream = null;
Stream instream = null;
StreamReader sr = null;
String url = "http://1.caunion.sinaapp.com/a.php ";
HttpWebRequest request = null;
HttpWebResponse response = null;

// Prepare the request and set parameters
Request = WebRequest. Create (url) as HttpWebRequest;
Request. Method = "POST ";
Request. ContentType = "application/x-www-form-urlencoded ";

Byte [] data = encoding. GetBytes (url + "& sentence =" + sentence );
Request. ContentLength = data. Length;
Outstream = request. GetRequestStream ();
Outstream. Write (data, 0, data. Length );
Outstream. Flush ();
Outstream. Close ();
// Send the request and obtain the response data

Response = request. GetResponse () as HttpWebResponse;
// The Post request is not sent to the target webpage until the request. GetResponse () program starts.
Instream = response. GetResponseStream ();
Sr = new StreamReader (instream, encoding );
// Return result webpage (html) Code

String content = sr. ReadToEnd ();
Sr. Close ();

XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. LoadXml (content. Trim ());
XmlNodeList nodeList = xmlDoc. GetElementsByTagName ("w ");

List <token> tokens = new List <token> ();
For (int j = 0; j <nodeList. Count; j ++ ){
Token tokennow;
XmlNode node1 = nodeList. Item (j );
Int start1 = Convert. ToInt32 (node1.InnerText), attr1 = Convert. ToInt32 (node1.Attributes ["t"]. Value );
If (j> 0)
{
XmlNode node2 = nodeList. Item (j-1 );
Int start2 = Convert. ToInt32 (node2.InnerText), attr2 = Convert. ToInt32 (node2.Attributes ["t"]. Value );
String tem = "";
For (int I = start2; I <start1; I ++)
{
Tem + = sentence [I]; // construct the word segmentation result
}
Tokennow = new token (start1, attr1, tem );
Tokens. Add (tokennow );
}
Else {
String tem = "";
For (int I = 0; I <start1; I ++)
{
Tem + = sentence [I]; // construct the word segmentation result
}
Tokennow = new token (start1, attr1, tem );
Tokens. Add (tokennow );
}

}

Console. Write ("word segmentation result: \ n ");
// At this time, tokens stores the location, part-of-speech id, and word splitting result of each token.
Foreach (token tem in tokens)
{
Console. Write (tem. getLexicon () + "\ n ");
}
Console. Write ("\ n ");

// Judge the stock trend and extract the stock name
Random ro = new Random ();
Int iResult;
Int iUp = 2851;
IResult = ro. Next (iUp );
String s_name = stockNames [iResult];
Boolean finish = false;
Int up = 0, down = 0;
StreamReader fsrup = new StreamReader ("up.txt", encoding); // bullish words
StreamReader fsrdown = new StreamReader ("down.txt", encoding); // bearish words
// Create a linked list
List <string> upString = new List <string> (), downString = new List <string> ();
String temstr1 = fsrup. ReadLine ();
Do {
UpString. Add (temstr1 );
Temstr1 = fsrup. ReadLine ();
} While (temstr1! = Null );
Fsrup. Close ();

String temstr2 = fsrdown. ReadLine ();
Do {
DownString. Add (temstr2 );
Temstr2 = fsrdown. ReadLine ();
} While (temstr2! = Null );
Fsrdown. Close ();
Foreach (token tem in tokens ){
// Match the obtained words with the rising database
Foreach (string tems in upString ){
If (tem. getLexicon () = tems)
Up ++;
}
// Match the obtained words with the DROP DATABASE
Foreach (string tems in downString ){
If (tem. getLexicon () = tems)
Down ++;
}

// Match the obtained words with the Stock database
If (! Finish)
{
// Stock name
Foreach (string sn in stockNames)
{
If (tem. getLexicon () = sn)
{
S_name = tem. getLexicon ();
Finish = true;
Break;
}
}

// Stock id
Foreach (string sn in stockNames)
{
If (tem. getLexicon () = sn)
{
S_name = tem. getLexicon ();
Finish = true;
Break;
}
}
}
}

// Judge based on the score
String st_trend = "";
If (up> down)
{
Console. Write ("this stock forecast result is up \ n ");
St_trend = "up ";
}
Else if (up = down)
{
IResult = ro. Next ();
If (iResult % 2 = 0)
{
Console. Write ("this stock forecast result is up \ n ");
St_trend = "up ";
}
Else {
Console. Write ("this stock forecast result is down \ n ");
St_trend = "down ";
}

}
Else
{
Console. Write ("this stock forecast result is down \ n ");
St_trend = "down ";
}
Console. WriteLine (s_name );

// Write information to the file
StreamWriter swr = new StreamWriter ("data \" + fid + ". txt", true, encoding );
Swr. WriteLine (s_name );
Swr. WriteLine (st_trend );
Swr. Close ();
}
Console. WriteLine ("Operation completed! ");
Console. WriteLine ("quit? ");
Console. ReadKey ();
Return;
}

}
}

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.