0

I want to extract the data in form of a hash. The text is like this. Sometimes there is a single Arrest type and multiple charge and charge description and sometimes one Arrest type and one charge and charge description. Sometime multiple arrest type and multiple charge descriptions. I want an output like this from below text

   {  Arrest Type=>  Out Of County Warrant, charge => 11378 HS , Charge Description => POSS CNTL SUB FOR SALE  ,  Arrest Type=>  Out Of County Warrant,  charge => 11379(A) HS   , Charge Description => TRANSP/ETC CNTL SUB  }

   {  Arrest Type=>   Bench Warrant, charge => 1203.2 PC , Charge Description =>  PROB VIOL:REARREST/REVOKE   }

 { Arrest Type=>   On View, charge => 11364 HS   , Charge Description =>  CNTL SUB PARAPHERNALIA , 
   Arrest Type=>   On View, charge => 488 PC  , Charge Description =>  PETTY THEFT,
   Arrest Type=>   Out Of County Warrant,  charge =>  487(C) PC  , Charge Description =>  GRAND THEFT FROM PERSON  }

Arrest Type\n Out Of County Warrant\n\n Charge
Charge Description\n\n 11378 HS
POSS CNTL SUB FOR SALE\n\n Charge
Charge Description\n 11379(A) HS
TRANSP/ETC CNTL SUB\n\n Charge
Charge Description\n\n 978.5 PC
BENCH WARRANT:FTA:FELONY\n\n Name

Arrest Type\n\n Bench Warrant\n\n Charge
Charge Description\n 1203.2 PC
PROB VIOL:REARREST/REVOKE\n\n Name

Arrest Type\n\n On View\n\n Charge
Charge Description\n 11364 HS
CNTL SUB PARAPHERNALIA\n\n Charge
Charge Description\n\n 488 PC
PETTY THEFT\n\n Arrest Type\n Out Of County Warrant\n\n
Charge Charge Description\n\n
487(C) PC GRAND THEFT FROM PERSON\n\n\n\n
GC 6254(f)1\n Name

This is the piece of code that I have tried. How can I get the desired output?

total_page.scan(/Arrest Type\s+(.*?)\s+(Charge\s+(.*?)\s+Charge Description\s+(.*?)\s+|((?:Charge\s+(.*?)\s+Charge Description\s+(.*?)\s+)+)?)(?=Arrest Type|Name\z)/m).each do |match|
  arrest_type = match[0].strip
  charge_data = match[1].split(/\s{2,}/)

  (0...charge_data.length).step(2) do |i|
    new_hash = {
      "Arrest Type" => arrest_type,
      "charge" => charge_data[i],
      "Charge Description" => charge_data[i+1]
    }
    arrest_data << new_hash
  end
end

# print the resulting array of hashes
arrest_data.each do |arrest|
  p arrest
end
snoozy
  • 25
  • 5

0 Answers0