从字符串中查找城市和国家名称

时间:2018-09-25 04:19:27

标签: javascript node.js npm

我在json文件中有城市和县列表。对于给定的字符串,我需要检查城市名称和国家名称是否存在。如果存在,我必须大写这个词。在节点JS中实现此目标的最佳方法是什么

请考虑此链接中的json。 https://raw.githubusercontent.com/russ666/all-countries-and-cities-json/master/countries.json

我的输入是“美国在布拉迪斯拉发玩戴维斯杯”

输出应为“美国在布拉迪斯拉发打戴维斯杯”

提示:城市和国家/地区名称的首字母应为大写字母。

我期望这样的代码

var myString="united states to play davis cup in bratislava";
var data=myjson;
var i=0;
myString=myString.split("");
for(i=0;i<myString.length;i++){
var output="";
//help this line
var check=myString[i].match(data)
if(check){
output+=myString[i].charAt(0).toUpperCase() + myString[i].slice(1);
}
else{
output+=myString[i]}
}

2 个答案:

答案 0 :(得分:0)

所有操作均从底部的function start()开始。为了显示目的,我使用了一个小的数据集,但是您可以使用const data = require('data.json')从json文件中获取数据。我也测试过大型数据集,就像一个魅力。希望对您有所帮助。

const data = {
    "United States":[
    "Washington","Bratislava","Hard","Going"],  
    "Afghanistan": [
    "Herat",
    "Kabul",
    "Kandahar",
    "Molah",
    "Rana",
    "Shar",
    "Sharif",
    "Wazir Akbar Khan"
]};
Array.prototype.myJoin = function(start,end){
    if(!start) start = 0;
    if(!end) end = this.length - 1;
    end++;
    return this.slice(start,end);
};


const getCityData = async (country) => {
    return country;
}

const changeFormat = async () => {
    try {
        let countries = Object.keys(data).map( (country, index) => {
            return country;
        })
        let citiesData = [];
        await countries.map( (country, index) => {
            citiesData = citiesData.concat(data[country]);
        })
        return countries.concat(citiesData);
    } catch (err) {
        return err;
    }
}
const checkSentence = (text, text_original, number, modified_data) => {
    return new Promise((resolve, reject)=>{
        try {
            if( !text || !text.length ){
                throw new Error('empty text');
            }
            // console.log('started ' + number);
            // console.log('number ' + number +' started')
            let upperCase = [];
            const number_const = number;
            let temp1 = new Array(text.length);
            temp1.fill(2);
            temp1.map( (v, i) => {
                // let temp = text;
                let temp = [...text_original, ...[]];
                // console.log('i' + i);
                // console.log('number' + number);
                
                if(i + number <= text.length ) {
                    // console.log('inside 1st if');
                    temp = temp.slice(i, i + number)
                    // console.log(text + ' 1');
                    temp = temp.join(' ')
                    // console.log(text + ' 2');                    
                    temp = temp.toLowerCase();
                    // console.log(text + ' 3');                    
                    if(modified_data.indexOf(temp) != -1){                        
                        upperCase.push({ start: i, end: i + number - 1 })
                    }
                }
            })
            
            let toBeChanged = [];
            if(upperCase.length){
             upperCase.map( (v, i) => {
                 // console.log(v);
                 let arr = range( v.start, v.end )
                 toBeChanged = toBeChanged.concat(arr);
             })
            }
            // console.log('ended number' + number);
            // console.log(toBeChanged);
            return resolve(toBeChanged);
        } catch (err) {
            return reject(err);
            // console.error(err);
            // return false;
        }      
    })
}

const range = (start, end) => {
    // console.log(start);
    // console.log(end);
    return Array(end - start + 1).fill().map((_, idx) => start + idx)
}

const start = async() => {
    try {
        excludeWords.map( (word, index) => {
            excludeWords[index] = excludeWords[index].toLowerCase();
        });
        
        let modified_data_1 = await changeFormat(); 
        let maximum = 1;
        modified_data = modified_data_1.map( (v, i) => {
            if(v.split(' ').length > maximum){
                maximum = v.split(' ').length
            }
            if(excludeWords.indexOf(v.toLowerCase()) == -1) {
                return v.toLowerCase();
            }
        });
        text = text.split(' ');
        if(maximum > text.length){
            maximum = text.length;
        }
        // console.log(maximum);
        let temp = new Array(maximum);
        temp.fill(2);
        let answer = await temp.map( (v, i) => {
            let tempArray = [...text, ...[]];
            let tempArray1 = [...text, ...[]];
            return checkSentence(tempArray, tempArray1, (maximum - i), modified_data);
        })
        return Promise.all(answer).then( (results) => {
            let merged = [].concat.apply([], results);
            // console.log('merged');
            merged = new Set(merged);
            merged = [...merged];
            // console.log(merged);
            merged.map((v, i) => {
                if(v == undefined || v == null){
                    return;
                }
                let temp1 = text[v].split('');
                temp1[0] = temp1[0].toUpperCase();
                text[v] = temp1.join('');
            })
            // console.log(text.join(' '));
            return text.join(' ');
        }).catch((err)=>{
            console.log(err);
        })  
    } catch (err) {
        // console.error('here ERROR');
        console.error(err);
        return false;
    }
}

let excludeWords = ['Hard', 'Going'];
let text = 'united states to davis cup hard wazir Akbar Khan in bratislava';

( async () => {
    let answer = await start();
    console.log(answer);
})();

答案 1 :(得分:0)

嗨,我已经以简单的方式完成了它的工作。我的问题是字符串“ davis” 也在json中作为城市出现。如何不大写这个词。例如:“难”,“去” ,这些词也有城市名称。但是这些词在我的程序中不被视为城市。

情况1:

输入:台湾因非典爆发而遭受重创。

输出应为:台湾受到SARS爆发的严重打击。

我的输出台湾因SARS爆发而困难

请安装大写npm并在根文件夹中使用data.json文件夹执行以下代码

var myData=require("./data");
var countriesArray=Object.keys(myData.data);
var citiesArray=Object.values(myData.data);
var capitalize=require('capitalize');
var citiesFlatten = [].concat.apply([], citiesArray);
var countryCities=countriesArray.concat(citiesFlatten);

var str = 'russia ponders space tourism deal';
var pattern = new RegExp("\\b("+countryCities.join("|")+")\\b","ig");
var matchArray=str.match(pattern);
if(!!matchArray){
    matchArray.forEach(function(item) {
        str=str.replace(item,capitalize.words(item));
    });
    console.log( str.replace(/^\w/, c => c.toUpperCase()));
}