wm: irc-h2o-bot

ref: 391dd56eacb294363687cb73b80c9d8ef6dc650d
dir: /Test_JSON.rb/

View raw version
require 'json'

# JSON string to be parsed
#json_string = 'data:{"name": "John", "age": 30, "city": "New York"}'

# Parse the JSON string

#test = json_string.sub('data:', '')

# parsed_json = JSON.parse(test)
#
# # Access the values from the parsed JSON
# name = parsed_json['name']
# age = parsed_json['age']
# city = parsed_json['city']
#
# # Output the values
# puts "Name: #{name}"
# puts "Age: #{age}"
# puts "City: #{city}"



data_string = 'data:{"token":{"id":9856,"text":"Hello","logprob":0.0,"special":false},"generated_text":null,"details":null} data:{"token":{"id":9856,"text":"Hello","logprob":0.0,"special":false},"generated_text":null,"details":null}'

m = 'data:{"token":{"id":9856,"text":"Hello","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":12,"text":"!","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":1265,"text":" How","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":418,"text":" can","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":295,"text":" I","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":733,"text":" help","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":299,"text":" you","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":1722,"text":" today","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":42,"text":"?","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":11,"text":"<|endoftext|>","logprob":0.0,"special":true},"generated_text":"Hello! How can I help you today?","details":null}';



# Split the data string into individual JSON strings
json_strings = m.split('\n')

# Initialize an array to store the parsed JSON objects
parsed_json_objects = []

# Iterate over each JSON string and parse it
json_strings.each do |json_str|
  if json_str.start_with?('data:')
    # Remove the leading "data:" prefix
    json_data = json_str[5..-1]

    # Parse the JSON string and add it to the parsed_json_objects array
    parsed_json_objects << JSON.parse(json_data)
  end
end

# Print the parsed JSON objects
parsed_json_objects.each do |json_obj|
  # puts JSON.pretty_generate(json_obj)
  print json_obj['token']['text']
end




data_string = 'data:{"token":{"id":9856,"text":"Hello","logprob":0.0,"special":false},"generated_text":null,"details":null} data:{"token":{"id":9856,"text":"Hello","logprob":0.0,"special":false},"generated_text":null,"details":null}'

# Split the data_string on whitespace followed by "data:" to separate each JSON string
json_strings = data_string.scan(/data:(\{.*?\})/)

parsed_data = []

json_strings.each do |json_str|
  # Prepend "data:" to the JSON string since it was removed during splitting
  # json_str = "data:" + json_str

  # Parse the JSON string
  parsed_data << JSON.parse(json_str)
end

# Now you have an array of parsed JSON objects
parsed_data.each do |data|
  puts data.inspect
end



data_string = 'data:{"token":{"id":9856,"text":"Hello","logprob":0.0,"special":false},"generated_text":null,"details":null} data:{"token":{"id":9856,"text":"Hello","logprob":0.0,"special":false},"generated_text":null,"details":null}'

m = 'data:{"token":{"id":9856,"text":"Hello","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":12,"text":"!","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":1265,"text":" How","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":418,"text":" can","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":295,"text":" I","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":733,"text":" help","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":299,"text":" you","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":1722,"text":" today","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":42,"text":"?","logprob":0.0,"special":false},"generated_text":null,"details":null}\n'+
  'data:{"token":{"id":11,"text":"<|endoftext|>","logprob":0.0,"special":true},"generated_text":"Hello! How can I help you today?","details":null}';

results = data_string.scan(/data:(\{.*?\})/)

parsed_data = results.map { |item| JSON.parse(item[0]) }

parsed_data.each do |data|
  puts "Name: #{data['token']['text']}"
  # puts "Age: #{data['age']}"
  # puts "City: #{data['city']}"
end