Text Mining Action Set

Derive Topics from a Document Collection

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 derive topics from a collection by using the tmMine action. The following DATA step creates an input data table named mycas.reviews. This step assumes that the CAS engine libref is named mycas, but you can substitute any appropriately named 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! 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
;
run;

The following PROC CAS step loads the included stop list into the active caslib for use:

   proc cas;
       loadtable caslib="ReferenceData" path="en_stoplist.sashdat";
   run;
quit;

The following PROC CAS step calls the tmMine action, which parses the text, applies the stop list, calculates the SVD factorization, and discovers topics all in a single action. When the action completes, Output 39.1.1 through Output 39.1.3 are printed.

 proc cas;
     loadactionset "textMining";
     action tmMine;
     param
     docId="did"
     documents={ name="reviews"}
     text="text"
     nounGroups= False
     tagging = True
     stemming= True
     stopList ={ name="en_stoplist"}
     parseConfig={name="config", replace=TRUE}
     parent ={ name="parent",replace=TRUE}
     offset ={name="offset",replace=TRUE}
     terms ={ name="terms", replace=TRUE}
     reduce=2
     k=3
     docPro ={ name="docpro", replace=TRUE}
     topics ={ name="topics", replace=TRUE}
     u ={ name="svdu", replace=TRUE}
     numLabels=3
     topicDecision=True
 ;
 action table.fetch /table="topics", orderBy="_TopicID_"; run;
 action table.fetch /table="docpro", orderBy="did"; run;
 action table.fetch /table="svdu", orderBy="_TermNum_"; run;
 run;
 quit;

Output 39.1.1 displays the contents of the mycas.topics table, which contains the discovered topics..

Output 39.1.1: Discovered Topics

Results from table.fetch

Selected Rows from Table TOPICS
_Index_Topic IDTopicCategoryNumber of TermsNumber of DocsTerm CutoffDocument Cutoff
11book, plot, readMult320.3810.65
22movie, +bore, +watchMult430.3850.632
33+television, phone, resolutionMult430.3850.524


Output 39.1.2 displays the contents of the mycas.docpro table, which contains the relationship of each document to each topic.

Output 39.1.2: Document Projections

Results from table.fetch

Selected Rows from Table DOCPRO
_Index_did Score for "book,
plot, read"
Score for "movie,
+bore, +watch"
Score for "+television,
phone, resolution"
book, plot, readmovie, +bore, +watchtelevision, phone, resolution
11000.5798821773001
22000.6905117599001
33000.6110259518001
4400.68251950010
5500.74572035120010
6600.83996619920010
770.818507955800100
880.860849480600100
990.645885457300000


Output 39.1.3 displays the contents of the mycas.svdu table, which contains the topic score of each term to each topic in the training data.

Output 39.1.3: Relationship between Topics and Terms

Results from table.fetch

Selected Rows from Table SVDU
_Index__TermNum__Col1__Col2__Col3_
11-0.0303167540.21513834060.2264079152
22-0.0235488070.01082771920.4187120658
330.00057393150.5119842534-0.023157874
440.4942887978-0.0174346090.0361238772
550.0065162170.43861061170.0034250391
660.1556906713-0.0363804380.378769758
77-0.023118229-0.0245523780.4631866963
88-0.012714918-0.027698370.4603405044
99-0.0190711590.4704832817-0.019501009
10100.5951267514-0.021917194-0.019394378
11110.01412644720.4927909226-0.047333542
12120.5785006335-0.004751182-0.081927976
1313-0.0235488070.01082771920.4187120658
14140.19816606720.17685022270.1289030697


Derive Topics from a Document Collection

This section contains Lua code.


s:upload{'reviews.csv', casout={name='reviews'}}
--s:loadtable(caslib="ReferenceData",path="en_stoplist.sashdat")
s:loadtable{caslib="casuser",path="en_stoplist.sashdat"}
-- Load action sets
s:loadactionset{actionset='textmining'}

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


-- discovered topics
r = s:fetch{table={name="topics"}}
print(r.Fetch)
-- relationship of each document to each topic
r = s:fetch{table='docpro'}
print(r.Fetch)
-- relationship of each term to each topic
r = s:fetch{table='svdu'}
print(r.Fetch)

Derive Topics from a Document Collection

This section contains Python code.

 # 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)
 s.loadtable(caslib="ReferenceData",path="en_stoplist.sashdat")

 # Create testing data
 score_reviews = StringIO('''text,did
 "Loved the movie! I watched it twice.",1
 "I liked the book. Great read! Good Plot!",2''')

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

s.builtins.loadActionSet(actionSet="textMining")                           # 1

s.textMining.tmMine(docId="did",                                           # 2
                    docPro={"name":"docpro", "replace":True},
                    documents={"name":"reviews"},
                    k=3,
                    nounGroups=False,
                    numLabels=3,
                    offset={"name":"offset", "replace":True},
                    parent={"name":"parent", "replace":True},
                    parseConfig={"name":"config", "replace":True},
                    reduce=2,
                    stopList={"name":"en_stopList"},
                    tagging=True,
                    terms={"name":"terms", "replace":True},
                    text="text",
                    topicDecision=True,
                    topics={"name":"topics", "replace":True},
                    u={"name":"svdu", "replace":True}
                   )


#discovered topics
print(s.fetch(table='topics',orderBy='_TopicID_'))
#relationship of each document to each topic
print(s.fetch (table='docpro',orderBy='did'))
#relationship of each  term to each topic
print(s.fetch(table='svdu', orderBy='_TermNum_'))

Derive Topics from a Document Collection

This example is not available for the R programming language.

Last updated: August 04, 2026