Skip to content
This repository

Hello, I made CurrencyIn.pm module for you. #64

Merged
merged 6 commits into from about 2 years ago

5 participants

Alchymista Robert Picard Gabriel Weinberg Torsten Raudssus Dylan Lloyd
Alchymista

When user type:
currency in {country}
then result is found and returned from hash table (now it is included inside module)

I know it requires some upgrades but this is my first day with Perl =)
so anyone feel free to do updates in it...

Module is based on this request:
https://duckduckhack.uservoice.com/forums/5168-plugins/suggestions/2678913-currency-in-x-tells-you-the-currency-in-x-countr

Pavel

Robert Picard

Cool Goodie! You should put the hash in the Perl file though. See here for an example of that.

Alchymista

Hello, thanks for comment.

No problem, I can put that hash inside Perl file. I am just unsure if it should be embed or loaded from external file.
Because I was thinking that those information (parsed from wikipedia) may be updated regularly (e.g. once a year =) and it is easier to do when in external file (parser is already coded in Python)
But I am open for suggestion. If it is really no problem then I can put it in the Perl file (it is easier solution anyway =).

Pavel

Robert Picard

I'd just put it in the Goodie but someone else might disagree.

Alchymista

OK, seem you thing it is better to put that hash into Perl file... so I am starting to work on it =)

Alchymista

Hash table with countries is now included in Perl file and it works fine on my computer so hopefully someone will accept my pull request :)

Robert Picard

Cool. Just a couple of other suggestions.

You might want to move the hash outside of the subroutine so it isn't re-generated every time it's called.

Also, what if the user types "currency of" instead of "currency in"? You might want to use =~ with a regex to find the country they want instead of using substr(). That way, you could just search the whole thing for a pattern like: /(in\s*.*|of\s*.*|for\s*.*)/ (probably not the right pattern to use but you get the idea) and not care about what came before it, e.g. what is the currency used in America or what currency will I need for Russia? (note: the latter case shows that you might also consider stripping the match of punctuation before checking the hash).

If you did decide to do that you'd probably want to change the trigger to any.

Alchymista

Great, thank you for suggestions =)

I get some sleep first and will take a look on it, tomorrow.

Gabriel Weinberg
Owner
yegg commented April 15, 2012

I think you are right -- it would be best to read in the output file so it could be updated easily at a later point. Also, you can add the python generation file right in the same share directory so it is easy to regenerate.

You should be able to read in the hash like this:

my %currency = share('currency.txt')->slurp

That would read one line in as a key, and the next as a value. Check out how the passphrase goodie for an example (though it uses an array).

Alchymista

So far I have two issues with "my %currency = share('currency.txt')->slurp" way of reading hash file:

1) it reads values with the end of the line for keys and values. So when I search for '$currency{"slovakia"}' then no results, because I must search for "slovakia\n" with end of the line included...

2) I have problem to pass value as array when share function reads '["Euro (EUR)", "Dolar (USD)"]' then it keeps that line as string not an array =(

It will be much appreciated if anyone can help =)

Pavel

btw: 'currency.txt' at the moment looks like this:
abkhazia
["Russian ruble (RUB)"]
afghanistan
["Afghan afghani (AFN)"]
zimbabwe
["Botswana pula (BWP)","British pound (GBP)","Euro (EUR)","South African rand (ZAR)","United States dollar (USD)"]
...

Robert Picard

@Alchymista In response to your second point you could make it a string where the values are separated by something like a semicolon so it looks like Zimbabwe => "Botswana pula (BWP):Euro (EUR):Dollar (USD)" and use my @arr = split(/:/, $currency{"simbabwe"}) to split it into an array.

Torsten Raudssus
Owner
Getty commented April 17, 2012

You should consider switch this to YAML https://metacpan.org/module/YAML

Alchymista

Thank you @rpicard
Not sure how I could forgot about so simple solution (probably because I wanted to store it like array so much...)

@Getty ... thanks for introducing YAML module, but I am quite new to Perl (all together I spent like three days with it) so for the moment I stick with my current solution (it is working =) ...
But if you think that with YAML it will be really, really cool then feel free to take all my code or redo whole CurrencyIn module if you..
(for me this is sort of exercise because I wanted to try what it is about "to be involved in opensource" =)

Torsten Raudssus
Owner
Getty commented April 17, 2012

you can definitly do that yourself :) its a good mission/training :) thanks for the code! :)

Robert Picard

@Alchymista I was just testing out your Goodie and have a few things for you to look into.

  • There is a new line in the response. I think chomp() might work but there is some stuff in that page about using it with slurp so you might need to use $var =~ s/\n//g; or something similar to strip the new line.
  • Maybe make the response more like a sentence, i.e. instead of saying Currency in Andorra is Euro (EUR) return The currency in Andorra is the Euro (EUR)..

It's coming along really well!

Alchymista

Thank you for help and support :)

Robert Picard

No problem! Thanks for putting so much work into it.

Dylan Lloyd nospampleasemam merged commit 4d01ce2 into from April 21, 2012
Dylan Lloyd nospampleasemam closed this April 21, 2012
Dylan Lloyd
Collaborator

Merged :-) Thanks again for contributing to DuckDuckGo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
300  lib/DDG/Goodie/CurrencyIn.pm
@@ -3,14 +3,14 @@ package DDG::Goodie::CurrencyIn;
3 3
 
4 4
 # TODO: At the moment it return value only if user inputs the whole country name...
5 5
 #     ...if user types "Salvador" instead of "El Salvador" then no results...
6  
-# TODO: think about how often currency in countries changes - what then? 
7  
-#     Parse text (parser already build) from Wikipedia and manually copy paste?
  6
+# TODO: think about how often currency in countries changes? 
  7
+#     Parser (for Wikipedia) is included in share directory...
8 8
 
9  
-# In some countries there are more than one currency
10  
-# For that reason values in hash are stored as arrays. Example:
11  
-# %countries( "Zimbabwe"=>["A","B"], "Slovakia"=>["A"], ... )"
  9
+# In some countries there are more than one currency.
  10
+# For that reason values in hash are stored as arrays. (loaded from .txt as comma separated values )
  11
