全部评论
tfidf???
没做,但是知道tfidf,阮一峰有一篇博文介绍过。
我做了,本地可以,网上AC 0 。。。gg,不知道哪里有问题啊
好像有问题,我发了提问贴
能把题 粘上来吗?
一直12.5%,搞不清楚哪里不对😂
我是这个题目 gg了
我靠,倒排索引,我论文搞的就是这个,后悔没投。。
感觉应该要用字典树做,用数组搞了半天12.5%,GG
找到一份通过的代码,你们看看 #!/usr/bin/env python
import math
import random
import sys
class Solution2(object):
def tfidf(self, line, idf):
tf = {}
tfidf = []
data = []
words = line.split(",")
length = len(words)
for word in words:
tf[word] = tf.get(word, 0) + 1 / length
for word in words:
tfidf.append(str(round(tf.get(word) * idf.get(word), 2)))
data.append("tf:%s,idf:%s" % (tf.get(word), idf.get(word)))
return (tfidf)
def getWordDocs(self, docs):
N = len(docs)
idf = {}
for doc in docs:
words = set(doc.split(","))
for word in words:
idf[word] = idf.get(word, 0) + 1
for (k, v) in idf.items():
idf[k] = round(math.log(N / v, 2), 2)
return idf
if __name__ == "__main__":
solution2 = Solution2()
n = int(sys.stdin.readline().strip())
dd = []
for i in range(0, n):
dd.append(sys.stdin.readline().strip())
idf = solution2.getWordDocs(dd)
for item in dd:
tfidf = solution2.tfidf(item, idf)
print(",".join(tfidf))
相关推荐
点赞 评论 收藏
分享