Merge branch 'tf_idf'
This commit is contained in:
commit
c8640001c7
12
indexer.py
12
indexer.py
@ -134,6 +134,8 @@ class Indexer():
|
|||||||
# so I came up with this, if anyone knows how to get a single cell and can explain it to
|
# so I came up with this, if anyone knows how to get a single cell and can explain it to
|
||||||
# me I would love to know, as I think that method might be quicker, maybe, idk it like
|
# me I would love to know, as I think that method might be quicker, maybe, idk it like
|
||||||
# 4am
|
# 4am
|
||||||
|
|
||||||
|
# retuns a dict of words/n-grams with their assosiated tf-idf score *can also return just a single score or a pandas dataframe
|
||||||
# https://stackoverflow.com/questions/34449127/sklearn-tfidf-transformer-how-to-get-tf-idf-values-of-given-words-in-documen
|
# https://stackoverflow.com/questions/34449127/sklearn-tfidf-transformer-how-to-get-tf-idf-values-of-given-words-in-documen
|
||||||
|
|
||||||
# Andy: added paramenter imporant_words in order to do multiplication of score
|
# Andy: added paramenter imporant_words in order to do multiplication of score
|
||||||
@ -162,6 +164,16 @@ class Indexer():
|
|||||||
#print(df)
|
#print(df)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return -1
|
return -1
|
||||||
|
tfidf = TfidfVectorizer(ngram_range=(1,3)) # ngram_range is range of n-values for different n-grams to be extracted (1,3) gets unigrams, bigrams, trigrams
|
||||||
|
tfidf_matrix = tfidf.fit_transform(words) # fit trains the model, transform creates matrix
|
||||||
|
df = pd.DataFrame(tfidf_matrix.toarray(), columns = tfidf.get_feature_names_out()) # store value of matrix to associated word/n-gram
|
||||||
|
#return(df.iloc[0][''.join(word)]) #used for finding single word in dataset
|
||||||
|
data = df.to_dict() # transform dataframe to dict *could be expensive the larger the data gets, tested on ~1000 word doc and took 0.002 secs to run
|
||||||
|
return data # returns the dict of words/n-grams with tf-idf
|
||||||
|
#print(df) # debugging
|
||||||
|
except:
|
||||||
|
print("Error in tf_idf!")
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
def get_data(self):
|
def get_data(self):
|
||||||
|
10
mytest.py
10
mytest.py
@ -4,6 +4,7 @@ from sklearn.feature_extraction.text import TfidfVectorizer
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
#tf_idf
|
#tf_idf
|
||||||
#words = whole text
|
#words = whole text
|
||||||
#word the word we finding the score for
|
#word the word we finding the score for
|
||||||
@ -19,13 +20,12 @@ words = ['this is the first document '
|
|||||||
doc1 = ["I can't fucking take it any more. Among Us has singlehandedly ruined my life. The other day my teacher was teaching us Greek Mythology and he mentioned a pegasus and I immediately thought 'Pegasus? more like Mega Sus!!!!' and I've never wanted to kms more. I can't look at a vent without breaking down and fucking crying. I can't eat pasta without thinking 'IMPASTA??? THATS PRETTY SUS!!!!' Skit 4 by Kanye West. The lyrics ruined me. A Mongoose, or the 25th island of greece. The scientific name for pig. I can't fucking take it anymore. Please fucking end my suffering."]
|
doc1 = ["I can't fucking take it any more. Among Us has singlehandedly ruined my life. The other day my teacher was teaching us Greek Mythology and he mentioned a pegasus and I immediately thought 'Pegasus? more like Mega Sus!!!!' and I've never wanted to kms more. I can't look at a vent without breaking down and fucking crying. I can't eat pasta without thinking 'IMPASTA??? THATS PRETTY SUS!!!!' Skit 4 by Kanye West. The lyrics ruined me. A Mongoose, or the 25th island of greece. The scientific name for pig. I can't fucking take it anymore. Please fucking end my suffering."]
|
||||||
doc2 = ["Anyways, um... I bought a whole bunch of shungite rocks, do you know what shungite is? Anybody know what shungite is? No, not Suge Knight, I think he's locked up in prison. I'm talkin' shungite. Anyways, it's a two billion year-old like, rock stone that protects against frequencies and unwanted frequencies that may be traveling in the air. That's my story, I bought a whole bunch of stuff. Put 'em around the la casa. Little pyramids, stuff like that."]
|
doc2 = ["Anyways, um... I bought a whole bunch of shungite rocks, do you know what shungite is? Anybody know what shungite is? No, not Suge Knight, I think he's locked up in prison. I'm talkin' shungite. Anyways, it's a two billion year-old like, rock stone that protects against frequencies and unwanted frequencies that may be traveling in the air. That's my story, I bought a whole bunch of stuff. Put 'em around the la casa. Little pyramids, stuff like that."]
|
||||||
word = 'life'
|
word = 'life'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tfidf = TfidfVectorizer()
|
tfidf = TfidfVectorizer(ngram_range=(3,3)) # ngram_range is range of n-values for different n-grams to be extracted (1,3) gets unigrams, bigrams, trigrams
|
||||||
tfidf_matrix = tfidf.fit_transform(doc1)
|
tfidf_matrix = tfidf.fit_transform(words)
|
||||||
df = pd.DataFrame(tfidf_matrix.toarray(), columns = tfidf.get_feature_names_out())
|
df = pd.DataFrame(tfidf_matrix.toarray(), columns = tfidf.get_feature_names_out())
|
||||||
print(df.iloc[0][''.join(word)])
|
#print(df.iloc[0][''.join(word)])
|
||||||
#print(df)
|
data = df.to_dict()
|
||||||
except KeyError: # word does not exist
|
except KeyError: # word does not exist
|
||||||
print(-1)
|
print(-1)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user