2013-02-19 167 views
0

我使用紅寶石,試圖解析的形式爲文本文件的字典文本文件...解析紅寶石

AAB eel bbc 
ABA did eye non pap mom ere bob nun eve pip gig dad nan ana gog aha 
    mum sis ada ava ewe pop tit gag tat bub pup 
    eke ele hah huh pep sos tot wow aba ala 
    bib dud tnt 
ABB all see off too ill add lee ass err xii ann fee vii inn egg odd bee dee goo 
    woo cnn pee fcc tee wee ebb edd gee ott ree vee ell orr rcc att boo cee cii 
    coo kee moo mss soo doo faa hee icc iss itt kii loo mee nee nuu ogg opp pii 
    tll upp voo zee 

我需要能夠通過第一列進行搜索,如「 AAB「,然後搜索與該密鑰關聯的所有值。我試圖將文本文件導入數組的散列,但永遠不會超過第一個存儲的值。我對如何搜索文件沒有興趣,無論是將數據存儲到某個數據結構中,還是每次只搜索文本文件,我都需要能夠做到。我對如何繼續這一點感到茫然,任何幫助將不勝感激。由於

-amc25114

回答

3

這將讀取你的字典文件。我將內容存儲在一個字符串中,然後 將它變成一個StringIO對象,讓我假裝它是一個文件。您可以使用 File.readlines直接從文件本身的讀取:

require 'pp' 
require 'stringio' 

text = 'AAB eel bbc 
ABA did eye non pap mom ere bob nun eve pip gig dad nan ana gog aha 
    mum sis ada ava ewe pop tit gag tat bub pup 
    eke ele hah huh pep sos tot wow aba ala 
    bib dud tnt 
ABB all see off too ill add lee ass err xii ann fee vii inn egg odd bee dee goo 
    woo cnn pee fcc tee wee ebb edd gee ott ree vee ell orr rcc att boo cee cii 
    coo kee moo mss soo doo faa hee icc iss itt kii loo mee nee nuu ogg opp pii 
    tll upp voo zee 
' 

file = StringIO.new(text) 

dictionary = Hash[ 
    file.readlines.slice_before(/^\S/).map{ |ary| 
    key, *values = ary.map(&:strip).join(' ').split(' ') 
    [key, values] 
    } 
] 

dictionary是一個哈希看起來像:

{ 
    "AAB"=>[ 
    "eel", "bbc" 
    ], 
    "ABA"=>[ 
    "did", "eye", "non", "pap", "mom", "ere", "bob", "nun", "eve", "pip", 
    "gig", "dad", "nan", "ana", "gog", "aha", "mum", "sis", "ada", "ava", 
    "ewe", "pop", "tit", "gag", "tat", "bub", "pup", "eke", "ele", "hah", 
    "huh", "pep", "sos", "tot", "wow", "aba", "ala", "bib", "dud", "tnt" 
    ], 
    "ABB"=>[ 
    "all", "see", "off", "too", "ill", "add", "lee", "ass", "err", "xii", 
    "ann", "fee", "vii", "inn", "egg", "odd", "bee", "dee", "goo", "woo", 
    "cnn", "pee", "fcc", "tee", "wee", "ebb", "edd", "gee", "ott", "ree", 
    "vee", "ell", "orr", "rcc", "att", "boo", "cee", "cii", "coo", "kee", 
    "moo", "mss", "soo", "doo", "faa", "hee", "icc", "iss", "itt", "kii", 
    "loo", "mee", "nee", "nuu", "ogg", "opp", "pii", "tll", "upp", "voo", "zee" 
    ] 
} 

您可以查找使用鍵:

 
dictionary['AAB'] 
=> ["eel", "bbc"] 

而且在陣列內使用include?進行搜索:

 
dictionary['AAB'].include?('eel') 
=> true 
dictionary['AAB'].include?('foo') 
=> false 
0
class A 

    def initialize 
    @h, key = readlines.inject({}) do |m, s| 
     a = s.split 
     m[key = a.shift] = [] if s =~ /^[^\s]/ 
     m[key] += a 
     m 
    end 
    end 

    def lookup k, v # not sure what you really want to do here 
    p [k, v, (@h[k].index v)] 
    end 

    self 
end.new.lookup 'ABA', 'wow' 
0

我的2美分:

file = File.open("/path_to_file_here") 
recent_key = "" 
results = Hash.new 
while (line = file.gets) 
    key = line[/[A-Z]+/] 
    recent_key = key if key 
    line.scan(/[a-z]+/).each do |val| 
    results[recent_key.to_sym] = [] if !results[recent_key.to_sym] 
    results[recent_key.to_sym] << val 
    end 
end 
puts results 

這會給你此輸出中:

 
{ 

:AAB=>["eel", "bbc"], 

:ABA=>["did", "eye", "non", "pap", "mom", "ere", "bob", "nun", "eve", "pip", "gig", "dad", "nan", "ana", "gog", "aha", "mum", "sis", "ada", "ava", "ewe", "pop", "tit", "gag", "tat", "bub", "pup", "eke", "ele", "hah", "huh", "pep", "sos", "tot", "wow", "aba", "ala", "bib", "dud", "tnt"], 

:ABB=>["all", "see", "off", "too", "ill", "add", "lee", "ass", "err", "xii", "ann", "fee", "vii", "inn", "egg", "odd", "bee", "dee", "goo", "woo", "cnn", "pee", "fcc", "tee", "wee", "ebb", "edd", "gee", "ott", "ree", "vee", "ell", "orr", "rcc", "att", "boo", "cee", "cii", "coo", "kee", "moo", "mss", "soo", "doo", "faa", "hee", "icc", "iss", "itt", "kii", "loo", "mee", "nee", "nuu", "ogg", "opp", "pii", "tll", "upp", "voo", "zee"] 

}