2017-04-02 55 views
0

因此,我有一個程序在.txt文件中查看一個句子。程序然後找出句子中每個單詞的位置以及句子中的獨特單詞。這兩個列表在程序中輸出,然後程序嘗試根據句子中的唯一字和句子中單詞的位置重新創建.txt文件中的原始句,然後應該在程序中輸出。我目前使用的代碼如下所示:重新創建語句時出錯

import json 
import os.path 

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(compfilename + ".txt") == False: 
     print ("No such file exists. Please enter a valid existing file.") 
     Error() 

def OutputDecompressed(): 
    global words 
    global orgsentence 
    newfile = open((compfilename)+'.txt', 'r') 
    saveddata = json.load(newfile) 
    orgsentence = saveddata 
    words = orgsentence.split(' ') 
    print ("Words in the sentence: " + str(words)) 

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

def PosText(): 
    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 Error(): 
    MainCompression() 

def OutputDecompressed2(): 
    for number in pos: 
    decompression.append(orgsentence[int(number)-1]) 
    finalsentence = (" ".join(decompression)) 
    print ("Original sentence from file: " + finalsentence) 

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

def MainCompression(): 
    global decompression 
    decompression = [] 
    global unilist 
    unilist = [] 
    InputFile() 
    Validation2() 
    OutputDecompressed() 
    Uniquewords() 
    PosText() 
    OutputText() 
    OutputDecompressed2() 

MainCompression() 

現在描述一個示例測試。假設有一個名爲「ohdear」 .txt文件,包含了一句:「你好你好你好你好」

現在程序如下:

Please enter an existing compressed file to be decompressed: ohdear 
Words in the sentence: ['hello', 'hello', 'hello', 'hello'] 
Unique words: ['hello'] 
The positions of the word(s) in the sentence are: [1, 1, 1, 1] 
Original sentence from file: h h h h 

正如你所看到的,原句是不是從句子中獨特的詞語和位置重新創建 - 奇怪地顯示了4小時。有人可以幫助解決這個錯誤,因爲我不知道如何從句子中單詞的獨特單詞和位置重新創建原始句子。問題出在OutputDecompressed2()函數中。感謝您提前提供任何幫助。我一直停留在這一段時間...

回答

0
words = orgsentence.split(' ') 

這意味着words是一個字符串列表和orgsentence只是一個大的字符串。但後來你得到:

orgsentence[int(number)-1] 

這只是一個字符。寧可得到words[int(number)-1]

另外:

find = dict((sentence, words.index(sentence)+1) for sentence in list(words)) 

只是給你的每個 'sentence',因爲這就是.index做的第一次出現,因此你必須輸出:

The positions of the word(s) in the sentence are: [1, 1, 1, 1] 

這顯然是錯誤的。

而順便說一句,sentence是一個可怕的變量名稱在那一點上,爲什麼不word

+0

謝謝,它現在有用! – Kronixion