I am new here so please be gentle with me and I'm still just a newbie in programming and especially in ruby language.
This is a combo box and I want to know where it goes after I change it or what function it calls. Can you please tell me where it goes or what it does? thanks
<p><%= f.select :done_ratio, ((0..100).step(1).to_a.collect {|r| ["#{r} %", r] }), :required => #issue.required_attribute?('done_ratio') %></p>
It is creating an HTML select (dropdown) box, with the values: 0 %, 1 %, 2 %, ..., 100 %.
The field will be submitted as part of a form. It may or may not be a required field, depending on the value of #issue.required_attribute?('done_ratio'). (This is presumably a method in the Issue model, which can be found in ./app/models/issue.rb.)
Breaking it down:
(0..100) -- This is creating Range object, from 0 to 100 (inclusive).
.step(1) -- This is not actually needed; you could delete it. But it's saying "step through the range 1 at a time" (which is the default anyway). It converts the Range to an Enumerator.
.to_a -- This is not actually needed; you could delete it. This is converting the Enumerator to an Array.
.collect {|r| ["#{r} %", r] } -- This is mapping the Array to a new list of arrays, like: [["0 %", 0], ["1 %", 1], ..., ["100 %", 100]]. (This method would also work perfectly fine on a Range or Enumerator object, since both classes include this method from the Enumerable module. Hence why the above two steps can both be removed!)
f.select :done_ratio, (...) -- This is creating an HTML select element called done_ratio, with the above names/values.
Assume I have an array of Strings:
['Laptop Apple Macbook Air A1465, Core i7, 8Gb, 256Gb SSD, 15"Retina, MacOS' ... 'another device description']
I'd like to extract from this description features like:
item=Laptop
brand=Apple
model=Macbook Air A1465
cpu=Core i7
...
Should I prepare the pre-defined known features first? Like
brands = ['apple', 'dell', 'hp', 'asus', 'acer', 'lenovo']
cpu = ['core i3', 'core i5', 'core i7', 'intel pdc', 'core m', 'intel pentium', 'intel core duo']
I am not sure that I need to use CountVectorizer and TfidfVectorizer here, it's more appropriate to have DictVictorizer, but how can I make dicts with keys extracting values from the entire string?
is it possible with scikit-learn's Feature Extraction? Or should I make my own .fit(), and .transform() methods?
UPDATE:
#sergzach, please review if I understood you right:
data = ['Laptop Apple Macbook..', 'Laptop Dell Latitude...'...]
for d in data:
for brand in brands:
if brand in d:
# ok brand is found
for model in models:
if model in d:
# ok model is found
So creating N-loops per each feature? This might be working, but not sure if it is right and flexible.
Yes, something like the next.
Excuse me, probably you should correct the code below.
import re
data = ['Laptop Apple Macbook..', 'Laptop Dell Latitude...'...]
features = {
'brand': [r'apple', r'dell', r'hp', r'asus', r'acer', r'lenovo'],
'cpu': [r'core\s+i3', r'core\s+i5', r'core\s+i7', r'intel\s+pdc', r'core\s+m', r'intel\s+pentium', r'intel\s+core\s+duo']
# and other features
}
cat_data = [] # your categories which you should convert into numbers
not_found_columns = []
for line in data:
line_cats = {}
for col, features in features.iteritems():
for i, feature in enumerate(features):
found = False
if re.findall(feature, line.lower(), flags=re.UNICODE) != []:
line_cats[col] = i + 1 # found numeric category in column. For ex., for dell it's 2, for acer it's 5.
found = True
break # current category is determined by a first occurence
# cycle has been end but feature had not been found. Make column value as default not existing feature
if not found:
line_cats[col] = 0
not_found_columns.append((col, line))
cat_data.append(line_cats)
# now we have cat_data where each column is corresponding to a categorial (index+1) if a feature had been determined otherwise 0.
Now you have column names with lines (not_found_columns) which was not found. View them, probably you forgot some features.
We can also write strings (instead of numbers) as categories and then use DV. In result the approaches are equivalent.
Scikit Learn's vectorizers will convert an array of strings to an inverted index matrix (2d array, with a column for each found term/word). Each row (1st dimension) in the original array maps to a row in the output matrix. Each cell will hold a count or a weight, depending on which kind of vectorizer you use and its parameters.
I am not sure this is what you need, based on your code. Could you tell where you intend to use this features you are looking for? Do you intend to train a classifier? To what purpose?
I have a filter optimization problem in Redis.
I have a Redis SET which keeps the doc and pos pairs of a type in a corpus.
example:
smembers type_in_docs.1
result: doc.pos pairs
array (size=216627)
0 => string '2805.2339' (length=9)
1 => string '2410.14208' (length=10)
2 => string '3516.1810' (length=9)
...
Another redis set i create live according to user choices
It contains selected docs.
smembers filteredDocs
I want to filter doc.pos pairs "type_in_docs" set according to user Doc id choices.
In fact if i didnt use concat values in set it was easy with SINTER.
So i implement a php filter code as below.
It works but need an optimization.
In big doc.pairs set too much time need. (Nearly After 150000 members!)
$concordance= $this->redis->smembers('types_in_docs.'.$typeID);
$filteredDocs= $this->redis->smembers('filteredDocs');
$filtered = array_filter($concordance, function($pairs) use ($filteredDocs) {
if( in_array(substr($pairs, 0, strpos($pairs, '.')), $filteredDocs) ) return true;
});
I tried sorted set with scores as docId.
Bu couldnt find a intersect or filter option for score values.
I am thinking and searching a Redis based solution with supported keys, sets or Lua script for time optimization.
But nothing find.
How can i filter Redis sets with concat values?
Thanks for helps.
Your code is slow primarily because you're moving a lot of data from Redis to your PHP filter. The general motivation here should be perform as much filtering as possible on the server. To do that you'd need to pay some sort of price in CPU & RAM.
There are many ways to do this, here's one:
Ensure you're using Redis v2.8.9 or above.
To allow efficiently looking for doc only, keep your doc.pos pairs as is but use Sorted Sets with score = 0, your e.g.:
ZADD type_in_docs.1 0 2805.2339 0 2410.14208 0 3516.1810
This will allow you to mimic SISMEMBER for doc in the set with:
ZRANGEBYLEX type_in_docs.1 [<$typeID> (<$typeID + "\xff">
You can now just SMEMBERS on the (usually) smaller filterDocs set and then call ZRANGEBYLEX on each for immediate gains.
If you want to do better - in extreme cases (i.e. large filterDocs, small type_in_docs) you should do the reverse.
If you want to do even better, use Lua to wrap up the filtering logic - something like:
-- #usage: redis-cli --filter_doc_pos.lua <filter set keyname> <type pairs keyname>
-- #returns: list of matching doc.pos pairs
local r = {}
for _, fv in pairs(redis.call("SMEMBERS", KEYS[1])) do
local t = redis.call("ZRANGEBYLEX", KEYS[2], "[" .. fv , "(" .. fv .. "\xff")
for _, tv in pairs(t) do
r[#r+1] = tv
end
end
return r
I'm coding a Google sketchup plugin with Ruby, and I faced a little problem. I have an array containing descriptions of every point like:
desc_array = ["anna ", "anna 45", "anna689", "anna36", "anna 888", "anna ",...]
The array containing every point's coordinates is:
todraw_su = [
[-16.23317, -16.530533, 99.276929],
[-25.142825, -12.476601, 99.237414],
[-32.716122, -5.92341, 99.187951],
[-38.964589, 4.181119, 99.182358],
[-41.351064, 18.350418, 99.453714],
[-40.797511, 33.987519, 99.697253],
...
]
I want to add a text in Google sketchup for each point. According to the Sketchup API this can be done by:
Sketchup.active_model.entities.add_text "This is the text", [x, y, z]
I tried:
todraw_su.each {|todraw| desc_array.each {|desc| Sketchup.active_model.entities.add_text desc,todraw }}
But, it gave me something unexpected, as it returns all the elements in desc_array for every element in to_draw.
What I want is every element in desc_array for every element in to_draw.
[desc_array, todraw_su].transpose.each do |desc, coord|
# ...
end
You can also do this with #zip like...
desc_array.zip(todraw_su).each do |desc, coord|
# ...
end
With the #zip technique, the result will always have the first array's dimension, with the second truncated or nil-padded as needed. This may or may not be TRT. Transpose will raise IndexError in that case.
So I have been struggling with an issue for a while now and seem to be getting nowhere...
The problem exists while I am looping through a hash, around the 3rd level deep.
So my code looks a little like:
request[:items][:location_item].each do |locaton_item|
pp location_item[:name]
location_item[:items][:sub_area_option_item].each do |sub_area_option_item|
pp sub_area_option_item[:name]
sub_area_option_item[:items][:option_item].each do |option_item|
option_item[:name] <- THIS IS WHERE MY CODE BREAKS
end
end
end
The reason this is failing is because this particular part of the loop has 2 different variants, one where there is an infinite amount of "option_items" and one where there is only one "option_item". Instead of sending me back the single "option_item" it is looping through this "option_item" and not finding this particular field. Any ideas as how to force it when only one object is found to still return it as an array?
UPDATE(05/092012)
TypeError - Symbol as array index
This error does not appear if I remove the single "option_item" from the hash or even by filtering it with a conditional statement.
As another update that was an example before, here is the code:
request[:items][:house_option_item][:items][:location_option_item].each do |location_option_item|
pp location_option_item.try(:[], :location).try(:[], :name)
# pp location_option_item[:location][:name]
location_option_item[:items][:sub_area_option_item].each do |sub_area_option_item|
pp "--" + sub_area_option_item[:sub_area][:name] + "--" + sub_area_option_item[:sub_area][:id]
count = 0
# pp sub_area_option_item[:items].length
# pp "+++++++++++++++++"
# if sub_area_option_item[:sub_area][:name] != "Conservatory"
[sub_area_option_item[:items][:option_item]].each do |option_item_key_info|
pp "----"
pp option_item_key_info[:usage_name]
end
# end
end
end
Example data(abbreviated):
{:"#xmlns:i"=>"http://www.w3.org/2001/XMLSchema-instance",
:"#xmlns:a"=>"http://schemas.datacontract.org/2004/07/RXTypes",
:items=>
{:house_option_item=>
{:design=>
{:house_style=>nil,
:sales_name=>nil,
:sq_ft=>"0",
:"#xmlns:b"=>"http://schemas.datacontract.org/2004/07/",
:arch_ref=>"D3H102.1",
:marketing_name=>"Warwick",
:num_beds=>"0"},
:items=>
{:location_option_item=>
[{:location=>{:sort_order=>"0", :name=>"Eat", :id=>"1"},
:items=>
{:sub_area_option_item=>
[{:sub_area=>
{:location_id=>"7",
:sort_order=>"0",
:name=>"Conservatory",
:id=>"31"},
:items=>
{:option_item=>
{:lead_days=>"0",
:collection_code=>"NEWHE01",
:usage_name=>"Conservatory",
:max_qty=>"1",
:is_a_bundle=>false,
:only_order_bundle=>false,
:sub_area=>"Conservatory",
:key_info=>
{:specification=>
"PVC-U 'Edwardian' style Conservatory in white with high performance toughened glass double glazed windows and 600mm (approx) dwarf base wall internally and externally. Conservatory to include fan and light, electric heater, double power point and ceramc floor tiling selected from tile choice . Size of Conservatory to be as follows: 3.2 x 3.1 metre (Availabil
ity will be subject to ground levels/conditions)",
:description=>"Conservatory",
:image_url=>"/2012/8/10/334_web.jpg",
:item_id=>"3",
:thumbnail_url=>"/2012/8/10/334_thumb.jpg"},
:build_stage_id=>"0",
:location=>"Building",
:option_flag=>"67",
:bundle_items=>nil,
:price=>"8950",
:choice_items=>nil,
:spec_level=>"85"}}},
{:sub_area=>
{:location_id=>"7",
:sort_order=>"0",
:name=>"Doors",
:id=>"32"},
:items=>
{:option_item=>
[{:lead_days=>"0",
:collection_code=>"NEWHE01",
:usage_name=>"Front Door",
:max_qty=>"0",
:is_a_bundle=>false,
:only_order_bundle=>false,
:sub_area=>"Doors",
:key_info=>
{:specification=>
"GRP pre-finished door with patterned glass manufactured by IG. Style of door to be all as indicated on house plan, finished in solid colour externally & white finish internally. Frame to be UPVC. Please refer to the External Finishing schedule to confirm the external finish selected for each property. The paint finish to be one of the New Heritage Colours - Camouflage beige
- BS381 389, Dk camouflage desert sand - BS381 420, Wine Red - RAL 3005, Braemar - BS4800 - 14C35, Deep Brunswick Green - BS381 227 and White. Door complete with lever handle furniture, multi point lock system, sleeved letterplate, viewer and door chain in black japanned finish externally & chrome lever handle furniture internally.",
:description=>"GRP Front door",
:image_url=>nil,
:item_id=>"4",
:thumbnail_url=>nil},
:build_stage_id=>"0",
:location=>"Building",
:option_flag=>"88",
:bundle_items=>nil,
:price=>"0",
:choice_items=>
{:option_item_key_info=>
[{:specification=>nil,
:description=>"NG42 Diamond",
:image_url=>"/2012/8/21/485_web.jpg",
:item_id=>"211",
:thumbnail_url=>"/2012/8/21/485_thumb.jpg"},
{:specification=>nil,
:description=>"NG21 Square",
:image_url=>"/2012/8/21/486_web.jpg",
:item_id=>"212",
:thumbnail_url=>"/2012/8/21/486_thumb.jpg"},
{:specification=>nil,
:description=>"NG51 Square",
:image_url=>"/2012/8/21/487_web.jpg",
:item_id=>"213",
:thumbnail_url=>"/2012/8/21/487_thumb.jpg"},
{:specification=>nil,
:description=>"NG42 Rectangle",
:image_url=>"/2012/8/21/488_web.jpg",
:item_id=>"214",
:thumbnail_url=>"/2012/8/21/488_thumb.jpg"}]},
:spec_level=>"83"},
{:lead_days=>"0",
:collection_code=>"NEWHE01",
:usage_name=>"Front Door",
:max_qty=>"0",
:is_a_bundle=>false,
:only_order_bundle=>false,
:sub_area=>"Doors",
:key_info=>
{:specification=>
"GRP pre-finished door with patterned glass manufactured by IG. Style of door to be all as indicated on house plan, finished in solid colour externally & white finish internally. Frame to be UPVC. Please refer to the External Finishing schedule to confirm the external finish selected for each property. The paint finish to be one of the New Heritage Colours - Camouflage beige
- BS381 389, Dk camouflage desert sand - BS381 420, Wine Red - RAL 3005, Braemar - BS4800 - 14C35, Deep Brunswick Green - BS381 227 and White. Door complete with lever handle furniture, multi point lock system, sleeved letterplate, viewer and door chain in black japanned finish externally & chrome lever handle furniture internally.",
:description=>"GRP Front door",
:image_url=>nil,
:item_id=>"4",
:thumbnail_url=>nil},
:build_stage_id=>"0",
:location=>"Building",
:option_flag=>"88",
:bundle_items=>nil,
:price=>"0",
:choice_items=>
{:option_item_key_info=>
[{:specification=>nil,
:description=>"NG42 Diamond",
:image_url=>"/2012/8/21/485_web.jpg",
:item_id=>"211",
:thumbnail_url=>"/2012/8/21/485_thumb.jpg"},
{:specification=>nil,
:description=>"NG21 Square",
:image_url=>"/2012/8/21/486_web.jpg",
:item_id=>"212",
:thumbnail_url=>"/2012/8/21/486_thumb.jpg"},
{:specification=>nil,
:description=>"NG51 Square",
:image_url=>"/2012/8/21/487_web.jpg",
:item_id=>"213",
:thumbnail_url=>"/2012/8/21/487_thumb.jpg"},
{:specification=>nil,
:description=>"NG42 Rectangle",
:image_url=>"/2012/8/21/488_web.jpg",
:item_id=>"214",
:thumbnail_url=>"/2012/8/21/488_thumb.jpg"}]},
:spec_level=>"83"},
{:lead_days=>"0",
:collection_code=>"NEWHE01",
:usage_name=>"Front Door",
:max_qty=>"0",
:is_a_bundle=>false,
:only_order_bundle=>false,
:sub_area=>"Doors",
:key_info=>
{:specification=>
"GRP pre-finished door with patterned glass manufactured by IG. Style of door to be all as indicated on house plan, finished in solid colour externally & white finish internally. Frame to be UPVC. Please refer to the External Finishing schedule to confirm the external finish selected for each property. The paint finish to be one of the New Heritage Colours - Camouflage beige
- BS381 389, Dk camouflage desert sand - BS381 420, Wine Red - RAL 3005, Braemar - BS4800 - 14C35, Deep Brunswick Green - BS381 227 and White. Door complete with lever handle furniture, multi point lock system, sleeved letterplate, viewer and door chain in black japanned finish externally & chrome lever handle furniture internally.",
:description=>"GRP Front door",
:image_url=>nil,
:item_id=>"4",
:thumbnail_url=>nil},
:build_stage_id=>"0",
:location=>"Building",
:option_flag=>"88",
:bundle_items=>nil,
:price=>"0",
:choice_items=>
{:option_item_key_info=>
[{:specification=>nil,
:description=>"NG42 Diamond",
:image_url=>"/2012/8/21/485_web.jpg",
:item_id=>"211",
:thumbnail_url=>"/2012/8/21/485_thumb.jpg"},
{:specification=>nil,
:description=>"NG21 Square",
:image_url=>"/2012/8/21/486_web.jpg",
:item_id=>"212",
:thumbnail_url=>"/2012/8/21/486_thumb.jpg"},
{:specification=>nil,
:description=>"NG51 Square",
:image_url=>"/2012/8/21/487_web.jpg",
:item_id=>"213",
:thumbnail_url=>"/2012/8/21/487_thumb.jpg"},
{:specification=>nil,
:description=>"NG42 Rectangle",
:image_url=>"/2012/8/21/488_web.jpg",
:item_id=>"214",
:thumbnail_url=>"/2012/8/21/488_thumb.jpg"}]},
:spec_level=>"83"},
Krista's example will work if there is only one option_item, but not if there's more. I'd add flatten and adjust it to:
[sub_area_option_item[:items][:option_item]].flatten.each do |option_item|
option_item[:name]
end
the [] around it makes sure it's an array. the flatten makes sure that if it already was an array, that you remove the extra layer of [] eg:
['a'].flatten
=> ["a"]
[['a','b','c',]].flatten
=> ["a", "b", "c"]
As far as I can tell, the second part of your loop should be the problem - given your source code and the sample data
01: request[:items][:house_option_item][:items][:location_option_item].each do |location_option_item|
02: location_option_item[:items][:sub_area_option_item].each do |sub_area_option_item|
03: count = 0
04: # if sub_area_option_item[:sub_area][:name] != "Conservatory"
05: [sub_area_option_item[:items][:option_item]].each do |option_item_key_info|
06: pp option_item_key_info[:usage_name]
07: end
08: # end
09: end
10: end
Note that line 01 would end up accessing location_option_item which in your sample above is:
:location_option_item=>
[{:location=>{:sort_order=>"0", :name=>"Eat", :id=>"1"},
:items=>
{:sub_area_option_item=>
[{:sub_area=>
{:location_id=>"7",
:sort_order=>"0",
:name=>"Conservatory",
:id=>"31"},
:items=>
{:option_item=>
{:lead_days=>"0",
:collection_code=>"NEWHE01",
........ etc .......
Notice that location_option_item is the key, which has for value an array.
Thus, my guess is that this line is the problem (02): location_option_item[:items]
Since location_option_item is an array, you cannot access the :items symbol from it.
If you wanted to get items it would be something more like:
location_option_item[1][:items]