Several lines of python code to resolve associations of Related words

Keywords: Python

In our daily life, we often encounter the problem of associating related words. That is to say, it is not too difficult to input a word and query related words. But how to accumulate so many words and use good algorithms to link related content is not simple in itself. I think the easiest way is to call the relevant interface, which saves a lot of trouble, and a few lines of python code can be done.

# -*- coding: utf-8 -*-
# flake8: noqa
__author__ = 'wukong'

import urllib
from urllib import urlencode

#Configure the appKey and openId you applied for
app_key="***"
open_id="***"

"""
request_url Request address
params Request parameters
method Request method

"""
def request_content(request_url,params,method):
    params = urlencode(params)
    
    if method and method.lower() =="get":
        f = urllib.urlopen("%s?%s" % (request_url, params))
    else:
        f = urllib.urlopen(request_url, params)
 
    content = f.read()
    print content

   
def main():
    
    domain="http://api.xiaocongjisuan.com/"
    servlet="data/relativeword/mining"
    method="get"
    request_url=domain+servlet
    
    #Dictionaries
    params ={}
    params["appKey"]=app_key
    params["openId"]=open_id
    
    #Change section
    params["keyword"]="preschool education"
    params["degree"]=1
    params["upLimit"]=20
    params["tSort"]="down"
    
    request_content(request_url,params,method)
    
if __name__ == '__main__':
    main()

Of course, it can also be implemented with nodejs

var http = require('http'); 
var qs = require('querystring'); 

//Configure the appKey and openId you applied for
app_key = "***";
open_id = "***";
 
function request_content(request_url,port,params,method){
    
    var path=request_url;
    if(!!params){
        var content = qs.stringify(params);  
        path=request_url+'?' + content;
    }
    
    var options = { 
        port: port,
        path: path,
        method: method
    }; 
    
    if(method.toLowerCase()=='post'){
        options['headers']="Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8";
    }

    var req = http.request(options, function (res) { 
        res.setEncoding('utf8'); 
        res.on('data', function (chunk) { 
            console.log(chunk); 
        }); 
    });  
    
    req.on('error', function (e) { 
        console.log('problem with request: ' + e.message); 
    }); 
    
    req.end();
}

function main(){

    var domain="http://api.xiaocongjisuan.com/";
    var port=8080;//http corresponds to 80 ports, https corresponds to 443 ports, please users correct themselves
    var servlet="data/relativeword/mining";
    var method="get";
    var request_url=domain+servlet;

    var params = {}; 
    params['appKey']=app_key;
    params['openId']=open_id;
    
    //Change section
    params["keyword"]="preschool education";
    params["degree"]=1;
    params["upLimit"]=20;
    params["tSort"]="down";
    
    request_content(request_url,port,params,method);
}

main();

How to use other languages? Click on me to see This is the case. Well, it's actually very simple, and there's no more redundancy.

Posted by ojsimon on Wed, 09 Oct 2019 18:17:15 -0700