我有用于检查更改DOM
元素的库,并且我有一个包含几乎所有国家/地区和大写字母的大型地图数组。
当虚拟聊天中出现新消息时,我会检查它是否包含关键字并需要country/capital
并在我的文本输入中获取结果。但是,有时它会给我错误的结果。白俄罗斯和其他一些国家的资本给我Washington.D.C
,当其他国家工作得很好,我似乎无法找到原因。
//Capitals Hunter
var CountryCapitalList = new Map();
CountryCapitalList.set("Afghanistan","Kabul");
CountryCapitalList.set("Albania","Tirana");
CountryCapitalList.set("Algeria","Algiers");
CountryCapitalList.set("Andorra","Andorra la Vella");
CountryCapitalList.set("Angola","Luanda");
CountryCapitalList.set("Antigua and Barbuda","Saint John's");
CountryCapitalList.set("Argentina","Buenos Aires");
CountryCapitalList.set("Armenia","Yerevan");
CountryCapitalList.set("Australia","Canberra");
CountryCapitalList.set("Austria","Vienna");
CountryCapitalList.set("Azerbaijan","Baku");
CountryCapitalList.set("Bahamas","Nassau");
CountryCapitalList.set("Bahrain","Manama");
CountryCapitalList.set("Bangladesh","Dhaka");
CountryCapitalList.set("Barbados","Bridgetown");
CountryCapitalList.set("Belarus","Minsk");
CountryCapitalList.set("Belgium","Brussels");
CountryCapitalList.set("Belize","Belmopan");
CountryCapitalList.set("Benin","Porto-Novo");
CountryCapitalList.set("Bhutan","Thimphu");
CountryCapitalList.set("Bolivia"," Sucre");
CountryCapitalList.set("Bosnia and Herzegovina","Sarajevo");
CountryCapitalList.set("Botswana","Gaborone");
CountryCapitalList.set("Brazil","Brasilia");
CountryCapitalList.set("Brunei","Bandar Seri Begawan");
CountryCapitalList.set("Bulgaria","Sofia");
CountryCapitalList.set("Burkina Faso","Ouagadougou");
CountryCapitalList.set("Burundi","Bujumbura");
CountryCapitalList.set("Cabo Verde","Praia");
CountryCapitalList.set("Cambodia","Phnom Penh");
CountryCapitalList.set("Cameroon","Yaounde");
CountryCapitalList.set("Canada","Ottawa");
CountryCapitalList.set("Central African Republic","Bangui");
CountryCapitalList.set("Chad","N'Djamena");
CountryCapitalList.set("Chile","Santiago");
CountryCapitalList.set("China","Beijing");
CountryCapitalList.set("Colombia","Bogotá");
CountryCapitalList.set("Comoros","Moroni");
CountryCapitalList.set("Congo","Brazzaville");
CountryCapitalList.set("Costa Rica"," San Jose");
CountryCapitalList.set("Cote d'Ivoire","Yamoussoukro");
CountryCapitalList.set("Croatia","Zagreb");
CountryCapitalList.set("Cuba","Havana");
CountryCapitalList.set("Cyprus","Nicosia");
CountryCapitalList.set("Czech Republic","Prague");
CountryCapitalList.set("Denmark","Copenhagen");
CountryCapitalList.set("Djibouti","Djibouti");
CountryCapitalList.set("Dominica","Roseau");
CountryCapitalList.set("Dominican Republic","Santo Domingo");
CountryCapitalList.set("Ecuador","Quito");
CountryCapitalList.set("Egypt","Cairo");
CountryCapitalList.set("El Salvador","San Salvador");
CountryCapitalList.set("Guinea","Malabo");
CountryCapitalList.set("Eritrea","Asmara");
CountryCapitalList.set("Estonia","Tallinn");
CountryCapitalList.set("Ethiopia","Addis Ababa");
CountryCapitalList.set("Fiji","Suva");
CountryCapitalList.set("Finland","Helsinki");
CountryCapitalList.set("France","Paris");
CountryCapitalList.set("Gabon","Libreville");
CountryCapitalList.set("Gambia","Banjul");
CountryCapitalList.set("Georgia","Tbilisi");
CountryCapitalList.set("Germany","Berlin");
CountryCapitalList.set("Ghana","Accra");
CountryCapitalList.set("Greece","Athens");
CountryCapitalList.set("Grenada","Saint George's");
CountryCapitalList.set("Guatemala","Guatemala City");
CountryCapitalList.set("Guinea","Conakry");
CountryCapitalList.set("Guinea-Bissau","Bissau");
CountryCapitalList.set("Guyana","Georgetown");
CountryCapitalList.set("Haiti","Port-au-Prince");
CountryCapitalList.set("Honduras","Tegucigalpa");
CountryCapitalList.set("Hungary","Budapest");
CountryCapitalList.set("Iceland","Reykjavik");
CountryCapitalList.set("India","New Delhi");
CountryCapitalList.set("Indonesia","Jakarta");
CountryCapitalList.set("Iran","Tehran");
CountryCapitalList.set("Iraq","Baghdad");
CountryCapitalList.set("Ireland","Dublin");
CountryCapitalList.set("Israel","Jerusalem");
CountryCapitalList.set("Italy","Rome");
CountryCapitalList.set("Jamaica","Kingston");
CountryCapitalList.set("Japan","Tokyo");
CountryCapitalList.set("Jordan","Amman");
CountryCapitalList.set("Kazakhstan","Astana");
CountryCapitalList.set("Kenya","Nairobi");
CountryCapitalList.set("Kiribati","Tarawa");
CountryCapitalList.set("Kosovo","Pristina");
CountryCapitalList.set("Kuwait","Kuwait City");
CountryCapitalList.set("Laos","Bishkek");
CountryCapitalList.set("Latvia","Riga");
CountryCapitalList.set("Lebanon","Beirut");
CountryCapitalList.set("Lesotho","Maseru");
CountryCapitalList.set("Liberia","Monrovia");
CountryCapitalList.set("Libya","Tripoli");
CountryCapitalList.set("Liechtenstein","Vaduz");
CountryCapitalList.set("Lithuania","Vilnius");
CountryCapitalList.set("Luxembourg","Luxembourg");
CountryCapitalList.set("Macedonia","Skopje");
CountryCapitalList.set("Madagascar","Antananarivo");
CountryCapitalList.set("Malawi","Lilongwe");
CountryCapitalList.set("Malaysia","Kuala Lumpur");
CountryCapitalList.set("Maldives","Male");
CountryCapitalList.set("Mali","Bamako");
CountryCapitalList.set("Malta","Valletta");
CountryCapitalList.set("Marshall Islands","Majuro");
CountryCapitalList.set("Mauritania","Nouakchott");
CountryCapitalList.set("Mauritius","Port Louis");
CountryCapitalList.set("Mexico","Mexico City");
CountryCapitalList.set("Micronesia","Palikir");
CountryCapitalList.set("Moldova","Chisinau");
CountryCapitalList.set("Monaco","Monaco");
CountryCapitalList.set("Mongolia","Ulaanbaatar");
CountryCapitalList.set("Montenegro","Podgorica");
CountryCapitalList.set("Morocco","Rabat");
CountryCapitalList.set("Mozambique","Maputo");
CountryCapitalList.set("Myanmar","Naypyidaw");
CountryCapitalList.set("Namibia","Windhoek");
CountryCapitalList.set("Nauru","Yaren District");
CountryCapitalList.set("Nepal","Kathmandu");
CountryCapitalList.set("Netherlands","Amsterdam");
CountryCapitalList.set("New Zealand","Wellington");
CountryCapitalList.set("Nigeria","Abuja");
CountryCapitalList.set("Nicaragua","Managua");
CountryCapitalList.set("Niger","Niamey");
CountryCapitalList.set("North Korea","Pyongyang");
CountryCapitalList.set("Norway","Oslo");
CountryCapitalList.set("Oman","Muscat");
CountryCapitalList.set("Pakistan","Islamabad");
CountryCapitalList.set("Palau","Ngerulmud");
CountryCapitalList.set("Palestine","Jerusalem");
CountryCapitalList.set("Panama","Panama City");
CountryCapitalList.set("New Guinea","Port Moresby");
CountryCapitalList.set("Paraguay","Asunción");
CountryCapitalList.set("Peru","Lima");
CountryCapitalList.set("Philippines","Manila");
CountryCapitalList.set("Poland","Warsaw");
CountryCapitalList.set("Portugal","Lisbon");
CountryCapitalList.set("Qatar","Doha");
CountryCapitalList.set("Romania","Bucharest");
CountryCapitalList.set("Russia","Moscow");
CountryCapitalList.set("Rwanda","Kigali");
CountryCapitalList.set("Saint Kitts and Nevis","Basseterre");
CountryCapitalList.set("Saint Lucia ","Castries");
CountryCapitalList.set("Saint Vincent and the Grenadines","Kingstown");
CountryCapitalList.set("Samoa","Apia");
CountryCapitalList.set("San Marino","San Marino");
CountryCapitalList.set("Sao Tome and Principe","Sao Tome");
CountryCapitalList.set("Saudi Arabia","Riyadh");
CountryCapitalList.set("Senegal","Dakar");
CountryCapitalList.set("Serbia","Belgrade");
CountryCapitalList.set("Seychelles","Victoria");
CountryCapitalList.set("Sierra Leone","Freetown");
CountryCapitalList.set("Singapore","Singapore");
CountryCapitalList.set("Slovakia","Bratislava");
CountryCapitalList.set("Slovenia","Ljubljana");
CountryCapitalList.set("Solomon Islands","Honiara");
CountryCapitalList.set("Somalia","Mogadishu");
CountryCapitalList.set("South Africa","Cape Town ");
CountryCapitalList.set("South Korea","Seoul");
CountryCapitalList.set("South Sudan","Juba");
CountryCapitalList.set("Spain","Madrid");
CountryCapitalList.set("Sri Lanka","Sri Jayawardenepura Kotte");
CountryCapitalList.set("Sudan","Khartoum");
CountryCapitalList.set("Suriname","Paramaribo");
CountryCapitalList.set("Swaziland","Mbabane,Lobamba");
CountryCapitalList.set("Sweden","Stockholm");
CountryCapitalList.set("Switzerland","Bern");
CountryCapitalList.set("Syria","Damascus");
CountryCapitalList.set("Taiwan","Taipei");
CountryCapitalList.set("Tajikistan","Dushanbe");
CountryCapitalList.set("Tanzania","Dodoma");
CountryCapitalList.set("Thailand","Bangkok");
CountryCapitalList.set("Timor-Leste","Dili");
CountryCapitalList.set("Togo","Lome");
CountryCapitalList.set("Tonga","Nukualofa");
CountryCapitalList.set("Tunisia","Tunis");
CountryCapitalList.set("Trinidad and Tobago","Port of Spain");
CountryCapitalList.set("Turkey","Ankara");
CountryCapitalList.set("Turkmenistan","Ashgabat");
CountryCapitalList.set("Tuvalu","Funafuti");
CountryCapitalList.set("Uganda","Kampala");
CountryCapitalList.set("Ukraine","Kiev");
CountryCapitalList.set("United Arab Emirates","Abu Dhabi");
CountryCapitalList.set("United Kingdom","London");
CountryCapitalList.set("England","London");
CountryCapitalList.set("U.S.","Washington, D.C.");
CountryCapitalList.set("US","Washington, D.C.");
CountryCapitalList.set("United States","Washington, D.C.");
CountryCapitalList.set("Uzbekistan","Tashkent");
CountryCapitalList.set("Vanuatu","Port Vila");
CountryCapitalList.set("Vatican","Vatican");
CountryCapitalList.set("Venezuela","Caracas");
CountryCapitalList.set("Vietnam","Hanoi");
CountryCapitalList.set("Yemen","Sana");
CountryCapitalList.set("Zambia","Lusaka");
CountryCapitalList.set("Zimbabwe","Harare");
CountryCapitalList.set("Zaire","Kinshasa");
CountryCapitalList.set("Cape Verde","Praya");
var ModerCap;
var ModerText;
var MessageCapText;
ModerCap = $(".messages li:last-child");
ModerText = ModerCap.children(".msg-body");
MessageCapText = ModerText.text().toLowerCase();
CountryCapitalList.forEach(function (value, key, map) {
if((MessageCapText.includes("capital") && MessageCapText.includes(key.toString().toLowerCase()))
|| (MessageCapText.includes("столица") && MessageCapText.includes(key.toString().toLowerCase())
|| MessageCapText.includes("столицу") && MessageCapText.includes(key.toString().toLowerCase())))
{
$(".chat-input").val(value);
console.log(value);
}
else if(((MessageCapText.includes("city") ||MessageCapText.includes("capital")) && MessageCapText.includes(value.toString().toLowerCase()))
|| (MessageCapText.includes("столица") && MessageCapText.includes(value.toString().toLowerCase())
|| MessageCapText.includes("столицей") && MessageCapText.includes(value.toString().toLowerCase())))
{
$(".chat-input").val(key);
console.log("key");
}
})
答案 0 :(得分:0)
这是因为您正在测试消息文本是否包含key
地图记录,其中一个键是"US"
。所以它会在belarus
中找到,因为最后两个字符匹配。分割消息并检查每个单词是否等于密钥而不是包含在整个消息中可能更好。