美文网首页
阿里云文本分析-文本关键字 Swift

阿里云文本分析-文本关键字 Swift

作者: 张鹏飞_6b83 | 来源:发表于2018-06-08 22:20 被阅读0次

    关于阿里云API的Swift语言桥接,首先要进行APPCODE的获取,这里就不多废话了,阿里云官网有教程。
    阿里云控制台;https://home.console.aliyun.com/new#/

    接下来直接上代码:

    funcpostData(_text:String,_completionHandler:@escaping(_result: [String:Any]) ->Void)  {
    
        let host = "http://gjccq.market.alicloudapi.com/rest/160601/text_analysis/key_words_extraction.json"
    
        varrequest =URLRequest(url:URL(string: host)!)
    
        request.httpMethod="POST"
    
        //请求添加头Header
        request.addValue("APPCODE 你的APPCODE", forHTTPHeaderField:"Authorization")
    
        //添加Json返回格式
        request.addValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type")
    
        request.cachePolicy = URLRequest.CachePolicy(rawValue: 1)!
    
        request.timeoutInterval = 5
    
        let body = "{\"inputs\":[{\"text\":{\"dataType\":50,\"dataValue\":\"\(text)\"},\"config\":{\"dataType\":50,\"dataValue\":\"{\\\"topN\\\":3,\\\"similarityType\\\":\\\"lcs\\\",\\\"delimiter\\\":\\\"。!?\\\"}\"}}]}"
    
        print(#line,body)
    
        request.httpBody= body.data(using:String.Encoding.utf8)!
    
        URLSession.shared.dataTask(with: request) { data, response, errorin
    
            if error == nil &&  data != nil{
    
                do{
    
                    letdic =tryJSONSerialization.jsonObject(with: data!, options: [])as! [String:Any]
    
                    letarray = dic["outputs"]as! [Dictionary]
    
                    completionHandler(array[0])
    
                }catch  {
    
                    print(#line, error)
    
                }
    
                return
    
            }
    
            }.resume() //[task resume];
    
    
    }
    

    这个是封装好的API桥接方法,接下来是调用:

      postData("你要分析的文本") { (result) in
                
                DispatchQueue.main.async(execute: {() -> Void in
    
                    print(#line, result)
                    let outputValue = result["outputValue"] as! [String:Any]
                    
                    let dataValue = outputValue["dataValue"] as! String
                    
                    var resultDic: [String: Any]? = nil
    
                    if let anEncoding = dataValue.data(using: .utf8) {
                        resultDic = try! JSONSerialization.jsonObject(with: anEncoding, options: []) as? [String: Any]
                    }
                    
                    let wordsweight = resultDic!["words weight"] as! [Any]
                    
                    var myArray = [String]()
    
                    for i in 0..<wordsweight.count {
    
                        let wordArray = wordsweight[i] as! [String : Any]
    
                        let word = wordArray["word"] as! String
    
                        myArray.append(word)
                        print(#line,myArray)
                    }
                })
            }
    

    最终打印结果就是我们拿到的关键字

    相关文章

      网友评论

          本文标题:阿里云文本分析-文本关键字 Swift

          本文链接:https://www.haomeiwen.com/subject/vfdzsftx.html