/
ToDb.cs
279 lines (242 loc) · 13.7 KB
/
ToDb.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using System.Data.SqlClient;
using Latino;
namespace SemanticAnotation
{
public class ToDb
{
const string ClDocument = @"INSERT INTO document(title, date, pub_date, time_get, response_url, url_key, domain_name, is_financial, pump_dump_index, document_guid )
VALUES ( @title, @date, @pubDate, @timeGet, @responseUrl, @urlKey, @domainName, @isFinancial, @pumpDumpIndex, @documentGuid );
SELECT SCOPE_IDENTITY();";
const string ClOccurrence = @"INSERT INTO occurrence(date, location_start, location_end, sentence_num, block_num, document_id, entity_id)
VALUES (@date,@locationStart,@locationEnd,@sentenceNum,@blockNum,@document_id,(SELECT id from entity where entity_uri=@entityUri));
SELECT SCOPE_IDENTITY();";
const string ClSentimentWordOccurrence = @"INSERT INTO sentiment_word_occurrence(date, location_start, location_end, sentence_num, block_num, document_id, entity_id)
VALUES (@date,@locationStart,@locationEnd,@sentenceNum,@blockNum,@document_id,(SELECT id from entity where entity_uri=@entityUri));
SELECT SCOPE_IDENTITY();";
const string ClDocumentSentiment = @"INSERT INTO document_sentiment(document_id, positives, negatives, polarity, tokens)
VALUES (@document_id, @positives, @negatives, @polarity, @tokens);";
const string ClBlockSentiment = @"INSERT INTO block_sentiment(document_id, block_num, positives, negatives, polarity, tokens)
VALUES (@document_id, @block_id, @positives, @negatives, @polarity, @tokens);";
const string ClSentenceSentiment = @"INSERT INTO sentence_sentiment(document_id, block_num, sentence_num, positives, negatives, polarity, tokens)
VALUES (@document_id, @block_id, @sentence_id, @positives, @negatives, @polarity, @tokens);";
const string ClTerm = @"INSERT INTO term(occurrence_id, term)
VALUES (@occurrence_id, @term);";
/// <summary>
/// Inserts the data about the document document table in the semantics database.
/// </summary>
public static long DocumentToDb(SqlConnection connection, string title, string date, string pubDate, string timeGet, string responseUrl, string urlKey, string domainName, bool isFinancial, double pumpDumpIndex, Guid documentGuid)
{
using (SqlCommand cmd = new SqlCommand(null, connection))
{
cmd.CommandTimeout = 300;
cmd.CommandText = ClDocument;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@title", Shorten(title, 1000));
cmd.Parameters.AddWithValue("@date", date);
cmd.Parameters.AddWithValue("@pubDate", Shorten(pubDate, 100));
cmd.Parameters.AddWithValue("@timeGet", timeGet);
cmd.Parameters.AddWithValue("@responseUrl", Shorten(responseUrl, 1000));
cmd.Parameters.AddWithValue("@urlKey", Shorten(urlKey, 1000));
cmd.Parameters.AddWithValue("@domainName", Shorten(domainName, 255));
cmd.Parameters.AddWithValue("@isFinancial", isFinancial);
cmd.Parameters.AddWithValue("@pumpDumpIndex", pumpDumpIndex);
cmd.Parameters.AddWithValue("@documentGuid", documentGuid);
try
{
long id = Decimal.ToInt32((Decimal)cmd.ExecuteScalarRetryOnDeadlock()); //Execute the command, get id of the inserted document
return id;
}
catch (SqlException ex)
{
string errorMessage = String.Format("\nError inserting document: \r\nThe document: \n\t\tTitle:{0} \n\t\tDate:{1} \n\t\tUrlKey:{2}", title, date, urlKey);
throw new Exception(ex + errorMessage);
}
}
}
public static long OccurrenceToDb(SqlConnection connection, string date, int locationStart, int locationEnd, int sentenceNum, int blockNum, long document_id, string entityUri)
{
using (SqlCommand cmd = new SqlCommand(null, connection))
{
cmd.CommandTimeout = 300;
cmd.CommandText = ClOccurrence;
// Console.WriteLine("\n -------------------"+entityUri);
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@date", date);
cmd.Parameters.AddWithValue("@locationStart", locationStart);
cmd.Parameters.AddWithValue("@locationEnd", locationEnd);
cmd.Parameters.AddWithValue("@sentenceNum", sentenceNum);
cmd.Parameters.AddWithValue("@blockNum", blockNum);
cmd.Parameters.AddWithValue("@document_id", document_id);
cmd.Parameters.AddWithValue("@entityUri", entityUri);
try
{
Decimal tmp = (Decimal)cmd.ExecuteScalarRetryOnDeadlock();
long id = Decimal.ToInt32(tmp); //Execute the command, get id of the inserted document
return id;
}
catch (Exception ex)
{
string errorMessage = String.Format("\nError inserting occurrence: \n\t\t DocumentId:{0} \n\t\tEntityUri:{1}", document_id, entityUri);
throw new Exception(ex + errorMessage);
}
}
}
public static long SentimentWordOccurrenceToDb(SqlConnection connection, string date, int locationStart, int locationEnd, int sentenceNum, int blockNum, long document_id, string entityUri)
{
using (SqlCommand cmd = new SqlCommand(null, connection))
{
cmd.CommandTimeout = 300;
cmd.CommandText = ClSentimentWordOccurrence;
// Console.WriteLine("\n -------------------"+entityUri);
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@date", date);
cmd.Parameters.AddWithValue("@locationStart", locationStart);
cmd.Parameters.AddWithValue("@locationEnd", locationEnd);
cmd.Parameters.AddWithValue("@sentenceNum", sentenceNum);
cmd.Parameters.AddWithValue("@blockNum", blockNum);
cmd.Parameters.AddWithValue("@document_id", document_id);
cmd.Parameters.AddWithValue("@entityUri", entityUri);
try
{
Decimal tmp = (Decimal)cmd.ExecuteScalarRetryOnDeadlock();
long id = Decimal.ToInt32(tmp); //Execute the command, get id of the inserted document
return id;
}
catch (Exception ex)
{
Console.WriteLine("\nError inserting sentiment word occurrence " + entityUri + "\n" + ex);
return 0;
}
}
}
/// <summary>
/// Inserts the EntityOccurrence data into the Occurrence table in the Semantics database.
/// </summary>
public static void DocumentSentimentToDb(SqlConnection connection, long docId, int positives, int negatives, int noTokens)
{
using (SqlCommand cmd = new SqlCommand(null, connection))
{
cmd.CommandTimeout = 300;
cmd.CommandText = ClDocumentSentiment;
double polarity = 0;
if (positives != 0 || negatives != 0)
{
polarity = 1.0 * (positives - negatives) / (positives + negatives);
}
// Console.WriteLine("\n -------------------"+entityUri);
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@document_id", docId);
cmd.Parameters.AddWithValue("@positives", positives);
cmd.Parameters.AddWithValue("@negatives", negatives);
cmd.Parameters.AddWithValue("@polarity", polarity);
cmd.Parameters.AddWithValue("@tokens", noTokens);
try
{
cmd.ExecuteNonQueryRetryOnDeadlock(); //Execute the command
}
catch (Exception ex)
{
string errorMessage = String.Format("\nError inserting document sentiment: \n\t\t DocumentId:{0}", docId);
throw new Exception(ex + errorMessage);
}
}
}
public static void BlockSentimentToDb(SqlConnection connection, long docId, short blockNum, int positives, int negatives, int noTokens)
{
using (SqlCommand cmd = new SqlCommand(null, connection))
{
cmd.CommandTimeout = 300;
cmd.CommandText = ClBlockSentiment;
double polarity = 1.0 * (positives - negatives) / (positives + negatives);
// Console.WriteLine("\n -------------------"+entityUri);
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@document_id", docId);
cmd.Parameters.AddWithValue("@block_id", blockNum);
cmd.Parameters.AddWithValue("@positives", positives);
cmd.Parameters.AddWithValue("@negatives", negatives);
cmd.Parameters.AddWithValue("@polarity", polarity);
cmd.Parameters.AddWithValue("@tokens", noTokens);
try
{
cmd.ExecuteNonQueryRetryOnDeadlock(); //Execute the command
}
catch (Exception ex)
{
string errorMessage = String.Format("\nError inserting block sentiment: \n\t\t DocumentId:{0} \n\t\t BlockNum{1}", docId, blockNum);
throw new Exception(ex + errorMessage);
}
}
}
public static void SentenceSentimentToDb(SqlConnection connection, long docId, short blockNum, short sentenceNum, int positives, int negatives, int noTokens)
{
using (SqlCommand cmd = new SqlCommand(null, connection))
{
cmd.CommandTimeout = 300;
cmd.CommandText = ClSentenceSentiment;
double polarity = 1.0 * (positives - negatives) / (positives + negatives);
// Console.WriteLine("\n -------------------"+entityUri);
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@document_id", docId);
cmd.Parameters.AddWithValue("@block_id", blockNum);
cmd.Parameters.AddWithValue("@sentence_id", sentenceNum);
cmd.Parameters.AddWithValue("@positives", positives);
cmd.Parameters.AddWithValue("@negatives", negatives);
cmd.Parameters.AddWithValue("@polarity", polarity);
cmd.Parameters.AddWithValue("@tokens", noTokens);
cmd.ExecuteNonQueryRetryOnDeadlock(); //Execute the command
}
}
/// <summary>
/// Inserts the data about the actual term (string) from teh document
/// </summary>
public static void TermToDb(SqlConnection connection, long occurrenceId, string term)
{
using (SqlCommand cmd = new SqlCommand(null, connection))
{
cmd.CommandTimeout = 300;
cmd.CommandText = ClTerm;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@occurrence_id", occurrenceId);
cmd.Parameters.AddWithValue("@term", Shorten(term, 400));
try
{
cmd.ExecuteNonQueryRetryOnDeadlock(); //Execute the command
}
catch (Exception)
{
Console.WriteLine("\nTerm insert exception : occurrence_id \t" + occurrenceId + " \t" + term);
}
}
}
//*****************************************************************************************************************************
//*****************************************************************************************************************************
//***************************************** SELECT *
/// <summary>
/// Selects * from the table tableName
/// </summary>
public static void PrintSelectAll(SqlConnection connect, string tableName)
{
SqlCommand command = connect.CreateCommand();
command.CommandText = "SELECT * from "+tableName;
Console.WriteLine("\nSELECT * FROM {0}: \n",tableName);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string thisrow = "";
for (int i = 0; i < reader.FieldCount; i++)
thisrow += reader.GetValue(i).ToString() + ",";
Console.WriteLine(thisrow);
}
}
//******************************************* shorten string to desired number of characters
private static string Shorten(string value, int length)
{
if (value.Length > length)
{
return value.Substring(0, length - 5) + "...";
}
return value;
}
}
}