私はPythonの新機能ですが、このエラーへの回答を検索しましたが、おそらく何か本当に基本的なものです。
私は、テキストで使用する単語に基づいて作者を特定するプロジェクトに取り組んでいます。単語をキーとして各著者の辞書に単語を追加し、その値はその単語の著者のテキストに現れる回数です。私はまた、すべての作者の言葉の語彙を作成し、これらを使って確率を計算します。これは最初は問題なく動作します。
私のコーパスが特に大きいわけではないので、私が使用しているk倍のクロス検証を追加すると、私の問題が発生します。私は空の辞書に割り当てた名前と一致する著者名のリストを繰り返します。一度私が望むファイルを解凍したら、辞書にクリーン/解析されたテキストを追加したいのですが、上記のエラーが出ます。author [word] = 1辞書fn以下のコードの最後の2行目を呼び出してください。私の他の答えの読書からは、strが不変であることと関係があるが、私は自分の問題に答えを適用する方法を見ることができない。あなたの助けが大変ありがとう! Ps私はこの作業をすべて行うことができる図書館などがあると知っていますが、プロジェクトの全体像は自分のモデルを構築し、それを他のモデルと比較することです。Pythonエラー: 'str'オブジェクトは項目割り当てをサポートしていません
path = "C:\\......\The Letters\\"
#create an empty vocab set
vocab = set()
stop = stopwords.words('english')
snowball = SnowballStemmer('english')
#create empty dictionary for each author
AuthorA = {}
AuthorB = {}
AuthorC = {}
authorList = ["AuthorA","AuthorB","Authorc"]
#function to preprocess the words. Opens & reads file, removes non alphabet
#characters, converts to lowercase, and tokenizes
def cleanText(path,author,eachfile):
f= open(path+author+"\\"+eachfile, "r")
contents = f.read()
strip = re.sub('[^a-zA-Z]',' ',contents)
lowerCase = strip.lower()
allwords = lowerCase.split()
return allwords
#function to add words to the vocabulary set
def createVocab(allwords):
for word in allwords:
if len(word)>= 4:
vocab.update(allwords)
return
#function to add words to author dictionary and count occurrences of each word
def dictionary(allwords, author):
for word in allwords:
if len(word)>= 4:
if word in author:
author[word]= author[word]+1
else:
author[word]= 1
return
def main():
global authorList
global path
global vocab
global AuthorA
global AuthorB
global AuthorC
for author in authorList:
#filename and path
listing = os.listdir(path+author)
#specify parameters for k fold validation
#split into 10 folds and take a file form each fold
#repeat for until the entire directory has been split
folds = 10
subset_size = len(path+author)/folds
for i in range(folds):
#use these files to train the model
current_train = listing[:i*subset_size:]+listing[(i+1)*subset_size:]
#use these files to test the model
current_test = listing[i*subset_size:][:subset_size]
#iterate through the files selected by current_train variable
for eachfile in current_train:
#call function to parse text
allwords = cleanText(path,author,eachfile)
#call fn to add words to dictionary
dictionary(allwords, author)
#call fn to add words to vocab
createVocab(allwords)
どのラインでエラーが表示されますか? –
[よく質問するにはどうすればよいですか?](http://stackoverflow.com/help/how-to-ask)と[最小限で完全で検証可能なサンプルを作成する方法](http: //stackoverflow.com/help/mcve)。今述べたように、質問はSO標準では広すぎます。また、インデントにも注意してください。 Pythonインデントでは、構文の一部であり、コード内では疑わしいと思われます。 –
エラーは、最後の2行目の辞書関数を呼び出し、else:author [word] = 1を参照しているときです。私はD.Camが答えてくれたと思っています。今コレクションを理解するだけです! – magsw