1. Bayesian classification algorithm (constructs the word vector from the text)
the build process of the vector is as followsdefLoaddataset (): Postinglist= [['my','Dog',' has','Flea', 'problems',' Help',' please'], ['maybe',' not',' Take','him', ' to','Dog','Park','Stupid'], ['my','dalmation',' is',' So','Cute', 'I',' Love','him'], ['Stop','Posting','Stupid','Worthless','Garbage'], ['Mr','Licks','ate','my','Steak',' How', ' to','Stop','him'], ['quit','Buying','Worthless','Dog',' Food','Stupid']] Classvec= [0,1,0,1,0,1]#1 for insulting words, 0 for normal speech returnPostinglist,classvecdefcreatevocablist (dataSet): Vocabset=set ([]) forDocumentinchDataset:vocabset= Vocabset |set (document)returnlist (Vocabset)defSetofwords2vec (vocablist,inputset): Returnvec= [0]*Len (vocablist) forWordinchInputset:ifWordinchVocablist:returnvec[vocablist.index (word)]= 1Else:Print("The Word:%s is isn't in my vocabulary") %WordreturnReturnvec
The python command that is called in CMD's doc command line is as follows Import == bayes.createvocablist (listoposts) myvocablist
The results of the program run are as follows
Check the glossary above and you will find that there are no duplicate words. Currently the glossary is not sorted, and if needed, it can be sorted later.
Here's a look at how the function Setofwords2vec () works
The function uses a glossary or all the words that you want to check as input, and then constructs a feature for each of these words.
Once a document is given (a message on the Spotted Dog website), the document is converted to a word vector. Next, check the validity of the function. In Myvocablist
What is the word for an element indexed to 2? , it should be the word Help. The word appears in the first document, and now check to see if it appears in the fourth document.
41. Learning for Python, Getting Started