@@ -10,6 +10,44 @@ namespace TensorFlowNET.Examples.CnnTextClassification
1010{
1111 public class DataHelpers
1212 {
13+ private const string TRAIN_PATH = "text_classification/dbpedia_csv/train.csv" ;
14+ private const string TEST_PATH = "text_classification/dbpedia_csv/test.csv" ;
15+
16+ public static ( int [ ] [ ] , int [ ] , int ) build_char_dataset ( string step , string model , int document_max_len )
17+ {
18+ string alphabet = "abcdefghijklmnopqrstuvwxyz0123456789-,;.!?:’'\" /|_#$%ˆ&*˜‘+=<>()[]{} " ;
19+ /*if (step == "train")
20+ df = pd.read_csv(TRAIN_PATH, names =["class", "title", "content"]);*/
21+ var char_dict = new Dictionary < string , int > ( ) ;
22+ char_dict [ "<pad>" ] = 0 ;
23+ char_dict [ "<unk>" ] = 1 ;
24+ foreach ( char c in alphabet )
25+ char_dict [ c . ToString ( ) ] = char_dict . Count ;
26+
27+ var contents = File . ReadAllLines ( TRAIN_PATH ) ;
28+
29+ var x = new int [ contents . Length ] [ ] ;
30+ var y = new int [ contents . Length ] ;
31+ for ( int i = 0 ; i < contents . Length ; i ++ )
32+ {
33+ string [ ] parts = contents [ i ] . ToLower ( ) . Split ( ",\" " ) . ToArray ( ) ;
34+ string content = parts [ 2 ] ;
35+ content = content . Substring ( 0 , content . Length - 1 ) ;
36+ x [ i ] = new int [ document_max_len ] ;
37+ for ( int j = 0 ; j < document_max_len ; j ++ )
38+ {
39+ if ( j >= content . Length )
40+ x [ i ] [ j ] = char_dict [ "<pad>" ] ;
41+ else
42+ x [ i ] [ j ] = char_dict . ContainsKey ( content [ j ] . ToString ( ) ) ? char_dict [ content [ j ] . ToString ( ) ] : char_dict [ "<unk>" ] ;
43+ }
44+
45+ y [ i ] = int . Parse ( parts [ 0 ] ) ;
46+ }
47+
48+ return ( x , y , alphabet . Length + 2 ) ;
49+ }
50+
1351 /// <summary>
1452 /// Loads MR polarity data from files, splits the data into words and generates labels.
1553 /// Returns split sentences and labels.
@@ -20,8 +58,8 @@ public class DataHelpers
2058 public static ( string [ ] , NDArray ) load_data_and_labels ( string positive_data_file , string negative_data_file )
2159 {
2260 Directory . CreateDirectory ( "CnnTextClassification" ) ;
23- Utility . Web . Download ( positive_data_file , "CnnTextClassification/rt -polarity.pos" ) ;
24- Utility . Web . Download ( negative_data_file , "CnnTextClassification/ rt-polarity.neg" ) ;
61+ Utility . Web . Download ( positive_data_file , "CnnTextClassification" , "rt -polarity.pos") ;
62+ Utility . Web . Download ( negative_data_file , "CnnTextClassification" , " rt-polarity.neg") ;
2563
2664 // Load data from files
2765 var positive_examples = File . ReadAllLines ( "CnnTextClassification/rt-polarity.pos" )
0 commit comments