Merge pull request #286 from shenlebantongying/staged

lingua: add safeguards to errors
This commit is contained in:
xiaoyifang 2022-12-28 14:28:29 +08:00 committed by GitHub
commit ce2eba6c63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -30,33 +30,30 @@ namespace {
{ {
/* map of iso lang code to wikipedia lang id /* map of iso lang code to wikipedia lang id
Data was obtained by this query on https://commons-query.wikimedia.org/ Data was obtained by this query on https://commons-query.wikimedia.org/
SELECT ?language ?languageLabel ?iso ?audios
WHERE {
{
SELECT ?language (COUNT(?audio) AS ?audios) WHERE {
?audio # Filter: P2 'instance of' is Q2 'record'
wdt:P407 ?language .
}
GROUP BY ?language
}
SELECT ?language ?languageLabel ?iso ?audios SERVICE <https://query.wikidata.org/sparql> {
WHERE { ?language wdt:P220 ?iso . # Assign value: P220 'ISO-639-3' into ?iso.
{ }
SELECT ?language (COUNT(?audio) AS ?audios) WHERE {
# Comment out the below statement to filter to only certain languages (e.g. Q34 or others)
# VALUES ?language { entity:Q34 }
?audio # Filter: P2 'instance of' is Q2 'record'
wdt:P407 ?language .
}
GROUP BY ?language
}
SERVICE <https://query.wikidata.org/sparql> {
?language wdt:P220 ?iso . # Assign value: P220 'ISO-639-3' into ?iso.
}
SERVICE <https://query.wikidata.org/sparql> { SERVICE <https://query.wikidata.org/sparql> {
SERVICE wikibase:label { SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
?language rdfs:label ?languageLabel . ?language rdfs:label ?languageLabel .
}
} }
} }
}
*/ */
const map< string, string > iso_to_wikipedia_id = { { "grc", "Q35497" }, const map< string, string > iso_to_wikipedia_id = { { "grc", "Q35497" },
@ -313,13 +310,12 @@ namespace {
sptr< DataRequest > getArticle( sptr< DataRequest > getArticle(
wstring const & word, vector< wstring > const & alts, wstring const &, bool ) override wstring const & word, vector< wstring > const & alts, wstring const &, bool ) override
{ {
if( word.size() > 50 ) if( word.size() < 50 )
{
return std::make_shared< DataRequestInstant >( false );
}
else
{ {
return std::make_shared< LinguaArticleRequest >( word, alts, languageCode,langWikipediaID, getId(), netMgr ); return std::make_shared< LinguaArticleRequest >( word, alts, languageCode,langWikipediaID, getId(), netMgr );
} else {
return std::make_shared< DataRequestInstant >( false );
} }
} }
@ -334,7 +330,7 @@ namespace {
} }
}; };
} } // namespace
vector< sptr< Dictionary::Class > > makeDictionaries( vector< sptr< Dictionary::Class > > makeDictionaries(
Dictionary::Initializing &, Config::Lingua const & lingua, QNetworkAccessManager & mgr ) Dictionary::Initializing &, Config::Lingua const & lingua, QNetworkAccessManager & mgr )
@ -383,18 +379,20 @@ void LinguaArticleRequest::addQuery( QNetworkAccessManager & mgr, const wstring
R"(&iiprop=url)" R"(&iiprop=url)"
R"(&iimetadataversion=1)" R"(&iimetadataversion=1)"
R"(&iiextmetadatafilter=Categories)" R"(&iiextmetadatafilter=Categories)"
R"(&gsrsearch=intitle:LL-%1 \(%2\)-.*-%3\.wav/)" R"(&gsrsearch=intitle:LL-%1 \(%2\)-.*-%3\.wav/)" // https://en.wikipedia.org/wiki/Help:Searching/Regex
R"(&gsrnamespace=6)" R"(&gsrnamespace=6)"
R"(&gsrlimit=10)" R"(&gsrlimit=10)"
R"(&gsrwhat=text)"; R"(&gsrwhat=text)";
reqUrl = reqUrl.arg(languageCode,langWikipediaID,QString::fromStdU32String( word ) ); reqUrl = reqUrl.arg(langWikipediaID,languageCode,QString::fromStdU32String( word ) );
qDebug()<< "lingualibre query " << reqUrl; qDebug()<< "lingualibre query " << reqUrl;
auto netRequest = QNetworkRequest( reqUrl );
netRequest.setTransferTimeout(3000);
auto netReply = auto netReply =
std::shared_ptr< QNetworkReply >( mgr.get( std::shared_ptr< QNetworkReply >( mgr.get(netRequest));
QNetworkRequest( reqUrl ) ) );
netReplies.emplace_back( netReply, Utf8::encode( word ) ); netReplies.emplace_back( netReply, Utf8::encode( word ) );
@ -404,10 +402,17 @@ void LinguaArticleRequest::addQuery( QNetworkAccessManager & mgr, const wstring
void LinguaArticleRequest::requestFinished( QNetworkReply * r ) void LinguaArticleRequest::requestFinished( QNetworkReply * r )
{ {
qDebug() << "Lingua query finished"; qDebug() << "Lingua query finished ";
sptr< QNetworkReply > netReply = netReplies.front().reply; sptr< QNetworkReply > netReply = netReplies.front().reply;
if( isFinished() || // Was cancelled
!netReply->isFinished() ||
netReply->error() != QNetworkReply::NoError ){
qWarning()<< "Lingua query failed: " << netReply->error();
return;
}
QJsonObject resultJson = QJsonDocument::fromJson( netReply->readAll() ).object(); QJsonObject resultJson = QJsonDocument::fromJson( netReply->readAll() ).object();
/* /*
@ -488,6 +493,7 @@ void LinguaArticleRequest::requestFinished( QNetworkReply * r )
else else
{ {
hasAnyData = false; hasAnyData = false;
finish();
} }
} }