Image Path encode to Base64 -Json file - ios

i am building app where I retrieve list of menus using Json file:
I got poblem within my UITableview , the loading is too slow .
example of Json:
"Mon": [
{
"Type" : "Breakfast",
"Name":"Sweet Potato Breakfast Taquitos",
"Price" : "OMR 1.500",
"Image":"http://img203.imageshack.us/img203/4443/satbf1.jpg",
"Description": "olive oil, sweet potato , onions, jalapenos , cilantro , salt , pepper , shredded cheddar cheese , eggs , milk, corn tortilla , olive oil "
},
{
"Type" : "Lunch",
"Name":"Mexican Rice",
"Price" : "OMR 1.500",
"Image":"http://img21.imageshack.us/img21/4296/mlunch1.jpg",
"Description": "rice, chicken broth, butter, olive oil, onion, garlic, tomato paste, lime juice, cilantro, cumin, salt"
}
]
I found that I have to convert my image path into Base46 string ...(How can I do that automatically )?and how will be the formate of my new path ??
Any help?

1 - If you need to convert only the path into base64 you can use the php function base64_encode()
$img_path = 'http://img203.imageshack.us/img203/4443/satbf1.jpg';
$b64_img_path = base64_encode($img_path);
2 - If you need to convert the image data into base64 all you have to do is:
$img_path = 'http://img203.imageshack.us/img203/4443/satbf1.jpg';
$img_data = file_get_contents($img_path);
$b64_img = base64_encode($img_data);
3 - Use base64_decode() to decode you base64 string.
$b64_str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
$str = base64_decode($b64_str);

Related

Pandoc Lua filter to replace specific English words to Chinese words in citations

