我正在编写一个函数来获取单词的长度并对其进行计数。在此函数中,我尝试获取两个字典的交集,但无法正确到达其中两个。
我的问题是在第37行(在最后一行之前),我无法到达位于13行的di_Title = di(Title)
我尝试了全局变量,但是它对我不起作用!
我的功能:
def Text_Analyze(Raw_Text):
Title_Length = []
Title_Dictionary = []
Article_Length = []
Article_Dictionary = []
Intersection_Dictionary = []
Title_Info = Raw_Text.PageTitle
for Each_Line in Title_Info:
Title = remove_punctuation(Each_Line)
Title = Title.lower()
Title = Title.split()
Title_Length.append(len(Title))
di_Title = dict()
for w_Title in Title:
Root_Title_Split = TurkishStemmer()
Root_Title_Word = Root_Title_Split.stem(w_Title)
if Root_Title_Word in di_Title:
di_Title[Root_Title_Word] = di_Title[Root_Title_Word] + 1
else:
di_Title[Root_Title_Word] = 1
Title_Dictionary.append(di_Title)
Article_Info = Raw_Text.PageArticle
for each_Line in Article_Info:
Article = remove_punctuation(each_Line)
Article = Article.lower()
Article = Article.split()
Article_Length.append(len(Article))
di_Article = dict()
for w_Article in Article:
root_Article_Split = TurkishStemmer()
root_Article_Word = root_Article_Split.stem(w_Article)
if root_Article_Word in di_Article:
di_Article[root_Article_Word] = di_Article[root_Article_Word] + 1
else:
di_Article[root_Article_Word] = 1
Article_Dictionary.append(di_Article)
Int_Word_Dic = intersect(di_Title, di_Article)
Intersection_Dictionary.append(Int_Word_Dic)
答案 0 :(得分:2)
在范围内声明的变量(在您的情况下为循环)在声明范围之外不可访问。
您可以在更高的范围(在您的情况下,在循环之前)声明它,并根据需要更改其值。
答案 1 :(得分:1)
我没有收到您的问题,但是只要每个title_info行与article_info中的同一行匹配,我都可以这样做
def Text_Analyze(Raw_Text):
Title_Length = []
Title_Dictionary = []
Article_Length = []
Article_Dictionary = []
Intersection_Dictionary = []
Title_Info = Raw_Text.PageTitle
for Each_Line in Title_Info:
Title = remove_punctuation(Each_Line)
Title = Title.lower()
Title = Title.split()
Title_Length.append(len(Title))
di_Title = dict()
for w_Title in Title:
Root_Title_Split = TurkishStemmer()
Root_Title_Word = Root_Title_Split.stem(w_Title)
if Root_Title_Word in di_Title:
di_Title[Root_Title_Word] = di_Title[Root_Title_Word] + 1
else:
di_Title[Root_Title_Word] = 1
Title_Dictionary.append(di_Title)
Article_Info = Raw_Text.PageArticle
for c,each_Line in enumerate(Article_Info):
Article = remove_punctuation(each_Line)
Article = Article.lower()
Article = Article.split()
Article_Length.append(len(Article))
di_Article = dict()
for w_Article in Article:
root_Article_Split = TurkishStemmer()
root_Article_Word = root_Article_Split.stem(w_Article)
if root_Article_Word in di_Article:
di_Article[root_Article_Word] = di_Article[root_Article_Word] + 1
else:
di_Article[root_Article_Word] = 1
Article_Dictionary.append(di_Article)
Int_Word_Dic = intersect(Title_Dictionary[c], di_Article)
Intersection_Dictionary.append(Int_Word_Dic)