2017-11-14 2 views
1

ich zur Zeit an einem kleinen Projekt arbeite und habe einen kompletten Geist leer getroffen, ich habe den folgenden Code Begriff Frequenz zu berechnen: von Bag import *TF_IDF Berechnung Sendefehler

words = 
['the','new','the','shiny','new','car','went','through','the','tunnel'] 
carDoc = Bag() 
for word in words: 
    carDoc.add(word) 

def tf(word, carDoc): 
    if word != "" and carDoc.size() > 0: 
     return carDoc.count(word)/carDoc.size() 

Ich habe auch die folgenden Code für inverse Dokumenthäufigkeit:

from Bag import * 
from math import log 

carDoc1 = Bag() 
for word in ['the', 'car']: 
    carDoc1.add(word) 

carDoc2 = Bag() 
for word in ['the', 'shiny', 'new']: 
    carDoc2.add(word) 

allCarDocs = [carDoc1, carDoc2] 

def idf(word, carDocs): 
    total = len(allCarDocs) 
    wordIsIn = 0 
    for docs in allCarDocs: 
     if docs.contains(word): 
      wordIsIn = wordIsIn + 1 
    return log(total/(1 + wordIsIn)) 

carDoc1 = Bag() 
for word in ['the', 'car']: 
    carDoc1.add(word) 
carDoc2 = Bag() 
for word in ['the', 'shiny', 'new']: 
    carDoc2.add(word) 

allCarDocs = [carDoc1, carDoc2] 

def tf_idf(word, documents): 
    return tf(word, carDoc) * idf (word, allCarDocs) 

und der Fehler, den ich bekommen ist Cardoc nicht

definiert

Diese sind fein und arbeiten, wie ich t bestimmt sem auch, aber wenn es darum geht, die tfidf-Funktion zu implementieren, bekomme ich immer Fehler. return tf (Wort, CARDOC) * idf (Wort, allCarDocs)

wo tun Sie Cardoc erhalten möchten: Jede Hilfe bei der Lösung des TFIDF für dieses Beispiel wird

+0

Können Sie den Fehler auch posten? –

Antwort

0

def tf_idf (Wort, Dokumente) erkannt werden, und allCarDocs, wenn Ihre Funktion (Wort, Dokumente) nehmen ??