這個程序是為了找到a句子和單詞之間的相似之處以及它們在同義詞中的相似之處我在第一次編碼時下載了nltk,它運行并且沒有錯誤,但是幾天后當我運行該程序時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)索引錯誤:列表索引超出范圍當 uploaded_doc_sentence 很短并且使用長句子時,會出現此錯誤。check.append(wn.wup_similarity(數據,sen[0]))我想比較句子和單詞并將結果存儲起來。這個類型#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貢獻1802條經驗 獲得超5個贊
問題是 中包含空列表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 塊,您實際上是在嘗試索引列表的第一個元素,從而為您提供一個IndexError.

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