判斷漲跌以及抽取股票名稱的原始碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
/* 標題:呼叫新浪分詞服務演算法
* 作者:賈道遠
* 時間:2012/4/13
* 用法:設定sentence為需要分詞的文章.然後執行即可
* 返回值結構: 得到每個token的劃分位置和詞性id.
* 定義:"劃分位置"意思是,這個token末尾的位置,比如第一個token是"明天",則劃分位置是2,接著第二個是"是",則它的劃分位置是3
* */
namespace CSPostData
{
class Program
{
class token {
int position; //劃分位置
int id;//詞性
string lexicon;//分得的詞語
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("股票資料庫構造中……");
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("成功!");
//對文章分詞並且判斷漲跌以及抽選股票名稱
int fid = 4398;
for(;fid<=6108;fid++){
StreamReader fsr = new StreamReader("data\\"+fid+".txt", encoding);//要分詞的文章
Console.Write("請您耐心等待,文章分詞中.......\n");
//讀取字串
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;
// 準備請求,設定引數
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();
//傳送請求並獲取相應回應資料
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程式才開始向目標網頁傳送Post請求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回結果網頁(html)程式碼
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];//構造分詞結果
}
tokennow = new token(start1, attr1, tem);
tokens.Add(tokennow);
}
else {
string tem = "";
for (int i = 0; i < start1; i++)
{
tem += sentence[i];//構造分詞結果
}
tokennow = new token(start1, attr1, tem);
tokens.Add(tokennow);
}
}
Console.Write("分詞結果為:\n\n\n");
//此時tokens儲存每個token的位置和詞性id以及分詞的結果
foreach (token tem in tokens)
{
Console.Write(tem.getLexicon()+"\n");
}
Console.Write("\n\n\n");
//下面對股票走勢進行判斷,並且提取股票名稱
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);//看漲詞語
StreamReader fsrdown = new StreamReader("down.txt", encoding);//看跌詞語
//構造連結串列
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) {
//將分得的詞和漲資料庫做匹配
foreach(string tems in upString){
if(tem.getLexicon() == tems)
up++;
}
//將分得的詞和跌資料庫做匹配
foreach(string tems in downString){
if(tem.getLexicon() == tems)
down++;
}
//將分得的詞和股票資料庫做匹配
if (!finish)
{
//股票名稱
foreach (string sn in stockNames)
{
if (tem.getLexicon() == sn)
{
s_name = tem.getLexicon();
finish = true;
break;
}
}
//股票id
foreach (string sn in stockNames)
{
if (tem.getLexicon() == sn)
{
s_name = tem.getLexicon();
finish = true;
break;
}
}
}
}
//根據打分情況進行判斷
string st_trend = "";
if (up > down)
{
Console.Write("本個股預測結果為 漲\n");
st_trend = "漲";
}
else if (up == down)
{
iResult = ro.Next();
if (iResult % 2 == 0)
{
Console.Write("本個股預測結果為 漲\n");
st_trend = "漲";
}
else {
Console.Write("本個股預測結果為 跌\n");
st_trend = "跌";
}
}
else
{
Console.Write("本個股預測結果為 跌\n");
st_trend = "跌";
}
Console.WriteLine(s_name);
//將資訊寫入檔案中
StreamWriter swr = new StreamWriter("data\\"+fid+".txt",true,encoding );
swr.WriteLine(s_name);
swr.WriteLine(st_trend);
swr.Close();
}
Console.WriteLine("操作完成!");
Console.WriteLine("退出?");
Console.ReadKey();
return ;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
/* 標題:呼叫新浪分詞服務演算法
* 作者:賈道遠
* 時間:2012/4/13
* 用法:設定sentence為需要分詞的文章.然後執行即可
* 返回值結構: 得到每個token的劃分位置和詞性id.
* 定義:"劃分位置"意思是,這個token末尾的位置,比如第一個token是"明天",則劃分位置是2,接著第二個是"是",則它的劃分位置是3
* */
namespace CSPostData
{
class Program
{
class token {
int position; //劃分位置
int id;//詞性
string lexicon;//分得的詞語
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("股票資料庫構造中……");
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("成功!");
//對文章分詞並且判斷漲跌以及抽選股票名稱
int fid = 4398;
for(;fid<=6108;fid++){
StreamReader fsr = new StreamReader("data\\"+fid+".txt", encoding);//要分詞的文章
Console.Write("請您耐心等待,文章分詞中.......\n");
//讀取字串
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;
// 準備請求,設定引數
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();
//傳送請求並獲取相應回應資料
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程式才開始向目標網頁傳送Post請求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回結果網頁(html)程式碼
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];//構造分詞結果
}
tokennow = new token(start1, attr1, tem);
tokens.Add(tokennow);
}
else {
string tem = "";
for (int i = 0; i < start1; i++)
{
tem += sentence[i];//構造分詞結果
}
tokennow = new token(start1, attr1, tem);
tokens.Add(tokennow);
}
}
Console.Write("分詞結果為:\n\n\n");
//此時tokens儲存每個token的位置和詞性id以及分詞的結果
foreach (token tem in tokens)
{
Console.Write(tem.getLexicon()+"\n");
}
Console.Write("\n\n\n");
//下面對股票走勢進行判斷,並且提取股票名稱
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);//看漲詞語
StreamReader fsrdown = new StreamReader("down.txt", encoding);//看跌詞語
//構造連結串列
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) {
//將分得的詞和漲資料庫做匹配
foreach(string tems in upString){
if(tem.getLexicon() == tems)
up++;
}
//將分得的詞和跌資料庫做匹配
foreach(string tems in downString){
if(tem.getLexicon() == tems)
down++;
}
//將分得的詞和股票資料庫做匹配
if (!finish)
{
//股票名稱
foreach (string sn in stockNames)
{
if (tem.getLexicon() == sn)
{
s_name = tem.getLexicon();
finish = true;
break;
}
}
//股票id
foreach (string sn in stockNames)
{
if (tem.getLexicon() == sn)
{
s_name = tem.getLexicon();
finish = true;
break;
}
}
}
}
//根據打分情況進行判斷
string st_trend = "";
if (up > down)
{
Console.Write("本個股預測結果為 漲\n");
st_trend = "漲";
}
else if (up == down)
{
iResult = ro.Next();
if (iResult % 2 == 0)
{
Console.Write("本個股預測結果為 漲\n");
st_trend = "漲";
}
else {
Console.Write("本個股預測結果為 跌\n");
st_trend = "跌";
}
}
else
{
Console.Write("本個股預測結果為 跌\n");
st_trend = "跌";
}
Console.WriteLine(s_name);
//將資訊寫入檔案中
StreamWriter swr = new StreamWriter("data\\"+fid+".txt",true,encoding );
swr.WriteLine(s_name);
swr.WriteLine(st_trend);
swr.Close();
}
Console.WriteLine("操作完成!");
Console.WriteLine("退出?");
Console.ReadKey();
return ;
}
}
}
相關文章
- 仿支付寶股票 猜漲跌ViewView
- SQL SERVER 使用者名稱、密碼登入判斷SQLServer密碼
- 通達信漲跌動因指標公式原始碼附圖指標公式原始碼
- 註冊使用者名稱字元長度判斷細節字元
- 【java web】--Ajax非同步判斷使用者名稱是否存在JavaWeb非同步
- 利用CNN對股票「圖片」進行漲跌分類——一次嘗試CNN
- MySQL判斷表名是否存在MySql
- 如何判斷網校原始碼是否值得使用?原始碼
- Java反射獲取位元組碼以及判斷型別Java反射型別
- 登入判斷使用者名稱和密碼是否正確的程式碼(連結和讀取資料庫)密碼資料庫
- arm 中斷配置以及處理的原始碼分析原始碼
- 儲存過程判斷若個表中是否存在某個名稱的欄位儲存過程
- asp.net判斷遊覽器資訊(遊覽器名稱,版本號等)ASP.NET
- 【炒股技術】股票追漲抓漲停的操作技巧
- js如何判斷指定名稱的函式是否存在JS函式
- 僅用一句SQL更新整張表的漲跌幅、漲跌率SQL
- 不學無數——Mybatis解析判斷表示式原始碼分析MyBatis原始碼
- Android判斷當前棧頂Activity的包名Android
- 判斷一個類名或者元素是否存在的思路
- (IOS)判斷密碼格式iOS密碼
- 語種名稱程式碼
- JS的判斷語句:判斷、迴圈JS
- 原始碼防洩密系統與程式相關性判斷原始碼
- QRowTable表格控制元件(二)-紅漲綠跌控制元件
- 常見物件-判斷一個字串是否對稱案例物件字串
- 【JavaScript 學以致用】值的判斷以及型別轉換JavaScript型別
- 註冊介面從資料庫中判斷使用者名稱是否存在(jsp+servlet+sqlserver)資料庫JSServletSQLServer
- 日期判斷行修改指令碼指令碼
- js函式中的if判斷和a==b判斷JS函式
- 使用帶型別判斷的比較判斷型別
- 高質量的帶貨直播原始碼應該從這四點判斷原始碼
- 直播app原始碼,輸入密碼和使用者名稱呼叫開發者工具APP原始碼密碼
- 直播軟體原始碼,利用uniapp checkbox判斷是否選中原始碼APP
- 影片直播原始碼,uniapp checkbox 怎麼判斷是否選中原始碼APP
- 【C++】判斷一顆二叉樹是否對稱C++二叉樹
- A股週期性行業的股票&A股壟斷(稀缺資源)的股票行業
- 判斷螢幕旋轉的事件程式碼事件
- 微軟SQLServer密碼管理的危險判斷微軟SQLServer密碼