|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Dave111 于 2020-3-6 16:07 编辑
用split()函数对一个情感词典分割后返回列表senList,再创建字典senDict时总是报错说IndexError: list index out of range
但是分别print列表senList的0和1位置都可输出,不知道原因在哪,求大佬相助(相关截图见下面)
- #读取 filename路径的每一行数据,存在data这个列表中并返回
- def readLines2(filename):
- fopen = open(filename,encoding='utf-8',errors='ignore')
- data=[]
- for x in fopen.readlines():
- if x.strip() != ' ':
- data.append(x.strip())
- fopen.close()
- return data
- #主要为情感定位
- def words():
- #情感词
- senList = readLines2('BosonNLP_sentiment_score.txt')
- print(len(senList))
- senDict = defaultdict()
- for s in senList:
- #print(s.split(" ",1)[0])
- #print(s.split(" ",1)[1])
- senDict[s.split(" ",1)[0]] = s.split(" ",1)[1]
- #否定词
- notList = readLines2('notDict.txt')
- #程度副词
- degreeList = readLines3('degreeDict.txt')
- degreeDict = defaultdict()
- for d in degreeList:
- degreeDict[d.split(' ')[0]] = d.split(' ')[1]
- return senDict,notList,degreeDict
复制代码
|
|