+# Example: %countries( "Zimbabwe"=>["A","B"], "Slovakia"=>["A"], ... )"
12 12
 
13  
-# Working examples:
  13
+# Working examples for queries:
14 14
 # What currency do I need in Egypt ?
15 15
 # What currency will I need for Zimbabwe
16 16
 # What is the currency used in Slovakia
@@ -19,288 +19,50 @@ package DDG::Goodie::CurrencyIn;
19 19
 
20 20
 use DDG::Goodie;
21 21
 
22  
-triggers any => 'currency';			# User typed currency...
  22
+triggers any => 'currency';	# User typed currency...
23 23
 
24  
-# Hash with all countries and currencies is now included in file
25  
-# There is already build parser (in Python) to generate it from Wikipedia
26 24
 # Countries are lowercased but input from user, too ... so those always match...
27 25
 # ...country is capitalized on output...
28  
-my %currencies = (
29  
-	"abkhazia"=>["Russian ruble (RUB)"],
30  
-	"afghanistan"=>["Afghan afghani (AFN)"],
31  
-	"akrotiri and dhekelia"=>["Euro (EUR)"],
32  
-	"albania"=>["Albanian lek (ALL)"],
33  
-	"alderney"=>["Alderney pound","British pound (GBP)","Guernsey pound"],
34  
-	"algeria"=>["Algerian dinar (DZD)"],
35  
-	"andorra"=>["Euro (EUR)"],
36  
-	"angola"=>["Angolan kwanza (AOA)"],
37  
-	"anguilla"=>["East Caribbean dollar (XCD)"],
38  
-	"antigua and barbuda"=>["East Caribbean dollar (XCD)"],
39  
-	"argentina"=>["Argentine peso (ARS)"],
40  
-	"armenia"=>["Armenian dram (AMD)"],
41  
-	"aruba"=>["Aruban florin (AWG)"],
42  
-	"ascension island"=>["Ascension pound","Saint Helena pound (SHP)"],
43  
-	"australia"=>["Australian dollar (AUD)"],
44  
-	"austria"=>["Euro (EUR)"],
45  
-	"azerbaijan"=>["Azerbaijani manat (AZN)"],
46  
-	"bahamas"=>["Bahamian dollar (BSD)"], # name on Wikipedia is like: "Bahamas, The"..?.
47  
-	"bahrain"=>["Bahraini dinar (BHD)"],
48  
-	"bangladesh"=>["Bangladeshi taka (BDT)"],
49  
-	"barbados"=>["Barbadian dollar (BBD)"],
50  
-	"belarus"=>["Belarusian ruble (BYR)"],
51  
-	"belgium"=>["Euro (EUR)"],
52  
-	"belize"=>["Belize dollar (BZD)"],
53  
-	"benin"=>["West African CFA franc (XOF)"],
54  
-	"bermuda"=>["Bermudian dollar (BMD)"],
55  
-	"bhutan"=>["Bhutanese ngultrum (BTN)","Indian rupee (INR)"],
56  
-	"bolivia"=>["Bolivian boliviano (BOB)"],
57  
-	"bonaire"=>["United States dollar (USD)"],
58  
-	"bosnia and herzegovina"=>["Bosnia and Herzegovina convertible mark (BAM)"],
59  
-	"botswana"=>["Botswana pula (BWP)"],
60  
-	"brazil"=>["Brazilian real (BRL)"],
61  
-	"british indian ocean territory"=>["United States dollar (USD)"],
62  
-	"british virgin islands"=>["British Virgin Islands dollar","United States dollar (USD)"],
63  
-	"brunei"=>["Brunei dollar (BND)","Singapore dollar (SGD)"],
64  
-	"bulgaria"=>["Bulgarian lev (BGN)"],
65  
-	"burkina faso"=>["West African CFA franc (XOF)"],
66  
-	"burma"=>["Burmese kyat (MMK)"],
67  
-	"burundi"=>["Burundian franc (BIF)"],
68  
-	"cambodia"=>["Cambodian riel (KHR)"],
69  
-	"cameroon"=>["Central African CFA franc (XAF)"],
70  
-	"canada"=>["Canadian dollar (CAD)"],
71  
-	"cape verde"=>["Cape Verdean escudo (CVE)"],
72  
-	"cayman islands"=>["Cayman Islands dollar (KYD)"],
73  
-	"central african republic"=>["Central African CFA franc (XAF)"],
74  
-	"chad"=>["Central African CFA franc (XAF)"],
75  
-	"chile"=>["Chilean peso (CLP)"],
76  
-	"china, people's republic of"=>["Chinese yuan (CNY)"],
77  
-	"cocos (keeling) islands"=>["Australian dollar (AUD)","Cocos (Keeling) Islands dollar"],
78  
-	"colombia"=>["Colombian peso (COP)"],
79  
-	"comoros"=>["Comorian franc (KMF)"],
80  
-	"congo, democratic republic of the"=>["Congolese franc (CDF)"],
81  
-	"congo, republic of the"=>["Central African CFA franc (XAF)"],
82  
-	"cook islands"=>["New Zealand dollar (NZD)","Cook Islands dollar"],
83  
-	"costa rica"=>["Costa Rican colón (CRC)"],
84  
-	"croatia"=>["Croatian kuna (HRK)"],
85  
-	"cuba"=>["Cuban convertible peso (CUC)","Cuban peso (CUP)"],
86  
-	"curaçao"=>["Netherlands Antillean guilder (ANG)"],
87  
-	"cyprus"=>["Euro (EUR)"],
88  
-	"czech republic"=>["Czech koruna (CZK)"],
89  
-	"côte d'ivoire"=>["West African CFA franc (XOF)"],
90  
-	"denmark"=>["Danish krone (DKK)"],
91  
-	"djibouti"=>["Djiboutian franc (DJF)"],
92  
-	"dominica"=>["East Caribbean dollar (XCD)"],
93  
-	"dominican republic"=>["Dominican peso (DOP)"],
94  
-	"east timor"=>["United States dollar (USD)"],
95  
-	"ecuador"=>["United States dollar (USD)"],
96  
-	"egypt"=>["Egyptian pound (EGP)"],
97  
-	"el salvador"=>["Salvadoran colón (SVC)","United States dollar (USD)"],
98  
-	"equatorial guinea"=>["Central African CFA franc (XAF)"],
99  
-	"eritrea"=>["Eritrean nakfa (ERN)"],
100  
-	"estonia"=>["Euro (EUR)"],
101  
-	"ethiopia"=>["Ethiopian birr (ETB)"],
102  
-	"falkland islands"=>["Falkland Islands pound (FKP)"],
103  
-	"faroe islands"=>["Danish krone (DKK)","Faroese króna"],
104  
-	"fiji"=>["Fijian dollar (FJD)"],
105  
-	"finland"=>["Euro (EUR)"],
106  
-	"france"=>["Euro (EUR)"],
107  
-	"french polynesia"=>["CFP franc (XPF)"],
108  
-	"gabon"=>["Central African CFA franc (XAF)"],
109  
-	"gambia, the"=>["Gambian dalasi (GMD)"],
110  
-	"georgia"=>["Georgian lari (GEL)"],
111  
-	"germany"=>["Euro (EUR)"],
112  
-	"ghana"=>["Ghana cedi (GHS)"],
113  
-	"gibraltar"=>["Gibraltar pound (GIP)"],
114  
-	"greece"=>["Euro (EUR)"],
115  
-	"grenada"=>["East Caribbean dollar (XCD)"],
116  
-	"guatemala"=>["Guatemalan quetzal (GTQ)"],
117  
-	"guernsey"=>["British pound (GBP)","Guernsey pound"],
118  
-	"guinea"=>["Guinean franc (GNF)"],
119  
-	"guinea-bissau"=>["West African CFA franc (XOF)"],
120  
-	"guyana"=>["Guyanese dollar (GYD)"],
121  
-	"haiti"=>["Haitian gourde (HTG)"],
122  
-	"honduras"=>["Honduran lempira (HNL)"],
123  
-	"hong kong"=>["Hong Kong dollar (HKD)"],
124  
-	"hungary"=>["Hungarian forint (HUF)"],
125  
-	"iceland"=>["Icelandic króna (ISK)"],
126  
-	"india"=>["Indian rupee (INR)"],
127  
-	"indonesia"=>["Indonesian rupiah (IDR)"],
128  
-	"iran"=>["Iranian rial (IRR)"],
129  
-	"iraq"=>["Iraqi dinar (IQD)"],
130  
-	"ireland"=>["Euro (EUR)"],
131  
-	"isle of man"=>["British pound (GBP)","Manx pound"],
132  
-	"israel"=>["Israeli new shekel (ILS)"],
133  
-	"italy"=>["Euro (EUR)"],
134  
-	"jamaica"=>["Jamaican dollar (JMD)"],
135  
-	"japan"=>["Japanese yen (JPY)"],
136  
-	"jersey"=>["British pound (GBP)","Jersey pound"],
137  
-	"jordan"=>["Jordanian dinar (JOD)"],
138  
-	"kazakhstan"=>["Kazakhstani tenge (KZT)"],
139  
-	"kenya"=>["Kenyan shilling (KES)"],
140  
-	"kiribati"=>["Australian dollar (AUD)","Kiribati dollar"],
141  
-	"korea, north"=>["North Korean won (KPW)"],
142  
-	"korea, south"=>["South Korean won (KRW)"],
143  
-	"kosovo"=>["Euro (EUR)"],
144  
-	"kuwait"=>["Kuwaiti dinar (KWD)"],
145  
-	"kyrgyzstan"=>["Kyrgyzstani som (KGS)"],
146  
-	"laos"=>["Lao kip (LAK)"],
147  
-	"latvia"=>["Latvian lats (LVL)"],
148  
-	"lebanon"=>["Lebanese pound (LBP)"],
149  
-	"lesotho"=>["Lesotho loti (LSL)","South African rand (ZAR)"],
150  
-	"liberia"=>["Liberian dollar (LRD)"],
151  
-	"libya"=>["Libyan dinar (LYD)"],
152  
-	"liechtenstein"=>["Swiss franc (CHF)"],
153  
-	"lithuania"=>["Lithuanian litas (LTL)"],
154  
-	"luxembourg"=>["Euro (EUR)"],
155  
-	"macau"=>["Macanese pataca (MOP)"],
156  
-	"macedonia, republic of"=>["Macedonian denar (MKD)"],
157  
-	"madagascar"=>["Malagasy ariary (MGA)"],
158  
-	"malawi"=>["Malawian kwacha (MWK)"],
159  
-	"malaysia"=>["Malaysian ringgit (MYR)"],
160  
-	"maldives"=>["Maldivian rufiyaa (MVR)"],
161  
-	"mali"=>["West African CFA franc (XOF)"],
162  
-	"malta"=>["Euro (EUR)"],
163  
-	"marshall islands"=>["United States dollar (USD)"],
164  
-	"mauritania"=>["Mauritanian ouguiya (MRO)"],
165  
-	"mauritius"=>["Mauritian rupee (MUR)"],
166  
-	"mexico"=>["Mexican peso (MXN)"],
167  
-	"micronesia"=>["Micronesian dollar","United States dollar (USD)"],
168  
-	"moldova"=>["Moldovan leu (MDL)"],
169  
-	"monaco"=>["Euro (EUR)"],
170  
-	"mongolia"=>["Mongolian tögrög (MNT)"],
171  
-	"montenegro"=>["Euro (EUR)"],
172  
-	"montserrat"=>["East Caribbean dollar (XCD)"],
173  
-	"morocco"=>["Moroccan dirham (MAD)"],
174  
-	"mozambique"=>["Mozambican metical (MZN)"],
175  
-	"nagorno-karabakh republic"=>["Armenian dram (AMD)","Nagorno-Karabakh dram"],
176  
-	"namibia"=>["Namibian dollar (NAD)","South African rand (ZAR)"],
177  
-	"nauru"=>["Australian dollar (AUD)","Nauruan dollar"],
178  
-	"nepal"=>["Nepalese rupee (NPR)"],
179  
-	"netherlands"=>["Euro (EUR)"],
180  
-	"new caledonia"=>["CFP franc (XPF)"],
181  
-	"new zealand"=>["New Zealand dollar (NZD)"],
182  
-	"nicaragua"=>["Nicaraguan córdoba (NIO)"],
183  
-	"niger"=>["West African CFA franc (XOF)"],
184  
-	"nigeria"=>["Nigerian naira (NGN)"],
185  
-	"niue"=>["New Zealand dollar (NZD)","Niuean dollar"],
186  
-	"northern cyprus"=>["Turkish lira (TRY)"],
187  
-	"norway"=>["Norwegian krone (NOK)"],
188  
-	"oman"=>["Omani rial (OMR)"],
189  
-	"pakistan"=>["Pakistani rupee (PKR)"],
190  
-	"palau"=>["Palauan dollar","United States dollar (USD)"],
191  
-	"palestine"=>["Israeli new shekel (ILS)","Jordanian dinar (JOD)"],
192  
-	"panama"=>["Panamanian balboa (PAB)","United States dollar (USD)"],
193  
-	"papua new guinea"=>["Papua New Guinean kina (PGK)"],
194  
-	"paraguay"=>["Paraguayan guaraní (PYG)"],
195  
-	"peru"=>["Peruvian nuevo sol (PEN)"],
196  
-	"philippines"=>["Philippine peso (PHP)"],
197  
-	"pitcairn islands"=>["New Zealand dollar (NZD)","Pitcairn Islands dollar"],
198  
-	"poland"=>["Polish złoty (PLN)"],
199  
-	"portugal"=>["Euro (EUR)"],
200  
-	"qatar"=>["Qatari riyal (QAR)"],
201  
-	"romania"=>["Romanian leu (RON)"],
202  
-	"russia"=>["Russian ruble (RUB)"],
203  
-	"rwanda"=>["Rwandan franc (RWF)"],
204  
-	"saba"=>["United States dollar (USD)"],
205  
-	"sahrawi republic"=>["Algerian dinar (DZD)","Mauritanian ouguiya (MRO)","Moroccan dirham (MAD)","Sahrawi peseta"],
206  
-	"saint helena"=>["Saint Helena pound (SHP)"],
207  
-	"saint kitts and nevis"=>["East Caribbean dollar (XCD)"],
208  
-	"saint lucia"=>["East Caribbean dollar (XCD)"],
209  
-	"saint vincent and the grenadines"=>["East Caribbean dollar (XCD)"],
210  
-	"samoa"=>["Samoan tālā (WST)"],
211  
-	"san marino"=>["Euro (EUR)"],
212  
-	"saudi arabia"=>["Saudi riyal (SAR)"],
213  
-	"senegal"=>["West African CFA franc (XOF)"],
214  
-	"serbia"=>["Serbian dinar (RSD)"],
215  
-	"seychelles"=>["Seychellois rupee (SCR)"],
216  
-	"sierra leone"=>["Sierra Leonean leone (SLL)"],
217  
-	"singapore"=>["Brunei dollar (BND)","Singapore dollar (SGD)"],
218  
-	"sint eustatius"=>["United States dollar (USD)"],
219  
-	"sint maarten"=>["Netherlands Antillean guilder (ANG)"],
220  
-	"slovakia"=>["Euro (EUR)"],
221  
-	"slovenia"=>["Euro (EUR)"],
222  
-	"solomon islands"=>["Solomon Islands dollar (SBD)"],
223  
-	"somalia"=>["Somali shilling (SOS)"],
224  
-	"somaliland"=>["Somaliland shilling"],
225  
-	"south africa"=>["South African rand (ZAR)"],
226  
-	"south georgia and the south sandwich islands"=>["British pound (GBP)","South Georgia and the South Sandwich Islands pound"],
227  
-	"south ossetia"=>["Russian ruble (RUB)"],
228  
-	"south sudan"=>["South Sudanese pound (SSP)"],
229  
-	"spain"=>["Euro (EUR)"],
230  
-	"sri lanka"=>["Sri Lankan rupee (LKR)"],
231  
-	"sudan"=>["Sudanese pound (SDG)"],
232  
-	"suriname"=>["Surinamese dollar (SRD)"],
233  
-	"swaziland"=>["Swazi lilangeni (SZL)"],
234  
-	"sweden"=>["Swedish krona (SEK)"],
235  
-	"switzerland"=>["Swiss franc (CHF)"],
236  
-	"syria"=>["Syrian pound (SYP)"],
237  
-	"são tomé and príncipe"=>["São Tomé and Príncipe dobra (STD)"],
238  
-	"taiwan (republic of china)"=>["New Taiwan dollar (TWD)"],
239  
-	"tajikistan"=>["Tajikistani somoni (TJS)"],
240  
-	"tanzania"=>["Tanzanian shilling (TZS)"],
241  
-	"thailand"=>["Thai baht (THB)"],
242  
-	"togo"=>["West African CFA franc (XOF)"],
243  
-	"tonga"=>["Tongan paʻanga (TOP)"],
244  
-	"transnistria"=>["Transnistrian ruble"],
245  
-	"trinidad and tobago"=>["Trinidad and Tobago dollar (TTD)"],
246  
-	"tristan da cunha"=>["Saint Helena pound (SHP)","Tristan da Cunha pound"],
247  
-	"tunisia"=>["Tunisian dinar (TND)"],
248  
-	"turkey"=>["Turkish lira (TRY)"],
249  
-	"turkmenistan"=>["Turkmenistani manat (TMT)"],
250  
-	"turks and caicos islands"=>["United States dollar (USD)"],
251  
-	"tuvalu"=>["Australian dollar (AUD)","Tuvaluan dollar"],
252  
-	"uganda"=>["Ugandan shilling (UGX)"],
253  
-	"ukraine"=>["Ukrainian hryvnia (UAH)"],
254  
-	"united arab emirates"=>["United Arab Emirates dirham (AED)"],
255  
-	"united kingdom"=>["British pound (GBP)"],
256  
-	"united states"=>["United States dollar (USD)"],
257  
-	"uruguay"=>["Uruguayan peso (UYU)"],
258  
-	"uzbekistan"=>["Uzbekistani som (UZS)"],
259  
-	"vanuatu"=>["Vanuatu vatu (VUV)"],
260  
-	"vatican city"=>["Euro (EUR)"],
261  
-	"venezuela"=>["Venezuelan bolívar (VEF)"],
262  
-	"vietnam"=>["Vietnamese đồng (VND)"],
263  
-	"wallis and futuna"=>["CFP franc (XPF)"],
264  
-	"yemen"=>["Yemeni rial (YER)"],
265  
-	"zambia"=>["Zambian kwacha (ZMK)"],
266  
-	"zimbabwe"=>["Botswana pula (BWP)","British pound (GBP)","Euro (EUR)","South African rand (ZAR)","United States dollar (USD)","Zimbabwean dollar (ZWL)"],
267  
-);	
268 26
 
