2017-03-31 6 views
-1

私が現在持っているコードを以下に示します。最初に行うのは、ユーザーに文章を入力することです。次に、プログラムは、文中の各単語の位置を見つけ、その単語をリストに分割して個々の単語を得る。次に、プログラムは繰り返し単語を取り除いて、リスト内の単語をユニークにします。次に、プログラムは、文(例えば、1,2,3,4,1,1,2,3,5)内の単語の位置とユニークな単語を別々のファイル(ユーザが名前を付けることができる)に保存する)。プログラムの次の部分は、別個のファイルから一意のテキストを解凍しようとし、その文および一意の単語中の単語の位置から元の文を再作成しようと試みる。私は別にこのテストを行ったので、この段階が機能することは分かっています。 OutputDecompressed decompression.append(orgwords [i])と はIndexErrorで、リストの索引が範囲外ですpython解凍テキスト

ファイル "/Users/Sid/Desktop/Task3New.py"、行70:私は今、プログラムを実行するときしかし、私はこのエラーメッセージを取得しておきます:リストインデックスが範囲外です

なぜこれが機能しないのか分かりません。すべての助けを感謝、ありがとう。

import json 
import os.path 

def InputSentence(): 
    global sentence 
    global words 
    sentence = input("Enter a sentence: ") 
    words = sentence.split(' ') 

def Validation(): 
    if sentence == (""): 
     print ("No sentence was inputted. \nPlease input a sentence...") 
     Error() 

def Uniquewords(): 
    print ("Words in the sentence: " + str(words)) 
    for i in range(len(words)): 
     if words[i] not in unilist: 
      unilist.append(words[i]) 
    print ("Unique words: " + str(unilist)) 

def PosText(): 
    global find 
    global pos 
    find = dict((sentence, words.index(sentence)+1) for sentence in   list(words)) 
    pos = (list(map(lambda sentence: find [sentence], words))) 
    return (pos) 

def OutputText(): 
    print ("The positions of the word(s) in the sentence are: " + str(pos)) 

def SaveFile(): 
    filename = input("We are now going to save the contents of this program  into a new file. \nWhat would you like to call the new file? ") 
    newfile = open((filename)+'.txt', 'w') 
    json.dump([unilist, pos], newfile) 
    newfile.close 


def InputFile(): 
    global compfilename 
    compfilename = input("Please enter an existing compressed file to be decompressed: ") 

def Validation2(): 
    if compfilename == (""): 
     print ("Nothing was entered for the filename. Please re-enter a valid filename.") 
     Error() 
    if os.path.exists(filename + ".txt") == False: 
     print ("No such file exists. Please enter a valid existing file.") 
     Error() 

def OutputDecompressed(): 
    newfile = open((compfilename)+'.txt', 'r') 
    saveddata = json.load(newfile) 
    orgpos = saveddata[1] 
    orgwords = saveddata[0] 
    print ("Unique words in the original sentence: " + str(orgwords) + "\nPosition of words in the sentence: " + str(orgpos)) 
    decompression = [] 
    prev = orgpos[0] 
    x=0 
    #decomposing the index locations 
    for cur in range(1,len(orgpos)): 
     if (prev == orgpos[cur]): x+= 1 
     else: 
      orgpos[cur]-=x 
      x=0 
     prev = orgpos[cur] 
    #Getting the output 
    for i in orgpos: 
     decompression.append(orgwords[i-1]) 
    finalsentence = (' '.join(decompression)) 
    print ("Original sentence from file: " + finalsentence) 


def Error(): 
    MainCompression() 


def MainCompression(): 
    global unilist 
    unilist = [] 
    InputSentence() 
    Uniquewords() 
    PosText() 
    OutputText() 
    SaveFile() 
    InputFile() 
    Validation() 
    OutputDecompressed() 

MainCompression() 

答えて

0

問題は、インデックスがunilist/orgwordsための指標としてwordsを形成使用していることです。

のは、問題を見てみましょう。ここでは

def PosText(): 
    global find 
    global pos 
    find = dict((sentence, words.index(sentence)+1) for sentence in   list(words)) 
    pos = (list(map(lambda sentence: find [sentence], words))) 
    return (pos) 

findは、リストwords内の位置にすべての単語をマッピングします。 (なぜ、wordsを反復する変数がsentenceと呼ばれていますか?)次に、すべての単語について、この位置が新しいリストに格納されます。このプロセスは、1行で表現することができますpos = [words.index(word)+1 for word in words]

あなたは今、あなたが見る、OutputDecompressedを見てみると:

for i in orgpos: 
    decompression.append(orgwords[i-1]) 

ここorgposposあるとorgwordsは、ユニークワードのリストです。現在、格納されているすべての索引は元の単語を戻すために使用されますが、orgwordsにアクセスするために使用されても、org3にはwordsの索引が含まれているため、この問題はありません。


この問題を解決するには、PosTextOutputDecompressedの一部を書き換えることである:

def PosText(): 
    global pos 
    pos = [unilist.index(word)+1 for word in words] 
    return pos 

def OutputDecompressed(): 
    newfile = open((compfilename)+'.txt', 'r') 
    saveddata = json.load(newfile) 
    orgpos = saveddata[1] 
    orgwords = saveddata[0] 
    print ("Unique words in the original sentence: " + str(orgwords) + "\nPosition of words in the sentence: " + str(orgpos)) 
    decompression = [] 
    # I could not figure out what this middle part was doing, so I left it out 
    for i in orgpos: 
     decompression.append(orgwords[i-1]) 
    finalsentence = (' '.join(decompression)) 
    print ("Original sentence from file: " + finalsentence) 

あなたのコードのいくつかのコメント:InputSentence()Validation()

  1. はに呼ばれるべきそれを確認する
  2. あなたはそれがあなたの代わりに、グローバル変数のパラメータを使用する必要がありcompfilenameなくfilename
  3. でなければなりませんValidation2()Validation2()なくValidation()
  4. を呼び出す必要があります。これは、関数が何をすべきかをより明確にする。たとえば、Uniquewordsは単語のリストを受け入れ、ユニークな単語のリストを返すことができます。また、現在では不可能なすべての機能を1つ1つテストするだけで、デバッグが簡単になります。あなたはこれがあなたのコードの機能を実装する私の方法だろうPEP 8
+0

に指定されたPythonのコーディングスタイルを使用することができ、それは他のPythonプログラマのための簡単にあなたのコードを読むために作るために

  • ます。https://gist.github .com/anonymous/5cc2861b1361791c6e00f21fb342cfae – BurningKarl

  • 関連する問題