bool Node::match(QStringList::const_iterator input, const QStringList &inputWords, const QString ¤tThat, const QString ¤tTopic, QStringList &capturedThatTexts, QStringList &capturedTopicTexts, QList<long> &categoriesId, Leaf **leaf) { if (input == inputWords.end()) return false; if ((word == "*") || (word == "_")) { ++input; for (;input != inputWords.end(); input++) { for (Node *child = childs.first(); child; child = childs.next()) { if (child->match(input, inputWords, currentThat, currentTopic, capturedThatTexts, capturedTopicTexts, categoriesId, leaf)) return true; } } } else { if (!word.isEmpty()) { if (word != *input) return false; ++input; } for (Node *child = childs.first(); child; child = childs.next()) { if (child->match(input, inputWords, currentThat, currentTopic, capturedThatTexts, capturedTopicTexts, categoriesId, leaf)) return true; } } if (input == inputWords.end()) { for (*leaf = leafs.first(); *leaf; *leaf = leafs.next()) { capturedThatTexts.clear(); capturedTopicTexts.clear(); if ( (!(*leaf)->that.isEmpty() && !exactMatch((*leaf)->that, currentThat, capturedThatTexts)) || // start lvk - Force match with current topic, if no match retry without topic (!exactMatch((*leaf)->topic, currentTopic, capturedTopicTexts)) ) //(!(*leaf)->topic.isEmpty() && !exactMatch((*leaf)->topic, currentTopic, capturedTopicTexts)) ) // end lvk continue; return true; } } return false; }
bool NDalign::makeNullHit(void) { _hits.push_back(exactMatch(0, 0, 0)); return(true); }
bool NDalign::findHits(void) { for (map<uint64,int32>::iterator bit=_bMap.begin(); bit != _bMap.end(); bit++) { uint64 kmer = bit->first; int32 bpos = bit->second; if (bpos == INT32_MAX) // Exists too many times in bSeq, don't care about it. continue; int32 apos = _aMap[kmer]; assert(apos != INT32_MAX); // Should never get a bMap if the aMap isn't set if ((apos - bpos < _minDiag) || (apos - bpos > _maxDiag)) fprintf(stderr, "NDalign::findHits()-- kmer "F_X64" apos - bpos = %d _minDiag = %d _maxDiag = %d\n", kmer, apos-bpos, _minDiag, _maxDiag); assert(apos - bpos >= _minDiag); // ...these too. assert(apos - bpos <= _maxDiag); _rawhits.push_back(exactMatch(apos, bpos, _merSize)); } #ifdef DEBUG_ALGORITHM fprintf(stderr, "NDalign::findHits()-- Found %u hits.\n", _rawhits.size()); #endif return(true); }
static void checkAccessMux( sout_stream_t *p_stream, char *psz_access, char *psz_mux ) { if( exactMatch( psz_access, "mmsh", 4 ) && !exactMatch( psz_mux, "asfh", 4 ) ) msg_Err( p_stream, "mmsh output is only valid with asfh mux" ); else if( !exactMatch( psz_access, "file", 4 ) && ( exactMatch( psz_mux, "mov", 3 ) || exactMatch( psz_mux, "mp4", 3 ) ) ) msg_Err( p_stream, "mov and mp4 mux are only valid with file output" ); else if( exactMatch( psz_access, "udp", 3 ) ) { if( exactMatch( psz_mux, "ffmpeg", 6 ) || exactMatch( psz_mux, "avformat", 8 ) ) { /* why would you use ffmpeg's ts muxer ? YOU DON'T LOVE VLC ??? */ char *psz_ffmpeg_mux = var_CreateGetString( p_stream, "sout-avformat-mux" ); if( !psz_ffmpeg_mux || strncmp( psz_ffmpeg_mux, "mpegts", 6 ) ) msg_Err( p_stream, "UDP output is only valid with TS mux" ); free( psz_ffmpeg_mux ); } else if( !exactMatch( psz_mux, "ts", 2 ) ) msg_Err( p_stream, "UDP output is only valid with TS mux" ); } }
DataStore::IQualifierPtrH parseFilterExpression(const std::string& expression, const DataStore::IFieldDescriptorConstList& fields) { // // For now, only support FIELD="value" // // Hack some reasonably large values and use sscanf as a quick & dirty parser char fieldName[64]; char expectedValue[1024]; // Doesn't really work for all types of data, but should work for example // data sets. int parsedItems = sscanf(expression.c_str(), "%64[^=]=%1024[^\n]", fieldName, expectedValue); if (parsedItems != 2) { throw std::runtime_error("Syntax error in filter expression"); } DataStore::IFieldDescriptorConstPtrH field = findFieldByName(fieldName, fields); if (!field) { std::string ex = "Unrecognized field \""; ex += fieldName; ex += "\" specified in filter expression"; throw std::runtime_error(ex); } DataStore::ValuePtrH desiredValue = field->fromString(expectedValue); if (!desiredValue) { std::string ex = "Syntax error in filter expression, value format is incorrect"; throw std::runtime_error(ex); } DataStore::IQualifierPtrH exactMatch(new DataStore::Logic::Exact(field, desiredValue)); return exactMatch; }
bool RectPacker::pack(int32_t sourceRectId) { // try to find an exactly matching node for the given sourceRectId // if there is none, we split the current node until we find one foundNode = false; // will be set to true if we actually can create a macthing node recursionStack.clear(); recursionStack.push_back(0); // start at the rootNode SourceRect& currentSourceRect = sourceRects[sourceRectId]; // check if current rect is degenerate and abort immediately if((currentSourceRect.rect.width == 0) || (currentSourceRect.rect.height == 0)) { // DOUT("rejecting degenerate rect"); return false; } // DOUT("------------------ "<<sourceRectId<<" "<<currentSourceRect.rect); // loop while we haven't found a match and still have space // the last entry in the stack will be the node we have to work with uint32_t iterations = 0; while(!foundNode && (recursionStack.size() > 0)) { iterations++; int32_t currentNodeIndex = recursionStack.back(); Node currentNode = nodes[currentNodeIndex]; // currentNode needs to be copied since we change the vector and the memory might be moved // DOUT("currentNodeIndex: "<<currentNodeIndex<<" stack size: "<<recursionStack.size()<<" rect: "<<currentNode.rect); // terminate if the currentNode is not in use yet and matches exactly if(!currentNode.hasBitmap() && !currentNode.hasChildren() && exactMatch(currentNode, currentSourceRect)) { foundNode = true; // currentNode.rect = currentSourceRect.rect; currentNode.rectid = currentSourceRect.id; // DOUT("///////////////////////////////////////// found match, stack size: "<<recursionStack.size()); nodes[currentNodeIndex] = currentNode; } else { // only split and recurse if node is not in use yet and sourceRect fits into this node if((currentNode.rectid == -1) && fits(currentNode, currentSourceRect)) { // make sure currentNode has children split(currentNode, currentSourceRect); // pop current node and add children if existent // add smaller child last so it will get checked first recursionStack.pop_back(); if((currentNode.large != -1) && (nodes[currentNode.large].rectid==-1)) recursionStack.push_back(currentNode.large); if((currentNode.tiny != -1) && (nodes[currentNode.tiny].rectid==-1)) recursionStack.push_back(currentNode.tiny); // DOUT("stack size after split: "<<recursionStack.size()); // DOUT("node ids after split: "<<currentNode.tiny << " " << currentNode.large); nodes[currentNodeIndex] = currentNode; } else { // just pop the node off the stack and continue // DOUT("won't split node that's already in use"); recursionStack.pop_back(); } } } // DOUT("iterations: "<<iterations<<" nodes: "<<nodes.size()); sumIter += iterations; sumNodes += nodes.size(); // checkOverlap(); return foundNode; }
QString AIMLParser::getResponse(QString input, QList<long> &categoriesId, const bool &srai) { //debug if (srai) _indent ++; QString indentSpace = QString().fill(' ', 2*_indent); _logStream << (!srai ? "\n" : "") + indentSpace + (srai ? "::SRAI: " : "::User Input: ") + input + "\n"; //perform substitutions for input string QVALUELIST_CLASSNAME<QRegExp>::Iterator itOld = _subOld.begin(); QStringList::Iterator itNew = _subNew.begin(); for (; itOld != _subOld.end(); ++itOld, ++itNew ) input.replace(*itOld, *itNew); if (!srai) { _inputList.prepend(input); if (_inputList.count() > MAX_LIST_LENGTH) _inputList.pop_back(); } QStringList capturedTexts, capturedThatTexts, capturedTopicTexts; QString curTopic = _parameterValue["topic"]; normalizeString(curTopic); _logStream << "::Current Topic: " << curTopic; Leaf *leaf = NULL; QString result(""); QStringList sentences = QStringList::split(QRegExp("[\\.\\?!,;\\x061f]"), input); QStringList::Iterator sentence = sentences.begin(); while (sentence != sentences.end()) { if (sentence != sentences.begin()) result += " "; //normalizeString(*sentence); *sentence = (*sentence).lower(); QStringList inputWords = QStringList::split(' ', *sentence); QStringList::ConstIterator it = inputWords.begin(); if (!_root.match(it, inputWords, _thatList.count() && _thatList[0].count() ? _thatList[0][0] : QString(""), curTopic, capturedThatTexts, capturedTopicTexts, categoriesId, &leaf)) { // start lvk - force match with current topic, if no match retry without topic if (curTopic.isEmpty()) { // end lvk return "Internal Error!"; // start lvk - force match with current topic, if no match retry without topic } else { _parameterValue["topic"] = ""; return getResponse(input, categoriesId, srai); } // end lvk } Node *parentNode = leaf->parent; QString matchedPattern = parentNode->word; while (parentNode->parent->parent) { parentNode = parentNode->parent; matchedPattern = parentNode->word + " " + matchedPattern; } _logStream << indentSpace + "::Matched pattern: [" + matchedPattern + "]"; if (!leaf->that.isEmpty()) _logStream << " - Matched that: [" + leaf->that + "]"; if (!leaf->topic.isEmpty()) _logStream << " - Matched topic: [" + leaf->topic + "]"; _logStream << "\n"; capturedTexts.clear(); exactMatch(matchedPattern, *sentence, capturedTexts); //strip whitespaces from the beggining and the end of result if (_visitedNodeList.contains(&leaf->tmplate)) result += "ProgramQ: Infinite loop detected!"; else { _visitedNodeList.append(&leaf->tmplate); categoriesId.append(leaf->id); result += resolveNode(&leaf->tmplate, categoriesId, capturedTexts, capturedThatTexts, capturedTopicTexts).stripWhiteSpace(); } ++sentence; } if (!srai) { QString tempResult = result.simplifyWhiteSpace(); //get the sentences of the result splitted by: . ? ! ; and "arabic ?" QStringList thatSentencesList = QStringList::split(QRegExp("[\\.\\?!;\\x061f]"), tempResult); QStringList inversedList; for (QStringList::Iterator it = thatSentencesList.begin(); it != thatSentencesList.end(); ++it) { //perform substitutions for that string itOld = _subOld.begin(); itNew = _subNew.begin(); for (; itOld != _subOld.end(); ++itOld, ++itNew ) tempResult.replace(*itOld, *itNew); normalizeString(*it); inversedList.prepend(*it); } _thatList.prepend(inversedList); if (_thatList.count() > MAX_LIST_LENGTH) _thatList.pop_back(); _visitedNodeList.clear(); } //debug _logStream << indentSpace + "::Result: " + result + "\n"; if (srai) _indent --; return result; }
//recursively replace all the values & return the QString result QString AIMLParser::resolveNode(QDomNode* node, QList<long> &categoriesId, const QStringList &capturedTexts, const QStringList &capturedThatTexts, const QStringList &capturedTopicTexts) { QString result(""); QString nodeName = node->nodeName(); QDomElement element = node->toElement(); if (nodeName == "random") { QVALUELIST_CLASSNAME<QDomNode> childNodes = elementsByTagName(node, "li"); uint childCount = childNodes.count(); uint random = rand() % childCount; QDomNode child = childNodes[random]; result = resolveNode(&child, categoriesId, capturedTexts, capturedThatTexts, capturedTopicTexts); } else if (nodeName == "condition") { QString name(""); uint condType = 2; if (element.hasAttribute("name")) { condType = 1; name = element.attribute("name"); if (element.hasAttribute("value")) { condType = 0; QString value = element.attribute("value").upper(); QStringList dummy; if (exactMatch(value, _parameterValue[name].upper(), dummy)) { //dirty trick to avoid infinite loop ! element.setTagName("parsedCondition"); result = resolveNode(&element, categoriesId, capturedTexts, capturedThatTexts, capturedTopicTexts); element.setTagName("condition"); } } } if (condType) { QVALUELIST_CLASSNAME<QDomNode> childNodes = elementsByTagName(node, "li"); for (int i = 0; i < childNodes.count(); i++) { QDomNode n = childNodes[i]; if (n.toElement().hasAttribute("value")) { if (condType == 2) name = n.toElement().attribute("name"); QString value = n.toElement().attribute("value").upper(); QStringList dummy; if (exactMatch(value, _parameterValue[name].upper(), dummy)) { result = resolveNode(&n, categoriesId, capturedTexts, capturedThatTexts, capturedTopicTexts); break; } } else { result = resolveNode(&n, categoriesId, capturedTexts, capturedThatTexts, capturedTopicTexts); break; } } } } else { QDomNode n = node->firstChild(); while (!n.isNull()) { result += resolveNode(&n, categoriesId, capturedTexts, capturedThatTexts, capturedTopicTexts); n = n.nextSibling(); } if (node->isText()) result = node->toText().nodeValue(); else if (nodeName == "set") _parameterValue[element.attribute("name")] = result.stripWhiteSpace(); else if (nodeName == "srai") result = getResponse(result, categoriesId, true); else if (nodeName == "think") result = ""; else if (nodeName == "system") //lvk: Remvoved for security reasons //result = executeCommand(result); ; else if (nodeName == "learn") { loadAiml(result); result = ""; } else if (nodeName == "uppercase") { result = result.upper(); } else if (nodeName == "lowercase") { result = result.lower(); } else if (!node->hasChildNodes()) { if (nodeName == "star") { int index = element.attribute("index", "1").toUInt() - 1; result = index < capturedTexts.count() ? capturedTexts[index] : QString(""); } else if (nodeName == "thatstar") { int index = element.attribute("index", "1").toUInt() - 1; result = index < capturedThatTexts.count() ? capturedThatTexts[index] : QString(""); } else if (nodeName == "topicstar") { int index = element.attribute("index", "1").toUInt() - 1; result = index < capturedTopicTexts.count() ? capturedTopicTexts[index] : QString(""); } else if (nodeName == "that") { QString indexStr = element.attribute("index", "1,1"); if (!indexStr.contains(",")) indexStr = "1," + indexStr; int index1 = indexStr.section(',', 0, 0).toInt()-1; int index2 = indexStr.section(',', 1, 1).toInt()-1; result = (index1 < _thatList.count()) && (index2 < _thatList[index1].count()) ? _thatList[index1][index2] : QString(""); } else if (nodeName == "sr") result = getResponse(capturedTexts.count() ? capturedTexts[0] : QString(""), categoriesId, true); else if ( (nodeName == "br") || (nodeName == "html:br") ) result = "\n"; else if ( nodeName == "get" ) result = _parameterValue[element.attribute("name")]; else if ( nodeName == "bot") result = _botVarValue[element.attribute("name")]; else if ( (nodeName == "person") || (nodeName == "person2") || (nodeName == "gender") ) result = capturedTexts.count() ? capturedTexts[0] : QString(""); else if (nodeName == "input") { int index = element.attribute("index", "1").toUInt() - 1; result = index < _inputList.count() ? _inputList[index] : QString(""); } //the following just to avoid warnings ! else if (nodeName == "li") ; else _logStream << "Warning: unknown tag \"" + nodeName + "\"\n"; } //the following just to avoid warnings ! else if ((nodeName == "template") || (nodeName == "pattern") || (nodeName == "li") || (nodeName == "person") || (nodeName == "person2") || (nodeName == "gender") || (nodeName == "parsedCondition")) ; else _logStream << "Warning: unknown tag \"" + nodeName + "\"\n"; } return result; }