如何用python完成评分功能呢_从文件python进行单词分析和评分
我正在對一個句子進行逐詞分析,例如
“嘿!這是一部很棒的電影???”
我上面有很多句子.
我有一個巨大的數據集文件,如下所示,如果該詞存在,我必須進行快速查找.如果這樣做,則進行分析并將其存儲在字典中,例如從單詞文件中獲取分數,句子的最后一個單詞的分數,句子的第一個單詞等.
句子[i] =>嘿!!這是一部很棒的電影???
句子[0] =嘿,句子[1] =有!句子[2] =這個,依此類推.
這是代碼:
def unigrams_nrc(file):
for line in file:
(term,score,numPos,numNeg) = re.split("", line.strip())
if re.match(sentence[i],term.lower()):
#presence or absence of unigrams of a target term
wordanalysis["unigram"] = found
else:
found = False
if found:
wordanalysis["trail_unigram"] = found if re.match(sentence[(len(sentence)-1)],term.lower()) else not(found)
wordanalysis["lead_unigram"] = found if re.match(sentence[0],term.lower()) else not(found)
wordanalysis["nonzero_sscore"] = float(score) if (float(score) != 0) else 0
wordanalysis["sscore>0"] = (float(score) > 0)
wordanalysis["sscore"] = (float(score) != 0)
if re.match(tweet[len(sentence)-1],term.lower()):
wordanalysis["sscore !=0 last token"] = (float(score) != 0)
這是文件(此文件中超過4000個單詞):
#fabulous 7.526 2301 2
#excellent 7.247 2612 3
#superb 7.199 1660 2
#perfection 7.099 3004 4
#terrific 6.922 629 1
#magnificent 6.672 490 1
#sensational 6.529 849 2
#heavenly 6.484 2841 7
#ideal 6.461 3172 8
#partytime 6.111 559 2
#excellence 5.875 1325 6
@thisisangel 5.858 217 1
#wonderful 5.727 3428 18
elegant 5.665 537 3
#perfect 5.572 3749 23
#fine 5.423 2389 17
excellence 5.416 279 2
#realestate 5.214 114 1
bicycles 5.205 113 1
我想知道是否有更好的方法可以做到上述幾點?
定義更好的方法:更快,更少的代碼和優雅.
我是python的新手,所以我知道這不是最好的代碼.我大約有4個文件需要檢查分數,因此希望以最佳方式實現此功能.
總結
以上是生活随笔為你收集整理的如何用python完成评分功能呢_从文件python进行单词分析和评分的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我国首台紧凑型加速器质谱仪研制成功,相关
- 下一篇: 因市场低迷,美国知名 NFT 交易平台