Text Mining Action Set
Example 22.2 Assign Discovered Topics to New Documents
Assign Discovered Topics to New Documents
This section contains PROC CAS code.
Note: Input data must be in a CAS table that is accessible in your CAS session. This 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 3: Shared Concepts in SAS Visual Data Mining and Machine Learning 8.3: Procedures Guide. For more information about PROC CAS and programming in CASL, see Getting Started with CASL, SAS Cloud Analytic Services: CAS Procedure Programming Guide and Reference, and SAS Viya: System Programming Guide.
proc cas;
loadtable caslib="ReferenceData" path="en_stoplist.sashdat";
run;
quit;
The tmScore action enables you to apply a topic model to new data. 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 22.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 22.2.1: Scored Documents
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)
# Generate document projections based on training data
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.