function doesQuestionIncludesVerb($extendedQueryArr) { foreach ($extendedQueryArr as $word => $pos) { if (posIsVerb($pos)) { if ($word != "is" && $word != "are") { return true; } } } return false; }
function extendQueryWordsByConceptTaxRelations($extendedQueryArr, $lang, $isQuestion = false) { global $is_a_relation_name_ar, $thing_class_name_en, $thing_class_name_ar, $TRANSLATION_MAP_EN_TO_AR; $conceptsFromTaxRelations = array(); if ($lang == "EN") { $thing_class_name = strtolower($thing_class_name_en); } else { $thing_class_name = $thing_class_name_ar; } $questionIncludesVerb = $isQuestion && doesQuestionIncludesVerb($extendedQueryArr); foreach ($extendedQueryArr as $word => $pos) { // ignore any tern which is not nound or verb // [0-9]+ to allow normal non pos tagged queries - incase of pharase search if (!preg_match("/NN|VB|[0-9]+/", $pos)) { continue; } /* * Differentiate between word and concept ID, in English 'word' is needed unmapped for 'verb' search */ if ($lang == "EN") { //corresponding arabic Concept - only if it is a concept //$conceptIDStr = $MODEL_QA_ONTOLOGY['CONCEPTS_EN_AR_NAME_MAP'][$word]; $conceptIDStr = getModelEntryFromMemory("ALL", "MODEL_QA_ONTOLOGY", "CONCEPTS_EN_AR_NAME_MAP", $word); } else { $conceptIDStr = $word; } //!$questionIncludesVerb since if the question includes a verb then the user is not looking ofr is-a relation if (!$questionIncludesVerb && modelEntryExistsInMemory("ALL", "MODEL_QA_ONTOLOGY", "CONCEPTS", $conceptIDStr)) { //$inboundRelationsArr = $MODEL_QA_ONTOLOGY['GRAPH_INDEX_TARGETS'][$conceptIDStr]; $inboundRelationsArr = getModelEntryFromMemory("ALL", "MODEL_QA_ONTOLOGY", "GRAPH_INDEX_TARGETS", $conceptIDStr); //$outboundRelationsArr = $MODEL_QA_ONTOLOGY['GRAPH_INDEX_SOURCES'][$conceptIDStr]; $outboundRelationsArr = getModelEntryFromMemory("ALL", "MODEL_QA_ONTOLOGY", "GRAPH_INDEX_SOURCES", $conceptIDStr); // FOR INBOUND IS-A RELATIONS EX: X IS AN ANIMAL($word) foreach ($inboundRelationsArr as $index => $relationArr) { $subject = $relationArr['source']; $verbAR = $relationArr['link_verb']; $subjectConceptArr = getModelEntryFromMemory("ALL", "MODEL_QA_ONTOLOGY", "CONCEPTS", $subject); if ($lang == "EN") { $subject = trim(removeBasicEnglishStopwordsNoNegation($subjectConceptArr['label_en'])); } /// CLEAN AND REPLACE CONCEPT $subject = cleanWordnetCollocation($subject); /////////////////////////// //TODO: check if this is needed $subject!=$thing_class_name if ($verbAR == $is_a_relation_name_ar && $subject != $thing_class_name) { // ignore phrase parent concepts // عذاب + عذاب الله if (strpos($subject, $conceptIDStr) === false) { $conceptsFromTaxRelations[] = $subject; } } } if ($isQuestion) { // FOR OUTBOUND IS-A RELATIONS EX: X($word) IS A PERSON foreach ($outboundRelationsArr as $index => $relationArr) { $verbAR = $relationArr['link_verb']; $object = $relationArr['target']; $objectConceptArr = getModelEntryFromMemory("ALL", "MODEL_QA_ONTOLOGY", "CONCEPTS", $object); if ($lang == "EN") { $object = trim(removeBasicEnglishStopwordsNoNegation($objectConceptArr['label_en'])); } /// CLEAN AND REPLACE CONCEPT $object = cleanWordnetCollocation($object); /////////////////////////// if ($verbAR == $is_a_relation_name_ar && $object != $thing_class_name) { //echoN(" $object!=$thing_class_name"); //echoN("|$thing_class_name|$object|"); // ignore phrase parent concepts // عذاب + عذاب الله if (strpos($object, $conceptIDStr) === false) { $conceptsFromTaxRelations[] = $object; } } } } } if (!$isQuestion) { ///////// add concept name to query if the current query word is found to be synonym to that concept if (modelEntryExistsInMemory("ALL", "MODEL_QA_ONTOLOGY", "SYNONYMS_INDEX", $word)) { //$conceptNameAR = $MODEL_QA_ONTOLOGY['SYNONYMS_INDEX'][$word]; $conceptNameAR = getModelEntryFromMemory("ALL", "MODEL_QA_ONTOLOGY", "SYNONYMS_INDEX", $word); $finalConceptName = $conceptNameAR; if ($lang == "EN") { $conceptArr = getModelEntryFromMemory("ALL", "MODEL_QA_ONTOLOGY", "CONCEPTS", $conceptNameAR); $finalConceptName = $conceptArr['label_en']; } $conceptsFromTaxRelations[] = $finalConceptName; } ////////////////////////////////////////////////////////////// } //$lang=="AR" check since AR words are not PoS tagged yet if ($isQuestion && ($lang == "AR" || posIsVerb($pos))) { if (($verbArr = getModelEntryFromMemory("ALL", "MODEL_QA_ONTOLOGY", "VERB_INDEX", $word)) != null || ($verbArr = isWordPartOfAVerbInVerbIndex($word, $lang))) { foreach ($verbArr as $index => $verbSTArr) { $subject = $verbSTArr['SUBJECT']; $object = $verbSTArr['OBJECT']; if ($lang == "EN") { $subjectConceptArr = getModelEntryFromMemory("ALL", "MODEL_QA_ONTOLOGY", "CONCEPTS", $wordConveretedToConceptID); $objectConceptArr = getModelEntryFromMemory("ALL", "MODEL_QA_ONTOLOGY", "CONCEPTS", $wordConveretedToConceptID); $object = trim(removeBasicEnglishStopwordsNoNegation($objectConceptArr['label_en'])); $subject = trim(removeBasicEnglishStopwordsNoNegation($subjectConceptArr['label_en'])); } // we are not interested in X is_a Thing - does not add value if ($object == $thing_class_name) { continue; } //echoN("-$subject>$word>$object"); //echoN(" $object!=$thing_class_name"); if (isset($extendedQueryArr[$subject])) { $conceptsFromTaxRelations[] = $object; } else { if (isset($extendedQueryArr[$object])) { $conceptsFromTaxRelations[] = $subject; } } } } } } $conceptsFromTaxRelations = array_unique($conceptsFromTaxRelations); //preprint_r($conceptsFromTaxRelations); return $conceptsFromTaxRelations; }