human
Can you provide a Python code to compute the term freq matrix given the following:
I am working on the file "Project_search.ipynb".
The first task is:
term freq matrix

with the lexicon we are able to compute the term freq matrix
Can you write Python code for it?
assistant
#Here is Python code to compute the term freq matrix
.

def get_tfm(corpus):
    
    def get_lexicon(corpus):
        lexicon = set()
        for doc in corpus:
            lexicon.update([word for word in doc.split()])
        return list(lexicon)
    
    lexicon = get_lexicon(corpus)
    
    tfm =[]
    for doc in corpus:
        tfv = [0]*len(lexicon)
        for term in doc.split():
            tfv[lexicon.index(term)] += 1
    
        tfm.append(tfv)
    
    return tfm, lexicon

#test_corpus = ['mountain bike', 'road bike carbon', 'bike helmet']
#tfm, lexicon = get_tfm(test_corpus)
#print lexicon
#print tfm