ask questions for clarification
をしようとしたとき、私はちょうどよりよく理解するために私のコードを掲示した場合、それが最善だと思う上げます。だから、まずファイル名を入力するようにユーザーに要求し、ファイル名が存在するかどうかを確認するだけです。そして、そうでなければ、プログラムを終了する。のみtxtファイルの例外TypeErrorで特定の行を見てしようとしている:「<string>に」左オペランドとして文字列が必要ではなく、辞書には、私が
私は前に似て、私は別のファイルが存在するならば、私はチェックのdict
に入れたキーワードのリストを含む個別のファイルを、...持っています。このファイルでは、最初のファイルのキーワードが2番目のファイルに含まれているかどうかを調べるためにImをチェックします。感情値を計算する。
for line in open('tweets.txt'):
line = line.split(" ")
lat = float(line[0][1:-1]) #Stripping the [ and the ,
long = float(line[1][:-1]) #Stripping the ]
if eastern.contains(lat, long):
eastScore += score(line)
elif central.contains(lat, long):
centralScore += score(line)
elif mountain.contains(lat, long):
mountainScore += score(line)
elif pacific.contains(lat, long):
pacificScore += score(line)
else:
continue
どのように私が行だけに集中することができ、次のようになります。しかし、これは私がちょうど私の質問は以下のセクションでは、少し手
前に説明したいとの助けを必要とするものではありませんファイル全体ではなくキーワードを含むファイル?
この部分で私は値のない行を無視します。
with open('tweets.txt') as f:
for line in f:
values = Counter(word for word in line.split() if word in sentiments)
if not values:
continue
私は、このような新しいファイルを作成し、新しいファイルにキーワードを含む行を書くような方法を試してみましたが、それは
TypeError: 'in <string>' requires string as left operand, not dict
を上げたが、それは私がとにかくやりたいことは本当に波平。だから私の最初の質問は、上記のセクションのキーワードを含む行だけにどのように集中できるのでしょうか?
[41.923916200000001, -88.777469199999999] 6 2011-08-28 19:24:18 My life is a moviee.
from collections import Counter
try:
keyW_Path = input("Enter file named keywords: ")
keyFile = open(keyW_Path, "r")
except IOError:
print("Error: file not found.")
exit()
# Read the keywords into a list
keywords = {}
wordFile = open('keywords.txt', 'r')
for line in wordFile.readlines():
word = line.replace('\n', '')
if not(word in keywords.keys()): #Checks that the word doesn't already exist.
keywords[word] = 0 # Adds the word to the DB.
wordFile.close()
# Read the file name from the user and open the file.
try:
tweet_path = input("Enter file named tweets: ")
tweetFile = open(tweet_path, "r")
except IOError:
print("Error: file not found.")
exit()
#Calculating Sentiment Values
with open('keywords.txt') as f:
sentiments = {word: int(value) for word, value in (line.split(",") for line in f)}
with open('tweets.txt') as f:
for line in f:
values = Counter(word for word in line.split() if word in sentiments)
if not values:
continue
happyScore_Tweet = (sum(values[word]*sentiments[word] for word in values)) // (len(values))
print(happyScore_Tweet)
def score(tweet):
total = 0
for word in tweet:
if word in sentiments:
total += 1
return total
#Classifying the regions
class Region:
def __init__(self, lat_range, long_range):
self.lat_range = lat_range
self.long_range = long_range
def contains(self, lat, long):
return self.lat_range[0] <= lat and lat < self.lat_range[1] and\
self.long_range[0] <= long and long < self.long_range[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))
eastScore = 0
centralScore = 0
pacificScore = 0
mountainScore = 0
happyScoreE = 0
for line in open('tweets.txt'):
line = line.split(" ")
lat = float(line[0][1:-1]) #Stripping the [ and the ,
long = float(line[1][:-1]) #Stripping the ]
if eastern.contains(lat, long):
eastScore += score(line)
elif central.contains(lat, long):
centralScore += score(line)
elif mountain.contains(lat, long):
mountainScore += score(line)
elif pacific.contains(lat, long):
pacificScore += score(line)
else:
continue
ここで行ったことを説明できますか? – NewToPython43532
.findallを使うと、指定した正規表現を持つすべての文字列を見つけることができます。これは、正規表現内の一致するグループのタプルのリストを返します(一致するグループは、正規表現内のカッコによって決定されます)。正規表現についてもっと知りたい場合は、https://regex101.com/ –
を試してみることをお勧めします。これを変更する必要がありますか? (\ - ?\ d + \。\ d +?)、\ s *(\ - ?\ d + \。\ d +) " – NewToPython43532