我有一个包含200多条推文的txt文件,我试图总计该地区所有推文的得分数(以下更多信息) 一条基本推文如下:
[30.346168930000001, -97.73518] 0 2011-08-29 04:54:22 Best vacation of my life #byfar
因此区域定义为
class Region:
def __init__(self, lat_tuple, long_tuple):
self.lat_tuple = lat_tuple
self.long_tuple = long_tuple
def contains(self, lat, long):
return self.lat_tuple[0] <= lat and lat < self.lat_tuple[1] and\
self.long_tuple[0] <= long and long < self.long_tuple[1]
eastern = Region((24.660845, 49.189787), (-87.518395, -67.444574))
central = Region((24.660845, 49.189787), (-101.998892, -87.518395))
mountain = Region((24.660845, 49.189787), (-115.236428, -101.998892))
pacific = Region((24.660845, 49.189787), (-125.242264, -115.236428))
所以我已经能够确定每个句子的得分量如下:
with open('words.txt') as f:
sentiments = {word: int(value)
for word, value in
(line.split(",") for line in f)}
with open('sentences.txt') as f:
for line in f:
values = Counter(word for word in line.split() if word in sentiments)
if not values:
continue
score_Tweet = (sum(values[word]*sentiments[word] for word in values)) // (len(values))
现在我只需要根据他们的地区添加分数。我尝过很多像if语句和def,但是所有这些都带来了错误