nlp.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Raccatto i parametri dall'URL -- mi aspetto un parametro di nome 'link'!
  2. thisUrlParams = {};
  3. window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
  4. thisUrlParams[key] = value;
  5. });
  6. console.log('URL get params: ', thisUrlParams);
  7. // Funzioni per eseguire le queries
  8. function prepareQueryURL(query){
  9. sparqlEndpoint = 'http://dev.restore.ovi.cnr.it:8890/sparql/';
  10. sparqlUrlParams = '?default-graph-uri=&query=' + encodeURIComponent(query) + '&output=json&callback=?';
  11. return sparqlEndpoint + sparqlUrlParams;
  12. }
  13. function doJsonQuery(query){
  14. queryURL = prepareQueryURL(query);
  15. response = $.ajax({//OGGETTO
  16. url: queryURL,
  17. dataType: "json",
  18. success: function (data){},
  19. error: function (e) {}
  20. });
  21. return response;
  22. }
  23. // Funzioni per raccattare + stringhificare l'output
  24. queryStringOutput = "";
  25. function stringifyResponse(val){
  26. resultArray = val['results']['bindings'];
  27. out = "";
  28. for(i = 0; i < resultArray.length; i++){
  29. out = out + JSON.stringify(resultArray[i])
  30. }
  31. queryStringOutput = (queryStringOutput + out).replace("}{",",");
  32. }
  33. queryTopoOutput = "";
  34. function stringifyTopo(val){
  35. resultArray = val['results']['bindings'];
  36. out = "";
  37. for(i = 0; i < resultArray.length; i++){
  38. out = out + JSON.stringify(resultArray[i])
  39. }
  40. queryStringOutput = (queryStringOutput + out).replace("}{",",");
  41. }
  42. ////////////////////
  43. // TESTI DELLE QUERY
  44. ////////////////////
  45. prefixes = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \
  46. PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \
  47. PREFIX crm: <http://www.cidoc-crm.org/cidoc-crm/> \
  48. PREFIX dat: <http://datini.archiviodistato.prato.it/la-ricerca/scheda/> \
  49. PREFIX mpp: <http://palazzopretorio.comune.prato.it/it/le-opere/alcuni-capolavori/> \
  50. PREFIX aut: <http://palazzopretorio.comune.prato.it/it/opere/autori/>"
  51. queryTesto = prefixes + " SELECT DISTINCT ?titolo ?testo WHERE {?subject crm:P128_carries <" + thisUrlParams.link + ">;\
  52. crm:P128_carries ?object .\
  53. ?object rdf:type crm:E33_Linguistic_Object;\
  54. crm:P190_has_symbolic_content ?testo;\
  55. rdfs:label ?titolo .}"
  56. queryLocations = prefixes + "select ?b where {\
  57. ?a <http://www.cidoc-crm.org/cidoc-crm/P2_has_type> 'Toponimo' .\
  58. ?a <http://www.w3.org/2000/01/rdf-schema#label> ?b .\
  59. }";
  60. console.log()
  61. async function fillPageContents(){
  62. // val = await doJsonQuery(queryLocations);
  63. // stringifyResponse(val);
  64. val = await doJsonQuery(queryTesto);
  65. stringifyResponse(val);
  66. putValuesInHTML();
  67. $.ajax({
  68. url: '../python/nlp.py',
  69. type: 'POST',
  70. data: {places: ["prato", "firenze", "avignone", "siena"], text: queryStringOutput},
  71. dataType: "json",
  72. success: function(data){
  73. alert(data);
  74. }
  75. });
  76. }
  77. function putValuesInHTML(){
  78. queryString = queryStringOutput.replaceAll("}}{", "},")
  79. console.log("Stringified Response:", queryString);
  80. queryOutput = JSON.parse(queryString);
  81. //
  82. testo = "<p>" + queryOutput.testo.value + "</p>";
  83. document.getElementById("testo_OVI").innerHTML = testo;
  84. document.getElementById("titolo_lettera").innerHTML = queryOutput.titolo.value;
  85. //
  86. // Query ancora farlocche
  87. }
  88. // Dà il via alle queries + riempimento HTML
  89. fillPageContents();