Boolean Rule Action Set
Deriving Boolean Rules from a Term-by-Document Frequency Matrix
This section contains PROC CAS code.
Note: Input data must be accessible in your CAS session, either as a CAS table or as a transient-scope table. A CAS table has a two-level name: the first level is your CAS engine libref, and the second level is the table name. You refer to this table in the CAS procedure by specifying only the second level. For more information about two-level names, see Chapter 2, Shared Concepts (SAS Viya: Machine Learning Procedures). A transient-scope table is called directly from the action and exists in memory for the duration of the action. For more information about accessing data, see SAS Viya: System Programming Guide. For more information about PROC CAS and programming in CASL, see SAS Cloud Analytic Services: CASL Programmer’s Guide and SAS Cloud Analytic Services: CASL Reference.
This example first executes the tmMine action to generate a term-by-document matrix for training data and uses the brTrain action to extract Boolean rules. Then it executes the tmScore action to generate a term-by-document matrix for testing data and uses the score action to match the extracted rules.
The following DATA step creates the data table mycas.reviews. This DATA step assumes that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.
data mycas.reviews;
infile datalines delimiter='|' missover;
length text $300 category $20;
input text$ positive category$ did;
datalines;
This is the greatest phone ever! love it!|1|electronics|1
The phone's battery life is too short and screen resolution is low.|0|electronics|2
The screen resolution is low, but I love this tv.|1|electronics|3
The movie itself is great and I like it, although the resolution is low.|1|movies|4
The movie's story is boring and the acting is poor.|0|movies|5
I watched this movie on tv, it's not good on a small screen. |0|movies|6
watched the movie first and loved it, the book is even better!|1|books |7
I like the story in this book, they should put it on screen.|1|books|8
I love the author, but this book is a waste of time, don't buy it.|0|books|9
;
The following three PROC CAS steps execute the tmMine action to generate a term-by-document matrix for the training data, execute the brTrain action to extract Boolean rules, and then print the rules, which are shown in Output 10.1.1.
proc cas;
loadactionset "textMining";
tmMine / documents='reviews' text='text' docid='did'
parent={name='parent', replace=1} terms={name='terms', replace=1}
parseConfig={name='config', replace=1} offset={name='pos', replace=1}
entities='None' NounGroups=0 stemming=1 tagging=0 reduce=1;
run;
proc cas;
loadactionset "boolRule";
brTrain / table='parent' docid='_document_' termid='_termnum_' minsupports=1
mPositive=1 gPositive=1
docinfo={table='reviews', id='did', targettype='BINARY',
targets={'positive'}}
terminfo={table='terms', id='_termnum_', label='_term_'}
casOuts={rules={name='review_rules' replace=1},
ruleterms={name='review_ruleterms' replace=1}};
run;
proc cas;
fetch / table='review_rules';
run;
Output 10.1.1: Results of the Training Data
| Selected Rows from Table REVIEW_RULES | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| _Index_ | target | target ID | target variable | target value | rule ID | rule | rule set's true positive | rule set's false positive | rule set's support | rule's true positive | rule's false positive | rule's support | F-1 score | precision | recall |
| 1 | positive | 1 | positive | 1 | 1 | like | 2 | 0 | 2 | 2 | 0 | 2 | 0.5714285714 | 1 | 0.4 |
| 2 | positive | 1 | positive | 1 | 2 | better | 3 | 0 | 3 | 1 | 0 | 1 | 0.75 | 1 | 0.6 |
| 3 | positive | 1 | positive | 1 | 3 | great | 4 | 0 | 4 | 1 | 0 | 1 | 0.8888888889 | 1 | 0.8 |
| 4 | positive | 1 | positive | 1 | 4 | love | 5 | 1 | 6 | 1 | 1 | 2 | 0.9090909091 | 0.8333333333 | 1 |
The following DATA step generates testing data for this example:
data mycas.new_reviews;
infile datalines delimiter='|' missover;
length text $300;
input text$ did;
datalines;
love it! a great phone, even better than advertised|1
I like the book, GREATEST in this genre|2
;
The following three PROC CAS steps execute the tmScore action to generate a term-by-document matrix for the testing data, execute the brScore action to score the data, and then print the scoring results, which are shown in Output 10.1.2.
proc cas;
tmscore / documents='new_reviews' text='text' docid='did' terms='terms'
parent={name='new_parent' replace=1} parseconfig='config';
run;
proc cas;
brScore / table='new_parent' ruleterms='review_ruleterms'
casOut={name='score_results',replace=1};
run;
proc cas;
fetch / table='score_results';
run;
Output 10.1.2: Scoring Results
| Selected Rows from Table SCORE_RESULTS | |||
|---|---|---|---|
| _Index_ | document ID | target ID | rule ID |
| 1 | 1 | 1 | 4 |
| 2 | 1 | 1 | 3 |
| 3 | 1 | 1 | 2 |
| 4 | 2 | 1 | 3 |
| 5 | 2 | 1 | 1 |
Deriving Boolean Rules from a Term-by-Document Frequency Matrix
This example is not available for the Lua programming language.
Deriving Boolean Rules from a Term-by-Document Frequency Matrix
This example is not available for the Python programming language.
Deriving Boolean Rules from a Term-by-Document Frequency Matrix
This example is not available for the R programming language.