2016-06-10 81 views
0

我正在創建一個代碼,我需要將一串單詞轉換爲數字,其中hi bye hi hello將變成0 1 0 2。我用字典來做到這一點,這就是爲什麼我在下一部分遇到麻煩。然後我需要將它壓縮成一個文本文件,然後解壓縮並重新構造成一個字符串。這是我難以忍受的一點。如何根據詞典的定義從詞典中找出單詞

我想做到這一點是通過壓縮的數字指標的方式,所以0 1 0 2位與詞典內容的文本文件,所以在文本文件中,將有0 1 0 2{hi:0, bye:1, hello:3}

現在我想做些什麼來解壓或閱讀到Python文件,使用指標(這是我將引用0 1 0 2從現在起),然後才考慮每一個詞了字典什麼並重建句子,所以如果出現了,它會查看字典,然後找到什麼有一個0定義,然後將其提取出來放入字符串中,因此它會找到hi並將其取出。

我希望這是可以理解的,並且至少有一個人知道如何去做,因爲我確信這是可能的,但是我一直無法在這裏或在網上找到任何提到這個主題的東西。

+0

你是什麼意思將其壓縮到文本文件?你是在談論將字典保存在文本文件中嗎? – Eular

+0

是的,我需要將文本的詞典和索引列表寫入文本文件 –

+4

看起來您希望我們爲您編寫一些代碼。儘管許多用戶願意爲遇險的編碼人員編寫代碼,但他們通常只在海報已嘗試自行解決問題時才提供幫助。證明這一努力的一個好方法是包含迄今爲止編寫的代碼,示例輸入(如果有的話),期望的輸出以及實際獲得的輸出(控制檯輸出,回溯等)。您提供的細節越多,您可能會收到的答案就越多。檢查[FAQ]和[問]。 –

回答

0

TheLazyScripter了針對該問題的解決辦法很好的解決方案,但運行時性能不好,因爲你在整個字典必須循環每個重建字。

我會說你選擇了錯誤的字典設計:爲了高效率,查找應該在一步完成,所以你應該有數字作爲鍵和單詞作爲項目。

由於您的問題,看起來像一個巨大的計算機科學功課(我會考慮我的學生;-)),我只給你解決方案的草圖:

  • 使用word in my_dict.values() #(adapt for py2/py3)測試這個詞是否已經在詞典中。
  • 如果否,insert the next available index作爲鍵和單詞作爲值。
  • 你完成了。
  • 對於重建這句話,只是
    • 通過你的號碼清單環
    • 使用數字在你的字典鍵和print(my_dict[key])
  • 準備異常處理的關鍵不在的情況下字典(如果你控制整個過程不應該發生,但這是很好的做法)。

該解決方案比您的方法更有效(且更易於實施)。

0

是的,你可以使用常規的詞典和列表來存儲數據。並使用jsonpickle將數據保存到磁盤。

import pickle 

s = 'hi hello hi bye' 
words = s.split() 
d = {} 
for word in word: 
    if word not in d: 
     d[word] = len(d) 

data = [d[word] for word in words] 

with open('/path/to/file', 'w') as f: 
    pickle.dump({'lookup': d, 'data': data}, f) 

然後讀回在

with open('/path/to/file', 'r') as f: 
    dic = pickle.load(f) 
    d = d['lookup'] 
reverse_d = {v: k for k, v in d.iteritems()} 
data = d['data'] 
words = [reverse_d[index] for index in data] 
line = ' '.join(words) 
print line 
0

因爲我不知道到底你有你的鍵盤佈局創造了我所能做的最好的就是猜測。在這裏,我創建了兩個函數,它們可以用於基於鍵盤映射將字符串寫入txt文件,並讀取txt文件並返回基於鍵盤映射的字符串。我希望這對你有用,或者至少讓你對這個過程有一個很好的理解!祝你好運!

import os 

def pack(out_file, string, conversion_map): 
    out_string = '' 
    for word in string.split(' '): 
     for key,value in conversion_map.iteritems(): 
      if word.lower() == value.lower(): 
       out_string += str(key)+' ' 
       break 
     else: 
      out_string += word+' ' 

    with open(out_file, 'wb') as file: 
     file.write(out_string) 

    return out_string.rstrip() 

def unpack(in_file, conversion_map, on_lookup_error=None): 
    if not os.path.exists(in_file): 
     return 

    in_file = ''.join(open(in_file, 'rb').readlines()) 
    out_string = '' 
    for word in in_file.split(' '): 
     for key, value in conversion_map.iteritems(): 
      if word.lower() == str(key).lower(): 
       out_string += str(value)+' ' 
       break 
     else: 
      if on_lookup_error: 
       on_lookup_error() 
      else: 
       out_string += str(word)+' ' 
    return out_string.rstrip() 

def fail_on_lookup(): 
    print 'We failed to find all words in our key map.' 
    raise Exception 

string = 'Hello, my first name is thelazyscripter' 
word_to_int_map = {0:'first', 
        1:'name', 
        2:'is', 
        3:'TheLazyScripter', 
        4:'my'} 

d = pack('data', string, word_to_int_map) #pack and write the data based on the conversion map 

print d #the data that was written to the file 
print unpack('data', word_to_int_map) #here we unpack the data from the file 
print unpack('data', word_to_int_map, fail_on_lookup)