動(dòng)漫人物
2023-07-18 16:42:31
這個(gè)程序是為了找到a句子和單詞之間的相似之處以及它們?cè)谕x詞中的相似之處我在第一次編碼時(shí)下載了nltk,它運(yùn)行并且沒有錯(cuò)誤,但是幾天后當(dāng)我運(yùn)行該程序時(shí)import nltknltk.download('stopwords')nltk.download('wordnet')from nltk.tokenize import word_tokenizefrom nltk.corpus import stopwordsfrom nltk.corpus import wordnet as wnfiltered_uploaded_sentences = []uploaded_sentence_synset = []database_word_synset = []uploaded_doc_sentence=" The issue of text semantics, such as word semantics and sentence semantics has received increasing attentions in recent years. However, rare research focuses on the document-level semantic matching due to its complexity. Long documents usually have sophisticated structure and massive information, which causes hardship to measure their semantic similarity. The semantic similarity between words, sentences, texts, and documents is widely studied in various fields, including natural language processing, document semantic comparison, artificial intelligence, semantic web, and semantic search engines. "database_word=["car","complete",'focus',"semantics"]stopwords = stopwords.words('english')uploaded_sentence_words_tokenized = word_tokenize(uploaded_doc_sentence)#filtering the sentence and synsetfor word in uploaded_sentence_words_tokenized: if word not in stopwords: filtered_uploaded_sentences.append(word)print (filtered_uploaded_sentences)for sentences_are in filtered_uploaded_sentences: uploaded_sentence_synset.append(wn.synsets(sentences_are)) print(uploaded_sentence_synset)#for finding similrity in the wordsfor databasewords in database_word: database_word_synset.append(wn.synsets(databasewords)[0]) print(database_word_synset)索引錯(cuò)誤:列表索引超出范圍當(dāng) uploaded_doc_sentence 很短并且使用長(zhǎng)句子時(shí),會(huì)出現(xiàn)此錯(cuò)誤。check.append(wn.wup_similarity(數(shù)據(jù),sen[0]))我想比較句子和單詞并將結(jié)果存儲(chǔ)起來。這個(gè)類型#the similarity main function for wordsfor data in database_word_synset: for sen in uploaded_sentence_synset : check.append(wn.wup_similarity(data,sen[0]))print(check)
2 回答

12345678_0001
TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
問題是 中包含空列表uploaded_sentence_synset。我不確定您要做什么,但將最后一段代碼修改為:
for data in database_word_synset:
for sen in uploaded_sentence_synset:
if sen:
check.append(wn.wup_similarity(data, sen[0]))
如果沒有 if-else 塊,您實(shí)際上是在嘗試索引列表的第一個(gè)元素,從而為您提供一個(gè)IndexError.

森林海
TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊
通過從列表中刪除空的 [] 列表塊并將多維列表變成單維列表,問題就解決了
list2 = [x for x in main_sen if x != []]
print(list2)
result=list()
for t in list2:
for x in t:
result.append(x)
添加回答
舉報(bào)
0/150
提交
取消