269  
-sub clearCountryName {	# Query may end with "?". If so take it away.
  27
+my %countries = share('currency.txt')->slurp;	
  28
+
  29
+sub clearCountryName {		
270 30
 	my $txt = shift;
271  
-	my $last = substr($txt, length($txt)-1, 1);
272  
-	if ($last eq "?") {
273  
-		$txt = substr($txt,0, length($txt) -1);
274  
-	}
  31
+	$txt =~ s/^\?$|\?$//g;	# Query may end with "?". If so take it away.
275 32
 	$txt =~ s/^\s+|\s+$//g;	# Trim spaces before and after the country name
276 33
 	return $txt
277 34
 }
278 35
 
279 36
 handle remainder => sub {
280  
-	# If there is 'in', 'of' or 'for' keyword then get country name - after the last appearance of keyword
  37
+
281 38
 	if (/^.*(?:in|of|for)\s(.*?)$/) {
282  
-		
283  
-		$country = lc($1);							# Country name is result of previous regexp - make it lowercased
  39
+		$country = lc($1);				# Country name is result of previous regexp - make it lowercased
284 40
 		$country = clearCountryName($country); 		# Clear country name - white spaces, question mark..
  41
+		$country = $country . "\n"; 			# At the moment share() function parsing names with "\n" so match user input..
285 42
 		
286  
-		if (exists $currencies{$country}){
287  
-			my $count = $#{$currencies{$country}} + 1;
288  
-			my $result = "";
289  
-			my $html = "";	
290  
-			my $output_country = $country;			# Pass it to the output_country
291  
-			$output_country =~ s/\b(\w)/\U$1/g; 	# so it can by capitalized
292  
-			if ($count == 1) {
293  
-				$result .= "Currency in $output_country is ";
294  
-				$html .= "Currency in $output_country is ";
  43
+		if (exists $countries{$country}){
  44
+			my $string_currency = $countries{$country};	# Load currencies as string (one line from .txt)
  45
+			my @currencies =  split(',', $string_currency);	# Split currencies into array
  46
+			$country =~ s/\n//g;				# Get rid of the end of the line character
  47
+			
  48
+			my $count = $#currencies + 1;			# Get number of currencies
  49
+			my $result = "";				# Store text result
  50
+			my $html = "";					# Store html result
  51
+			my $output_country = $country;			# Pass country name to the output_country
  52
+			$output_country =~ s/\b(\w)/\U$1/g; 		# so it can by capitalized			
  53
+			if ($count == 1) {				# In number of currencies == 1
  54
+				$result .= "The currency in $output_country is the ";
  55
+				$html .= "The urrency in $output_country is the ";
295 56
 			} else {
296 57
 				$result .= "Currencies in $output_country are: \n";
297 58
 				$html .= "Currencies in $output_country are:<br />";
298 59
 			}
299 60
 			
300 61
 			# Append result with all currencies
301  
-			for $i (0.. $#{$currencies{$country}}){
302  
-				$result .= "$currencies{$country}[$i] \n";
303  
-				$html .= "$currencies{$country}[$i] <br />";
  62
+			foreach $i (@currencies){
  63
+				$i =~ s/\n//g;	# Get rid of the end of the line character
  64
+				$result .= "$i\n";
  65
+				$html .= "$i <br />";
304 66
 			}
305 67
 			
306 68
 			zci is_cached => 1;
21  share/goodie/currency_in/README.txt
... ...
@@ -0,0 +1,21 @@
  1
+parser.py generates currencies.txt for 'CurrencyIn.pm' module.
  2
+
  3
+Currencies for each country are from: 
  4
+http://en.wikipedia.org/wiki/List_of_circulating_currencies
  5
+
  6
+First run: 'fetch.sh' to fetch wikipedia page used by parser,
  7
+then run: 'parse.sh' or just 'python parse.py'
  8
+
  9
+
  10
+DETAILS:
  11
+Output is the file (currencies.txt) with list of countries and currencies with iso code in ()
  12
+For example: 
  13
+slovakia			
  14
+Euro (EUR)
  15
+
  16
+If there is more than one currency used by each country then each currency is separated by comma
  17
+For example:
  18
+zimbabwe			
  19
+Botswana pula (BWP),British pound (GBP)	
  20
+
  21
+Country name is lowercased for better comparison with user input. But it gets capitalized in search results.
476  share/goodie/currency_in/currency.txt
... ...
@@ -0,0 +1,476 @@
  1
+abkhazia
  2
+Russian ruble (RUB)
  3
+afghanistan
  4
+Afghan afghani (AFN)
  5
+akrotiri and dhekelia
  6
+Euro (EUR)
  7
+albania
  8
+Albanian lek (ALL)
  9
+alderney
  10
+Alderney pound,British pound (GBP),Guernsey pound
  11
+algeria
  12
+Algerian dinar (DZD)
  13
+andorra
  14
+Euro (EUR)
  15
+angola
  16
+Angolan kwanza (AOA)
  17
+anguilla
  18
+East Caribbean dollar (XCD)
  19
+antigua and barbuda
  20
+East Caribbean dollar (XCD)
  21
+argentina
  22
+Argentine peso (ARS)
  23
+armenia
  24
+Armenian dram (AMD)
  25
+aruba
  26
+Aruban florin (AWG)
  27
+ascension island
  28
+Ascension pound,Saint Helena pound (SHP)
  29
+australia
  30
+Australian dollar (AUD)
  31
+austria
  32
+Euro (EUR)
  33
+azerbaijan
  34
+Azerbaijani manat (AZN)
  35
+bahamas
  36
+Bahamian dollar (BSD)
  37
+bahrain
  38
+Bahraini dinar (BHD)
  39
+bangladesh
  40
+Bangladeshi taka (BDT)
  41
+barbados
  42
+Barbadian dollar (BBD)
  43
+belarus
  44
+Belarusian ruble (BYR)
  45
+belgium
  46
+Euro (EUR)
  47
+belize
  48
+Belize dollar (BZD)
  49
+benin
  50
+West African CFA franc (XOF)
  51
+bermuda
  52
+Bermudian dollar (BMD)
  53
+bhutan
  54
+Bhutanese ngultrum (BTN),Indian rupee (INR)
  55
+bolivia
  56
+Bolivian boliviano (BOB)
  57
+bonaire
  58
+United States dollar (USD)
  59
+bosnia and herzegovina
  60
+Bosnia and Herzegovina convertible mark (BAM)
  61
+botswana
  62
+Botswana pula (BWP)
  63
+brazil
  64
+Brazilian real (BRL)
  65
+british indian ocean territory
  66
+United States dollar (USD)
  67
+british virgin islands
  68
+British Virgin Islands dollar,United States dollar (USD)
  69
+brunei
  70
+Brunei dollar (BND),Singapore dollar (SGD)
  71
+bulgaria
  72
+Bulgarian lev (BGN)
  73
+burkina faso
  74
+West African CFA franc (XOF)
  75
+burma
  76
+Burmese kyat (MMK)
  77
+burundi
  78
+Burundian franc (BIF)
  79
+cambodia
  80
+Cambodian riel (KHR)
  81
+cameroon
  82
+Central African CFA franc (XAF)
  83
+canada
  84
+Canadian dollar (CAD)
  85
+cape verde
  86
+Cape Verdean escudo (CVE)
  87
+cayman islands
  88
+Cayman Islands dollar (KYD)
  89
+central african republic
  90
+Central African CFA franc (XAF)
  91
+chad
  92
+Central African CFA franc (XAF)
  93
+chile
  94
+Chilean peso (CLP)
  95
+china
  96
+Chinese yuan (CNY)
  97
+cocos islands
  98
+Australian dollar (AUD),Cocos (Keeling) Islands dollar
  99
+colombia
  100
+Colombian peso (COP)
  101
+comoros
  102
+Comorian franc (KMF)
  103
+congo, democratic republic of the
  104
+Congolese franc (CDF)
  105
+congo, republic of the
  106
+Central African CFA franc (XAF)
  107
+cook islands
  108
+New Zealand dollar (NZD),Cook Islands dollar
  109
+costa rica
  110
+Costa Rican colón (CRC)
  111
+croatia
  112
+Croatian kuna (HRK)
  113
+cuba
  114
+Cuban convertible peso (CUC),Cuban peso (CUP)
  115
+curaçao
  116
+Netherlands Antillean guilder (ANG)
  117
+cyprus
  118
+Euro (EUR)
  119
+czech republic
  120
+Czech koruna (CZK)
  121
+côte d'ivoire
  122
+West African CFA franc (XOF)
  123
+denmark
  124
+Danish krone (DKK)
  125
+djibouti
  126
+Djiboutian franc (DJF)
  127
+dominica
  128
+East Caribbean dollar (XCD)
  129
+dominican republic
  130
+Dominican peso (DOP)
  131
+east timor
  132
+United States dollar (USD)
  133
+ecuador
  134
+United States dollar (USD)
  135
+egypt
  136
+Egyptian pound (EGP)
  137
+el salvador
  138
+Salvadoran colón (SVC),United States dollar (USD)
  139
+equatorial guinea
  140
+Central African CFA franc (XAF)
  141
+eritrea
  142
+Eritrean nakfa (ERN)
  143
+estonia
  144
+Euro (EUR)
  145
+ethiopia
  146
+Ethiopian birr (ETB)
  147
+falkland islands
  148
+Falkland Islands pound (FKP)
  149
+faroe islands
  150
+Danish krone (DKK),Faroese króna
  151
+fiji
  152
+Fijian dollar (FJD)
  153
+finland
  154
+Euro (EUR)
  155
+france
  156
+Euro (EUR)
  157
+french polynesia
  158
+CFP franc (XPF)
  159
+gabon
  160
+Central African CFA franc (XAF)
  161
+gambia, the
  162
+Gambian dalasi (GMD)
  163
+georgia
  164
+Georgian lari (GEL)
  165
+germany
  166
+Euro (EUR)
  167
+ghana
  168
+Ghana cedi (GHS)
  169
+gibraltar
  170
+Gibraltar pound (GIP)
  171
+greece
  172
+Euro (EUR)
  173
+grenada
  174
+East Caribbean dollar (XCD)
  175
+guatemala
  176
+Guatemalan quetzal (GTQ)
  177
+guernsey
  178
+British pound (GBP),Guernsey pound
  179
+guinea
  180
+Guinean franc (GNF)
  181
+guinea-bissau
  182
+West African CFA franc (XOF)
  183
+guyana
  184
+Guyanese dollar (GYD)
  185
+haiti
  186
+Haitian gourde (HTG)
  187
+honduras
  188
+Honduran lempira (HNL)
  189
+hong kong
  190
+Hong Kong dollar (HKD)
  191
+hungary
  192
+Hungarian forint (HUF)
  193
+iceland
  194
+Icelandic króna (ISK)
  195
+india
  196
+Indian rupee (INR)
  197
+indonesia
  198
+Indonesian rupiah (IDR)
  199
+iran
  200
+Iranian rial (IRR)
  201
+iraq
  202
+Iraqi dinar (IQD)
  203
+ireland
  204
+Euro (EUR)
  205
+isle of man
  206
+British pound (GBP),Manx pound
  207
+israel
  208
+Israeli new shekel (ILS)
  209
+italy
  210
+Euro (EUR)
  211
+jamaica
  212
+Jamaican dollar (JMD)
  213
+japan
  214
+Japanese yen (JPY)
  215
+jersey
  216
+British pound (GBP),Jersey pound
  217
+jordan
  218
+Jordanian dinar (JOD)
  219
+kazakhstan
  220
+Kazakhstani tenge (KZT)
  221
+kenya
  222
+Kenyan shilling (KES)
  223
+kiribati
  224
+Australian dollar (AUD),Kiribati dollar
  225
+korea, north
  226
+North Korean won (KPW)
  227
+korea, south
  228
+South Korean won (KRW)
  229
+kosovo
  230
+Euro (EUR)
  231
+kuwait
  232
+Kuwaiti dinar (KWD)
  233
+kyrgyzstan
  234
+Kyrgyzstani som (KGS)
  235
+laos
  236
+Lao kip (LAK)
  237
+latvia
  238
+Latvian lats (LVL)
  239
+lebanon
  240
+Lebanese pound (LBP)
  241
+lesotho
  242
+Lesotho loti (LSL),South African rand (ZAR)
  243
+liberia
  244
+Liberian dollar (LRD)
  245
+libya
  246
+Libyan dinar (LYD)
  247
+liechtenstein
  248
+Swiss franc (CHF)
  249
+lithuania
  250
+Lithuanian litas (LTL)
  251
+luxembourg
  252
+Euro (EUR)
  253
+macau
  254
+Macanese pataca (MOP)
  255
+macedonia
  256
+Macedonian denar (MKD)
  257
+madagascar
  258
+Malagasy ariary (MGA)
  259
+malawi
  260
+Malawian kwacha (MWK)
  261
+malaysia
  262
+Malaysian ringgit (MYR)
  263
+maldives
  264
+Maldivian rufiyaa (MVR)
  265
+mali
  266
+West African CFA franc (XOF)
  267
+malta
  268
+Euro (EUR)
  269
+marshall islands
  270
+United States dollar (USD)
  271
+mauritania
  272
+Mauritanian ouguiya (MRO)
  273
+mauritius
  274
+Mauritian rupee (MUR)
  275
+mexico
  276
+Mexican peso (MXN)
  277
+micronesia
  278
+Micronesian dollar,United States dollar (USD)
  279
+moldova
  280
+Moldovan leu (MDL)
  281
+monaco
  282
+Euro (EUR)
  283
+mongolia
  284
+Mongolian tögrög (MNT)
  285
+montenegro
  286
+Euro (EUR)
  287
+montserrat
  288
+East Caribbean dollar (XCD)
  289
+morocco
  290
+Moroccan dirham (MAD)
  291
+mozambique
  292
+Mozambican metical (MZN)
  293
+nagorno-karabakh republic
  294
+Armenian dram (AMD),Nagorno-Karabakh dram
  295
+namibia
  296
+Namibian dollar (NAD),South African rand (ZAR)
  297
+nauru
  298
+Australian dollar (AUD),Nauruan dollar
  299
+nepal
  300
+Nepalese rupee (NPR)
  301
+netherlands
  302
+Euro (EUR)
  303
+new caledonia
  304
+CFP franc (XPF)
  305
+new zealand
  306
+New Zealand dollar (NZD)
  307
+nicaragua
  308
+Nicaraguan córdoba (NIO)
  309
+niger
  310
+West African CFA franc (XOF)
  311
+nigeria
  312
+Nigerian naira (NGN)
  313
+niue
  314
+New Zealand dollar (NZD),Niuean dollar
  315
+northern cyprus
  316
+Turkish lira (TRY)
  317
+norway
  318
+Norwegian krone (NOK)
  319
+oman
  320
+Omani rial (OMR)
  321
+pakistan
  322
+Pakistani rupee (PKR)
  323
+palau
  324
+Palauan dollar,United States dollar (USD)
  325
+palestine
  326
+Israeli new shekel (ILS),Jordanian dinar (JOD)
  327
+panama
  328
+Panamanian balboa (PAB),United States dollar (USD)
  329
+papua new guinea
  330
+Papua New Guinean kina (PGK)
  331
+paraguay
  332
+Paraguayan guaraní (PYG)
  333
+peru
  334
+Peruvian nuevo sol (PEN)
  335
+philippines
  336
+Philippine peso (PHP)
  337
+pitcairn islands
  338
+New Zealand dollar (NZD),Pitcairn Islands dollar
  339
+poland
  340
+Polish złoty (PLN)
  341
+portugal
  342
+Euro (EUR)
  343
+qatar
  344
+Qatari riyal (QAR)
  345
+romania
  346
+Romanian leu (RON)
  347
+russia
  348
+Russian ruble (RUB)
  349
+rwanda
  350
+Rwandan franc (RWF)
  351
+saba
  352
+United States dollar (USD)
  353
+sahrawi republic
  354
+Algerian dinar (DZD),Mauritanian ouguiya (MRO),Moroccan dirham (MAD),Sahrawi peseta
  355
+saint helena
  356
+Saint Helena pound (SHP)
  357
+saint kitts and nevis
  358
+East Caribbean dollar (XCD)
  359
+saint lucia
  360
+East Caribbean dollar (XCD)
  361
+saint vincent and the grenadines
  362
+East Caribbean dollar (XCD)
  363
+samoa
  364
+Samoan tālā (WST)
  365
+san marino
  366
+Euro (EUR)
  367
+saudi arabia
  368
+Saudi riyal (SAR)
  369
+senegal
  370
+West African CFA franc (XOF)
  371
+serbia
  372
+Serbian dinar (RSD)
  373
+seychelles
  374
+Seychellois rupee (SCR)
  375
+sierra leone
  376
+Sierra Leonean leone (SLL)
  377
+singapore
  378
+Brunei dollar (BND),Singapore dollar (SGD)
  379
+sint eustatius
  380
+United States dollar (USD)
  381
+sint maarten
  382
+Netherlands Antillean guilder (ANG)
  383
+slovakia
  384
+Euro (EUR)
  385
+slovenia
  386
+Euro (EUR)
  387
+solomon islands
  388
+Solomon Islands dollar (SBD)
  389
+somalia
  390
+Somali shilling (SOS)
  391
+somaliland
  392
+Somaliland shilling
  393
+south africa
  394
+South African rand (ZAR)
  395
+south georgia and the south sandwich islands
  396
+British pound (GBP),South Georgia and the South Sandwich Islands pound
  397
+south ossetia
  398
+Russian ruble (RUB)
  399
+south sudan
  400
+South Sudanese pound (SSP)
  401
+spain
  402
+Euro (EUR)
  403
+sri lanka
  404
+Sri Lankan rupee (LKR)
  405
+sudan
  406
+Sudanese pound (SDG)
  407
+suriname
  408
+Surinamese dollar (SRD)
  409
+swaziland
  410
+Swazi lilangeni (SZL)
  411
+sweden
  412
+Swedish krona (SEK)
  413
+switzerland
  414
+Swiss franc (CHF)
  415
+syria
  416
+Syrian pound (SYP)
  417
+são tomé and príncipe
  418
+São Tomé and Príncipe dobra (STD)
  419
+taiwan (republic of china)
  420
+New Taiwan dollar (TWD)
  421
+tajikistan
  422
+Tajikistani somoni (TJS)
  423
+tanzania
  424
+Tanzanian shilling (TZS)
  425
+thailand
  426
+Thai baht (THB)
  427
+togo
  428
+West African CFA franc (XOF)
  429
+tonga
  430
+Tongan paʻanga (TOP)
  431
+transnistria
  432
+Transnistrian ruble
  433
+trinidad and tobago
  434
+Trinidad and Tobago dollar (TTD)
  435
+tristan da cunha
  436
+Saint Helena pound (SHP),Tristan da Cunha pound
  437
+tunisia
  438
+Tunisian dinar (TND)
  439
+turkey
  440
+Turkish lira (TRY)
  441
+turkmenistan
  442
+Turkmenistani manat (TMT)
  443
+turks and caicos islands
  444
+United States dollar (USD)
  445
+tuvalu
  446
+Australian dollar (AUD),Tuvaluan dollar
  447
+uganda
  448
+Ugandan shilling (UGX)
  449
+ukraine
  450
+Ukrainian hryvnia (UAH)
  451
+united arab emirates
  452
+United Arab Emirates dirham (AED)
  453
+united kingdom
  454
+British pound (GBP)
  455
+united states
  456
+United States dollar (USD)
  457
+uruguay
  458
+Uruguayan peso (UYU)
  459
+uzbekistan
  460
+Uzbekistani som (UZS)
  461
+vanuatu
  462
+Vanuatu vatu (VUV)
  463
+vatican city
  464
+Euro (EUR)
  465
+venezuela
  466
+Venezuelan bolívar (VEF)
  467
+vietnam
  468
+Vietnamese đồng (VND)
  469
+wallis and futuna
  470
+CFP franc (XPF)
  471
+yemen
  472
+Yemeni rial (YER)
  473
+zambia
  474
+Zambian kwacha (ZMK)
  475
+zimbabwe
  476
+Botswana pula (BWP),British pound (GBP),Euro (EUR),South African rand (ZAR),United States dollar (USD),Zimbabwean dollar (ZWL)
3  share/goodie/currency_in/fetch.sh
... ...
@@ -0,0 +1,3 @@
  1
+#!/bin/bash
  2
+mkdir -p download
  3
+wget "http://en.wikipedia.org/wiki/List_of_circulating_currencies" -O download/page.dat
68  share/goodie/currency_in/parse.py
... ...
@@ -0,0 +1,68 @@
  1
+#!/usr/bin/python
  2
+# -*- coding: utf-8 -*-
  3
+
  4
+# Released under the GPL v2 license 
  5
+# https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  6
+
  7
+import lxml.html
  8
+import sys
  9
+
  10
+#url = "http://en.wikipedia.org/wiki/List_of_circulating_currencies"
  11
+url = "https://secure.wikimedia.org/wikipedia/en/wiki/List_of_circulating_currencies"
  12
+
  13
+countries = {};     # country:[[currency, code] [currency, code],...]
  14
+country = ""        # store current country for each row
  15
+currency = ""       # store current currency for each row
  16
+iso_code = ""       # store current iso code for currency
  17
+description = ""    # store currency and iso code when saving into file
  18
+
  19
+def add_currency(country, currency, iso_code, countries):
  20
+    "Add country into countries list"
  21
+    country = country.encode("utf8")
  22
+    if country in countries:
  23
+        countries[country].append([currency, iso_code])
  24
+    else:
  25
+        countries[country] = [[currency, iso_code]]
  26
+
  27
+def clear_text(text):
  28
+    "Clear text of anotations in []. When e.g. 'Ascension pound[A]' contains [A]"
  29
+    start = text.find("[")
  30
+    if start != -1:
  31
+        text = text[:start]
  32
+    return text
  33
+
  34
+tree = lxml.html.parse("download/page.dat").getroot()
  35
+tables = tree.find_class("wikitable sortable")
  36
+for table in tables:
  37
+    for row in table.findall('tr'):
  38
+        cells = row.findall('td')
  39
+        if len(cells) == 6:
  40
+            country = cells[0].text_content()
  41
+            currency = cells[1].text_content()
  42
+            iso_code = cells[3].text_content()
  43
+
  44
+        if len(cells) == 5:
  45
+            currency = cells[0].text_content()
  46
+            iso_code = cells[2].text_content()
  47
+        
  48
+        currency = clear_text(currency)
  49
+        iso_code = iso_code if iso_code != "None" else ""
  50
+        
  51
+        if currency != "None" and currency != "":
  52
+            add_currency(country[1:], currency, iso_code, countries)
  53
+
  54
+"Make output file 'currency.txt' as Perl 'hash table' ready for 'CurrencyIn.pm' module"
  55
+output = "currency.txt"
  56
+f= open(output, "w")
  57
+result = []
  58
+for country in sorted(countries):
  59
+	description = ""
  60
+	formated_record = []
  61
+			
  62
+	for record in countries[country]:
  63
+		iso_code = "" if record[1] == "" else (" (" + record[1] + ")")
  64
+		currency = record[0]
  65
+		formated_record.append((currency + iso_code).encode("utf8"))
  66
+		description = ','.join(str(x) for x in formated_record)
  67
+	f.write(country.lower() + "\n" +  description + "\n")
  68
+f.close()
5  share/goodie/currency_in/parse.sh
... ...
@@ -0,0 +1,5 @@
  1
+#!/bin/bash
  2
+
  3
+# Make currency.txt:
  4
+python parse.py
  5
+
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.