When writing a Chinese paper, both Chinese and English papers could be cited. However, styles are slightly differently. The example is as follows:
Cite an English article (Smith et al. 2022), and cite a Chinese article (张三 等 2018).
In other words, for papers with multiple authors, et al. is used for English papers, while 等 is applied for Chinese papers. Considering that Citation Style Language cannot handle multiple languages, I’d ask help for Lua filter.
A Markdown file named test.md as an example:
Cite an English article [#makarchev2022], and cite a Chinese article [#luohongyun2018].
Then run the command below:
pandoc -C -t native test.md
And the output of the main body:
[ Para
[ Str "Cite"
, Space
, Str "an"
, Space
, Str "English"
, Space
, Str "article"
, Space
, Cite
[ Citation
{ citationId = "makarchev2022"
, citationPrefix = []
, citationSuffix = []
, citationMode = NormalCitation
, citationNoteNum = 1
, citationHash = 0
}
]
[ Str "(Makarchev"
, Space
, Str "et"
, Space
, Str "al."
, Space
, Str "2022)"
]
, Str ","
, Space
, Str "and"
, Space
, Str "cite"
, Space
, Str "a"
, Space
, Str "Chinese"
, Space
, Str "article"
, Space
, Cite
[ Citation
{ citationId = "luohongyun2018"
, citationPrefix = []
, citationSuffix = []
, citationMode = NormalCitation
, citationNoteNum = 2
, citationHash = 0
}
]
[ Str "(\32599\32418\20113"
, Space
, Str "et"
, Space
, Str "al."
, Space
, Str "2018)"
]
, Str "."
]
Because #luohongyun2018 is a Chinese bibliography, I want to replace the last English et al. followed it, i.e.:
, Str "et"
, Space
, Str "al."
to an Chinese word 等:
, Str "\31561"
Is it possible to make it via Lua filter? Following the example in the Lua filter page, I have tried but didn’t make it by myself.
Any suggestions would be appreciated. Thanks in advance.
The filter below does two things: it checks if the citation text contains Chinese characters and, if so, then continues to to replace the et al..
The test for Chinese characters is a bit fragile; it could be made more robust by using the utf8.codepoint function from standard Lua library instead.
function Cite (cite)
return cite:walk{
Inlines = function (inlines)
local has_cjk = false
inlines:walk {
Str = function (s)
has_cjk = has_cjk or
pandoc.layout.real_length(s.text) > pandoc.text.len(s.text)
end
}
-- do nothing if this does not contain wide chars.
if not has_cjk then
return nil
end
local i = 1
local result = pandoc.Inlines{}
while i <= #inlines do
if i + 2 <= #inlines and
inlines[i].text == 'et' and
inlines[i+1].t == 'Space' and
inlines[i+2].text == 'al.' then
result:insert(pandoc.Str '等')
i = i + 3
else
result:insert(inlines[i])
i = i + 1
end
end
return result
end
}
end

how to load a vector of certrain word form word2vec saved model?

how can i find a respective words vector from previous trained word2vec model?
data = {'one': array([-0.06590105, 0.01573388, 0.00682817, 0.53970253, -0.20303348,
-0.24792041, 0.08682659, -0.45504045, 0.89248925, 0.0655603 ,
......
-0.8175681 , 0.27659689, 0.22305458, 0.39095637, 0.43375066,
0.36215973, 0.4040089 , -0.72396156, 0.3385369 , -0.600869 ],
dtype=float32),
'two': array([ 0.04694849, 0.13303463, -0.12208422, 0.02010536, 0.05969441,
-0.04734801, -0.08465996, 0.10344813, 0.03990637, 0.07126121,
......
0.31673026, 0.22282903, -0.18084198, -0.07555179, 0.22873943,
-0.72985399, -0.05103955, -0.10911274, -0.27275378, 0.01439812],
dtype=float32),
'three': array([-0.21048863, 0.4945509 , -0.15050395, -0.29089224, -0.29454648,
0.3420335 , -0.3419629 , 0.87303966, 0.21656844, -0.07530259,
......
-0.80034876, 0.02006451, 0.5299498 , -0.6286509 , -0.6182588 ,
-1.0569025 , 0.4557548 , 0.4697938 , 0.8928275 , -0.7877308 ],
dtype=float32),
'four': ......
}
now i want to obtain like
word = "one"
wordvector = data.get_vector(word)
and returns
[-0.06590105, 0.01573388, 0.00682817, 0.53970253, -0.20303348,
-0.24792041, 0.08682659, -0.45504045, 0.89248925, 0.0655603 ,
......
-0.8175681 , 0.27659689, 0.22305458, 0.39095637, 0.43375066,
0.36215973, 0.4040089 , -0.72396156, 0.3385369 , -0.600869 ]
one_array = data['one']
datais a dictionary. To get the value of a dictionary for a certain key, you call value = dict[key].
With one_list = data['one'].tolist(), you get the wordvector of the word 'one' as a list, which seems to be your expected output.

Convert month (date) into full month (string)

I have a date field (adate10) called Period with values like 07/02/2018.
I need to convert this into a string like "July 2, 2018."
I can use the code below to produce a string, Period_String, that says "JUL 2, 2018." I can't figure out how to format the month as the full month, e.g., "July." Is the only option to create an if/then statement that says 'if 1 then "January", if 2, then "February"', etc.? Was hoping there was a built in format but can't find it.
* extract each date element, then format.
compute mo = xdate.month(Period).
compute da = xdate.mday(Period).
compute yr = xdate.year(Period).
formats mo (month) da yr (F4.0).
execute.
* concatentate date elements as strings.
string Period_String (a30).
compute Period_String = concat(ltrim(string(mo,month)), " ", ltrim(string(da,F4)), ", ", ltrim(string(yr,F4))).
execute.
see the revised month format (month9 instead of month):
compute Period_String = concat(ltrim(string(mo,month9)), " ", ltrim(string(da,F4)), ", ", ltrim(string(yr,F4))).
EDIT:
using #mirirai's one-liner suggestion + getting only first letter in capitals:
string Period_String(a20).
COMPUTE Period_String = CONCAT(
RTRIM(char.substr(STRING(period,MONTH9),1,1)),
RTRIM(lower(char.substr(STRING(period,MONTH9),2)))," ",
LTRIM(STRING(XDATE.MDAY(Period),F2)), ", ",
LTRIM(STRING(XDATE.YEAR(period),F4))
).

IOS Swift 2D Array Sort

Using Swift i would like to sort the below array using the country name [0][1] instead of country code. Not sure how to achieve this.
let prefixCodes = [["AD", "Andorra"], ["AE", "United Arab Emirates"], ["AF", "Afghanistan"], ["AG", "Antigua & Barbuda"], ["AI", "Anguilla"], ["AL", "Albania"], ["AM", "Armenia"], ["AO", "Angola"], ["AQ", "Antarctica"], ["AR", "Argentina"], ["AS", "American Samoa"], ["AT", "Austria"], ["AU", "Australia"], ["AW", "Aruba"], ["AX", "Åland Islands"], ["AZ", "Azerbaijan"], ["BA", "Bosnia & Herzegovina"], ["BB", "Barbados"], ["BD", "Bangladesh"], ["BE", "Belgium"], ["BF", "Burkina Faso"], ["BG", "Bulgaria"], ["BH", "Bahrain"], ["BI", "Burundi"], ["BJ", "Benin"], ["BL", "St. Barthélemy"], ["BM", "Bermuda"], ["BN", "Brunei"], ["BO", "Bolivia"], ["BQ", "Caribbean Netherlands"], ["BR", "Brazil"], ["BS", "Bahamas"], ["BT", "Bhutan"], ["BV", "Bouvet Island"], ["BW", "Botswana"], ["BY", "Belarus"], ["BZ", "Belize"], ["CA", "Canada"], ["CC", "Cocos (Keeling) Islands"], ["CD", "Congo - Kinshasa"], ["CF", "Central African Republic"], ["CG", "Congo - Brazzaville"], ["CH", "Switzerland"], ["CI", "Côte d’Ivoire"], ["CK", "Cook Islands"], ["CL", "Chile"], ["CM", "Cameroon"], ["CN", "China"], ["CO", "Colombia"], ["CR", "Costa Rica"], ["CU", "Cuba"], ["CV", "Cape Verde"], ["CW", "Curaçao"], ["CX", "Christmas Island"], ["CY", "Cyprus"], ["CZ", "Czech Republic"], ["DE", "Germany"], ["DJ", "Djibouti"], ["DK", "Denmark"], ["DM", "Dominica"], ["DO", "Dominican Republic"], ["DZ", "Algeria"], ["EC", "Ecuador"], ["EE", "Estonia"], ["EG", "Egypt"], ["EH", "Western Sahara"], ["ER", "Eritrea"], ["ES", "Spain"], ["ET", "Ethiopia"], ["FI", "Finland"], ["FJ", "Fiji"], ["FK", "Falkland Islands"], ["FM", "Micronesia"], ["FO", "Faroe Islands"], ["FR", "France"], ["GA", "Gabon"], ["GB", "United Kingdom"], ["GD", "Grenada"], ["GE", "Georgia"], ["GF", "French Guiana"], ["GG", "Guernsey"], ["GH", "Ghana"], ["GI", "Gibraltar"], ["GL", "Greenland"], ["GM", "Gambia"], ["GN", "Guinea"], ["GP", "Guadeloupe"], ["GQ", "Equatorial Guinea"], ["GR", "Greece"], ["GS", "So. Georgia & So. Sandwich Isl."], ["GT", "Guatemala"], ["GU", "Guam"], ["GW", "Guinea-Bissau"], ["GY", "Guyana"], ["HK", "Hong Kong SAR China"], ["HM", "Heard & McDonald Islands"], ["HN", "Honduras"], ["HR", "Croatia"], ["HT", "Haiti"], ["HU", "Hungary"], ["ID", "Indonesia"], ["IE", "Ireland"], ["IL", "Israel"], ["IM", "Isle of Man"], ["IN", "India"], ["IO", "British Indian Ocean Territory"], ["IQ", "Iraq"], ["IR", "Iran"], ["IS", "Iceland"], ["IT", "Italy"], ["JE", "Jersey"], ["JM", "Jamaica"], ["JO", "Jordan"], ["JP", "Japan"], ["KE", "Kenya"], ["KG", "Kyrgyzstan"], ["KH", "Cambodia"], ["KI", "Kiribati"], ["KM", "Comoros"], ["KN", "St. Kitts & Nevis"], ["KP", "North Korea"], ["KR", "South Korea"], ["KW", "Kuwait"], ["KY", "Cayman Islands"], ["KZ", "Kazakhstan"], ["LA", "Laos"], ["LB", "Lebanon"], ["LC", "St. Lucia"], ["LI", "Liechtenstein"], ["LK", "Sri Lanka"], ["LR", "Liberia"], ["LS", "Lesotho"], ["LT", "Lithuania"], ["LU", "Luxembourg"], ["LV", "Latvia"], ["LY", "Libya"], ["MA", "Morocco"], ["MC", "Monaco"], ["MD", "Moldova"], ["ME", "Montenegro"], ["MF", "St. Martin"], ["MG", "Madagascar"], ["MH", "Marshall Islands"], ["MK", "Macedonia"], ["ML", "Mali"], ["MM", "Myanmar (Burma)"], ["MN", "Mongolia"], ["MO", "Macau SAR China"], ["MP", "Northern Mariana Islands"], ["MQ", "Martinique"], ["MR", "Mauritania"], ["MS", "Montserrat"], ["MT", "Malta"], ["MU", "Mauritius"], ["MV", "Maldives"], ["MW", "Malawi"], ["MX", "Mexico"], ["MY", "Malaysia"], ["MZ", "Mozambique"], ["NA", "Namibia"], ["NC", "New Caledonia"], ["NE", "Niger"], ["NF", "Norfolk Island"], ["NG", "Nigeria"], ["NI", "Nicaragua"], ["NL", "Netherlands"], ["NO", "Norway"], ["NP", "Nepal"], ["NR", "Nauru"], ["NU", "Niue"], ["NZ", "New Zealand"], ["OM", "Oman"], ["PA", "Panama"], ["PE", "Peru"], ["PF", "French Polynesia"], ["PG", "Papua New Guinea"], ["PH", "Philippines"], ["PK", "Pakistan"], ["PL", "Poland"], ["PM", "St. Pierre & Miquelon"], ["PN", "Pitcairn Islands"], ["PR", "Puerto Rico"], ["PS", "Palestinian Territories"], ["PT", "Portugal"], ["PW", "Palau"], ["PY", "Paraguay"], ["QA", "Qatar"], ["RE", "Réunion"], ["RO", "Romania"], ["RS", "Serbia"], ["RU", "Russia"], ["RW", "Rwanda"], ["SA", "Saudi Arabia"], ["SB", "Solomon Islands"], ["SC", "Seychelles"], ["SD", "Sudan"], ["SE", "Sweden"], ["SG", "Singapore"], ["SH", "St. Helena"], ["SI", "Slovenia"], ["SJ", "Svalbard & Jan Mayen"], ["SK", "Slovakia"], ["SL", "Sierra Leone"], ["SM", "San Marino"], ["SN", "Senegal"], ["SO", "Somalia"], ["SR", "Suriname"], ["SS", "South Sudan"], ["ST", "São Tomé & Príncipe"], ["SV", "El Salvador"], ["SX", "Sint Maarten"], ["SY", "Syria"], ["SZ", "Swaziland"], ["TC", "Turks & Caicos Islands"], ["TD", "Chad"], ["TF", "French Southern Territories"], ["TG", "Togo"], ["TH", "Thailand"], ["TJ", "Tajikistan"], ["TK", "Tokelau"], ["TL", "Timor-Leste"], ["TM", "Turkmenistan"], ["TN", "Tunisia"], ["TO", "Tonga"], ["TR", "Turkey"], ["TT", "Trinidad & Tobago"], ["TV", "Tuvalu"], ["TW", "Taiwan"], ["TZ", "Tanzania"], ["UA", "Ukraine"], ["UG", "Uganda"], ["UM", "U.S. Outlying Islands"], ["US", "United States"], ["UY", "Uruguay"], ["UZ", "Uzbekistan"], ["VA", "Vatican City"], ["VC", "St. Vincent & Grenadines"], ["VE", "Venezuela"], ["VG", "British Virgin Islands"], ["VI", "U.S. Virgin Islands"], ["VN", "Vietnam"], ["VU", "Vanuatu"], ["WF", "Wallis & Futuna"], ["WS", "Samoa"], ["YE", "Yemen"], ["YT", "Mayotte"], ["ZA", "South Africa"], ["ZM", "Zambia"], ["ZW", "Zimbabwe"]]
Thank you in advance.
With sort and the shorthand arguments you can order the second element in your 2D array like this:
let sortedAfterCountryNames = prefixCodes.sort{ $0[1] < $1[1] }
print(sortedAfterCountryNames)
// [["AF", "Afghanistan"], ["AL", "Albania"], ["DZ", "Algeria"], ["AS", "American Samoa"], ...
Hope this will help you.
let sortedArray = prefixCodes.sort { (first, second) -> Bool in
return first.last < second.last
}
You should use the sort method to sort your array.
Let's first create an unsorted array:
var arr = [["A", "Hello world"],
["C", "Bye world"],
["B", "idk what to say here"]]
Now let's look at what arguments the sort method needs. Apparently it needs a closure of the following signature:
([String], [String]) -> Bool
The most straightforward (but not so swifty) approach would be to write a function with that signature:
func myFunction (first: [String], second: [String]) -> Bool {
}
In that function, you need to decide whether the algorithm should swap the two arguments. If it should not, return true, otherwise false. I think if you want to sort in alphabetical order, you should just compare the two strings
return first[1] < second[1]
You access the element at index 1 because that is where you store the country names, right?
Then, call sort with the function
arr = arr.sort(myFunction)
However, that is not very swifty. To make this the most swifty, you can do this:
arr.sort {
$0[1] < $1[1]
}
Much better!
You may do the following:
prefixCodes.sort { $0[1] < $1[1] }
But to be safer, I think that you should define a Country struct like this:
struct Country {
let code: String
let name: String
init(_ code: String, _ name: String) {
self.code = code
self.name = name
}
}
let countries = [Country("AD", "Andorra"), Country("AE", "United Arab Emirates"), Country("BQ", "Caribbean Netherlands")]
print(countries.sort { $0.name < $1.name })
So that if you want to add more details to your countries, you will not have a chance to break your sort closure.
Or if you want to keep things simple but still enforce strong typing, you could use tuples:
let countries = [("AD", "Andorra"), ("AE", "United Arab Emirates"), ("BQ", "Caribbean Netherlands")]
print(countries.sort { $0.1 < $1.1 })
If you mix different tuple lengths in your array:
let countries = [("AD", "Andorra"), ("toto"), ("tata", "titi", "tutu")]
Swift will return this error:
error: type of expression is ambiguous without more context

Using filters in swift for checklist part in iOS application

We are working on an iOS application where we have recipes. In the first page of the app, we have list of ingredients. The user chooses the ingredients and the app is supposed to list the recipes that you can make with the ingredients chosen. We thought filters would be a good idea and we did this. Do you think there is a way we can improve the following code or what can we do for this scenario.
Also this code is giving error at the last line.
let array = [
    ["id":"Simple and healty salmon", "Ingr": "Salmon"],//in Simple and healty salmon,there is salmon only as ingridient.When salmon is checked ,Simple and healthy salmon recipe has to come out.
    ["id":"Boiled Eggs", "Ingr": "Egg"],//other recipies are as follows
    ["id":"Perfect Basic White Rice", "Ingr": "Rice"],
    ["id":"Baba Ghanoush", "Ingr": "Eggplant, Onions ,Garlic, Lemon, Parsley"],
    ["id":"Really Fudgy Brownies", "Ingr": "Butter, Choclate, Vanilla, Flour"],
    ["id":"Simple Healthy Summer Salad", "Ingr": "Lettuce, Spinach, Cucumbera, Tomato, Olive Oil"],
    ["id":"Fried Mashed Potato", "Ingr": "Potato, Onion"],
    ["id":"Gnocci", "Ingr": "Potato, egg, Flavor"],
    ["id":"Piyaz", "Ingr": "Potato, egg, Flour"],
    ["id":"Rice Puding", "Ingr": "Rice, Milk,"],
   
]
// there is an error at var newArray
var newArray = array.filter { $0["Ingr"] == "Rice" }//here only rice is checked.
print(newArray)
Instead of storing the ingredients as a comma separated String, you should be storing them as an Array<String>. For example, your entry for Gnocci, should be:
["id":"Gnocci", "Ingr": ["Potato", "Egg", "Flavor"]]
Then you could filter successfully. Here's a full example
let array = [
["id":"Simple and healty salmon", "Ingr": ["Salmon", "Egg"]],
["id":"Boiled Eggs", "Ingr": ["Egg"]],
["id":"Perfect Basic White Rice", "Ingr": ["Rice"]]
]
let results = array.filter { recipe in
if let ingredients = recipe["Ingr"] as? Array<String> {
return ingredients.indexOf("Egg") != nil
}
return false
}
results will contains only the first two items in the original array

Resources