• Qt词典搜索


    Qt词典搜索

    采用阿凡达数据-API数据接口及爱词霸API数据接口实现词典搜索功能,实例字符串搜索接口分别为:中文词组采用“词典”,中文单个字采用“中华字典”,英文或其他字符采用“爱词霸”;
    对应的API接口:
    http://api.avatardata.cn/CiHai/LookUp?key=[申请的APPKEY]&keyword=[搜索内容]
    http://api.avatardata.cn/XinHuaZiDian/LookUp?key=[申请的APPKEY]&content=[搜索内容]
    http://dict-co.iciba.com/api/dictionary.php?key=[申请的APPKEY]&w=[搜索内容]

    1.字符串搜索分类

    void DictLookUpKeyRequest(const QString &strSearchKey)
    {
        QString strDictLookupKey = strSearchKey.trimmed();
        if (strDictLookupKey.isEmpty())
        {
            emit DictLookUpKeyFinished(false, "---INVALID_URL---");
            return;
        }
    
        QString strLookUpUrl = "";    
        if (strDictLookupKey.contains(QRegExp("[\x4e00-\x9fa5]")))
        {
            //search key contains Chinese character
            if (strDictLookupKey.contains(QRegExp("[A-Za-z0-9]")))
            {
                emit DictLookUpKeyFinished(false, "---NO_SUPPORT---");
                return;
            }
    
            if (strDictLookupKey.size() >= 2)
            {
                strLookUpUrl = QString("http://api.avatardata.cn/CiHai/LookUp?key=d19e2a742a7b467c83a63bdd263b371b&keyword=%1").arg(strSearchKey);            
            }
            else
            {
                strLookUpUrl = QString("http://api.avatardata.cn/XinHuaZiDian/LookUp?key=175dcbe2c06e49db9eef7f76045ddb55&content=%1").arg(strSearchKey);
            }
        }
        else
        {
            //search other key
            strLookUpUrl = QString("http://dict-co.iciba.com/api/dictionary.php?key=B602AC1E01426961CADF60B71CA97484&w=%1").arg(strSearchKey);
        }
    
        HttpDictLookUpRequest(strLookUpUrl); //QNetworkRequest
    }

    2.QNetworkRequest搜索网络请求

    void HttpDictLookUpRequest(const QUrl& url)
    {
        QNetworkReply *reply = mpManager->get(QNetworkRequest(url));
    
        connect(reply, &QNetworkReply::finished, this, [this]()
        {
            QNetworkReply* reply = static_cast<QNetworkReply*>(sender());
            reply->deleteLater();
    
            if (reply->error())
            {
                emit HttpGetRequestFinished(false, "");
            }
            else
            {
                emit HttpGetRequestFinished(true, reply->readAll());
            }
        });
    }

    3.搜索返回数据处理

    void OnDictLookUpResult(bool bFinsihed, const QString& DictLookUpKeyReplyStr)
    {
        QString strTextResult = QString("< font color=#6e6e6e >%1</font>").arg(tr("The query word is not entered in the dictionary, please try again later"));
        if (bFinsihed)
        {
            if (DictLookUpKeyReplyStr.contains("</dict>"))
            {
                //iciba search result
                if (!DictLookUpKeyReplyStr.contains("<acceptation>"))
                {
                    mpTextEdit->setText(strTextResult);
                    return;
                }
    
                QXmlStreamReader reader(DictLookUpKeyReplyStr);
                while (!reader.atEnd())
                {
                    reader.readNext();
                    if (reader.isStartElement())
                    {
                        if (reader.name().contains("key"))
                        {
                            strTextResult = "";
                            strTextResult.append(QString("< font color=#4a4a4a ><b>%1</b></font>").arg(reader.readElementText()));
                        }
                        else if (reader.name().contains("pos") || reader.name().contains("acceptation"))
                        {
                            strTextResult.append(QString("< font color=#6e6e6e ><br>%1</font>").arg(reader.readElementText()));
                        }                    
                    }
                }
            }
            else
            {
                QJsonParseError jsonParseError;
                QJsonDocument jsonDocument = QJsonDocument::fromJson(DictLookUpKeyReplyStr.toUtf8(), &jsonParseError);
                if (jsonParseError.error == QJsonParseError::NoError)
                {
                    QJsonObject jsonObj = jsonDocument.object();
                    int returnValue = jsonObj.value("return_code").toInt();
                    if (returnValue == 0)
                    {
                        QJsonObject resultJsonObj = jsonObj.value("result").toObject();
                        if (!resultJsonObj.value("words").toString().isEmpty())
                        {
                            //CiHai search result
                            strTextResult = QString("< font color=#4a4a4a ><b>%1</b></font>").arg(resultJsonObj.value("words").toString());
                            strTextResult.append(QString("< font color=#6e6e6e ><br>%1</font>").arg(resultJsonObj.value("content").toString()));
    
                        }
                        else
                        {
                            //XinHuaZiDian search result
                            QJsonArray resultArray = jsonObj.value("result").toArray(); 
                            if (resultArray.size() > 0)
                            {
                                QJsonObject resultArrayJsonObj = resultArray.at(0).toObject();
                                strTextResult = QString("< font color=#4a4a4a ><b>%1</b></font>").arg(resultArrayJsonObj.value("hanzi").toString());
                                if (!resultArrayJsonObj.value("jianjie").toString().isEmpty())
                                {
                                    strTextResult.append(QString("< font color=#6e6e6e ><br>%1</font>").arg(resultArrayJsonObj.value("jianjie").toString()));
                                }
                                if (!resultArrayJsonObj.value("xiangjie").toString().isEmpty())
                                {
                                    strTextResult.append(QString("< font color=#6e6e6e ><br>%1</font>").arg(resultArrayJsonObj.value("xiangjie").toString()));
                                }                                
                            }
                        }
                    }
                }
    
            }        
        }
    
        mpTextEdit->setText(strTextResult);
    }
  • 相关阅读:
    webapi之fiddler头设置
    ios---setContentOffset
    webapi参数处理get过个参数
    socket网络编程
    logging模块
    configparser模块(拷贝)
    hashlib模块--摘要算法
    异常处理
    面向对象拓展
    反射
  • 原文地址:https://www.cnblogs.com/sz-leez/p/5910443.html
Copyright © 2020-2023  润新知