Text Mining Action Set

Assign Discovered Topics to New Documents

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 demonstrates how to apply a topic model to new data by using the tmMine action and assumes that you have already run the code in Example 39.1.

The following DATA step creates some text content to be scored:

    data mycas.scoreReviews;
    infile datalines delimiter='|' missover;
       length text $300;
       input text$ did;
       datalines;
    Loved the movie! I watched it twice.|1
    I liked the book. Great read! Good Plot!|2
    ;
    run;

The tmScore action enables you to assign topic scores to the new documents. The following PROC CAS step calls the tmScore action and uses the config table, U table, and Terms table from the previous example as input:


 proc cas;
 loadactionset "textMining";
 action tmScore;
 param
    docId="did"
    documents={name="scoreReviews"}
    text="text"
    terms={name="terms"}
    parseConfig={name="config"}
    u={name="svdu"}
    docPro ={ name="scoreDocpro", replace=TRUE}
    topics={name="topics"}
    topicDecision=True
 ;
 action table.fetch /table="scoreDocpro"; run;
 run;
 quit;

Output 39.2.1 displays the contents of the mycas.scoreDocpro table, which contains the results. Each document is assigned a score that indicates how strongly it encapsulates each of the three topics that were discovered in the first example.

Output 39.2.1: Scored Documents

Results from table.fetch

Selected Rows from Table SCOREDOCPRO
_Index_did_Col1__Col2__Col3__TextTopic_1_TextTopic_2_TextTopic_3
1100.68153156040010
220.802015850300100


Assign Discovered Topics to New Documents

This section contains Lua code.


s:upload{'reviews.csv', casout={name='reviews'}}
s:upload{'stoplist.csv',casout={name='stoplist'}}

-- Load action sets
s:loadactionset{actionset='textmining'}

-- Discover topics and Doc Projections
s:tmMine{
       docid='did',
          documents='reviews',
          text='text',
          nounGroups=false,
          tagging=false,
          stopList='stoplist',
          parseConfig={name='config',replace=true},
          parent={name='parent',replace=true},
          offset={name='offset',replace=true},
          terms={name='terms',replace=true},
          reduce=1,
          k=3,
          docpro={name='docpro',replace=True},
          topics={name='topics',replace=True},
          u={name='svdu',replace=True},
          numLabels=3,
          topicDecision=true
}


s:upload{'scoreReviews.csv',casout={name='scoreReviews'}}

 -- Generate document projections based on training data
 s:tmscore{
         docid='did',
         docpro={name='scoreDocpro',replace=True},
         documents='score_reviews',
         parseconfig='config',
         terms='terms',
         text='text',
         topics='topics',
         u='svdu'
         }
 -- discovereed topics
 r=s:fetch{table='scoreDocpro'}
 print(r.Fetch)

Assign Discovered Topics to New Documents

This section contains Python code.

import swat


# Create training data
from io import StringIO
reviews = StringIO('''text,positive,category,did
"This is the greatest phone ever! love it! It can replace my tv!",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.  Good viewing.",1,electronics,3
"The movie itself is great and I liked watching it. Good acting!",1,movies,4
"The movie's story is boring and the acting is poor.",0,movies,5
"I watched this movie but it was boring..",0,movies,6
"The book has a terrific plot!",1,books,7
"The book's plot was suspenseful. Good read.",1,books,8
"I love the author, but this book is a waste of time to read.",0,books,9''')


handler = dmh.CSV(reviews, skipinitialspace=True)
s.addtable(table='reviews', **handler.args.addtable)

handler = dmh.CSV(stoplist, skipinitialspace=True)
s.addtable(table='stoplist', **handler.args.addtable)

# Discover topics and Doc Projections
s.loadactionset(actionset='textmining')
s.tmMine(
	   docid='did',
	   documents='reviews',
	   text='text',
	   nounGroups=False,
	   tagging=False,
	   stopList='stoplist',
	   parseConfig=s.CASTable('config', replace=True),
    parent=s.CASTable('parent', replace=True),
    offset=s.CASTable('offset', replace=True),
    terms=s.CASTable('terms', replace=True),
    reduce=1,
    k=3,
    docpro=s.CASTable('docpro',replace=True),
    topics=s.CASTable('topics',replace=True),
    u=s.CASTable('svdu',replace=True),
    numLabels=3,
    topicDecision=True
)

# Create testing data
scoreReviews = StringIO('''text,did
 "Loved the movie! Even better than advertised.",1
 "I like the book. Better than the movie.",2''')

handler = dmh.CSV(scoreReviews, skipinitialspace=True)
s.addtable(table='scoreReviews', **handler.args.addtable)


s.tmscore(documents='scoreReviews',u='svdu', textvar='text',
           docidvar='did', terms='terms',
           parseconfig='config',
           topics='topics',
           docpro=s.CASTable('scoreDocpro',replace=True),
           topicDecision=True)

# Topic "score" of each document to each topic from the train data
pprint(s.fetch('scoreDocpro'))

Assign Discovered Topics to New Documents

This example is not available for the R programming language.

Last updated: August 04, 2026