Exemplo n.º 1
0
bool CMultiWordCreator::FindKWWords(const SArtPointer& artP, EDicType dicType)
{
    if (!artP.IsValid() || m_FoundArticles.find(artP) != m_FoundArticles.end())
        return false;

    yvector<TGztArticle> gztArticles;
    yvector<SDictIndex> auxArticles;
    bool hasTextKeys = false;

    CollectArticleInfo(artP, dicType, gztArticles, auxArticles, hasTextKeys);

    if (hasTextKeys)
        FindKeyWords(dicType);

    for (size_t i = 0; i < gztArticles.size(); ++i) {
        SArtPointer sub_art(gztArticles[i].GetTitle());
        if (!(sub_art == artP) && m_FoundArticles.find(sub_art) != m_FoundArticles.end())
            continue;

        // the single article could contain keys of both 'tomita:' or 'alg:' types
        ProcessTomitaGztArticle(gztArticles[i]);
        ProcessAlgArticle(gztArticles[i]);
        ProcessTaggerGztArticle(gztArticles[i]);    // and 'tagger:' as well

        m_FoundArticles.insert(sub_art);
    }

    FindAuxKWWords(auxArticles, dicType);

    m_FoundArticles.insert(artP);

    return true;
}
Exemplo n.º 2
0
void CMultiWordCreator::FindAuxKWWords(yvector<SDictIndex>& indexes, EDicType dicType)
{
    if (dicType == DICTYPE_COUNT)
        return;

    bool bHasTextKW = false;
    yvector<SDictIndex> tomitaKWArticles;
    SDictIndex algArticleIndex;//считатеся, что у одного kw_type не больше одного alg
    for (size_t i = 0; i < indexes.size(); ++i) {
        SDictIndex index = indexes[i];
        YASSERT(dicType == index.m_DicType);    //словарь должен быть для всех одинаковый

        const article_t* pArt = GlobalDictsHolder->GetAuxArticle(index);
        if (m_FoundArticles.find(SArtPointer(pArt->get_title())) != m_FoundArticles.end())
            continue;

        if (pArt->has_text_content()) // значит поле СОСТАВ состоит из обычных слов
            bHasTextKW = true;
        else if (pArt->has_gram_file_name())
            tomitaKWArticles.push_back(index);
        else if (pArt->has_alg())
            algArticleIndex = index;
    }

    if (bHasTextKW || dicType == KW_DICT)
        FindKeyWords(dicType);

    for (size_t i = 0; i < tomitaKWArticles.size(); ++i)
        ProcessTomitaAuxArticle(tomitaKWArticles[i]);

    if (algArticleIndex.IsValid())
        ProcessAlgArticle(TArticleRef(algArticleIndex));
}
bool FvMassStringFilter::HasUnLawKeyWord(const FvString& kUTF8Str) const
{
	if(kUTF8Str.empty() || !m_uiUnLawKeyWordsPairArraySize)
		return false;

	size_t uiOffset = 0;
	size_t uiOffsetNext = 0;
	FvUInt64 kWord;
	while((uiOffsetNext = GetUTF8Word(kUTF8Str.c_str(), uiOffset, kWord)) != -1)
	{
		UnLawKeyWords* pkUnLawKeyWords = FindKeyWords(kWord);
		if(!pkUnLawKeyWords)
		{
			uiOffset = uiOffsetNext;
			continue;
		}

		const UnLawKeyWords& kUnLawKeyWords = *pkUnLawKeyWords;
		FV_ASSERT(kUnLawKeyWords.size());
		const FvString* pkIdx = &kUnLawKeyWords[0];
		const FvString* pkEnd = pkIdx + kUnLawKeyWords.size();
		for(; pkIdx!=pkEnd; ++pkIdx)
		{
			//! TODO: 换成KMP算法,会不会更快?
			if(kUTF8Str.find(*pkIdx, uiOffset) != FvString::npos)
				return true;
		}

		uiOffset = uiOffsetNext;
	}

	return false;
}
Exemplo n.º 4
0
//ищет статьи на определенном (wp) отрезке слов (используется, например, для поиска в ссылках)
//так как повторять всю бодягу с кэшами неохота и накладно, то ленивости здесь почти не будет
//при вызове ф-ции FindWordsInPeriod второй раз на одних и тех же данных, лишнего ничего найдено не будет,
//но и, если ничего не было найдено, это нигде не запоминается и второй раз будем пытаться искать
void CMultiWordCreator::FindWordsInPeriod(const SArtPointer& artP, const CWordsPair& wp)
{
    yvector<SWordHomonymNum> foundWords;
    GetFoundWordsInPeriod(artP, foundWords, wp);

    if (!foundWords.empty())
        return;

    yvector<TGztArticle> gztArticles;
    yvector<SDictIndex> auxArticles;
    bool hasTextKeys = false;
    CollectArticleInfo(artP, KW_DICT, gztArticles, auxArticles, hasTextKeys);

    if (hasTextKeys)
        FindKeyWords(KW_DICT);

    yvector<CWordSequence*> newWordSequences;

    // nasty copy-pasting...
    for (size_t i = 0; i < gztArticles.size(); ++i) {
        for (NGzt::TCustomKeyIterator it(gztArticles[i], ::GetTomitaPrefix()); it.Ok(); ++it) {
            CTomitaChainSearcher TomitaChainSearcher(m_Words, m_pReferenceSearcher, *this);
            newWordSequences.clear();
            TomitaChainSearcher.RunFactGrammar(*it, GlobalDictsHolder->GetGrammarOrRegister(*it), newWordSequences, wp);
            if (TomitaChainSearcher.m_bProcessedByGrammar)
                m_bProcessedByGrammar = true;

            for (size_t j = 0; j < newWordSequences.size(); ++j) {
                newWordSequences[j]->PutGztArticle(gztArticles[i]);
                AddMultiWordInt(newWordSequences[j], false, TGramBitSet(), wp);
            }
        }
    }

    for (size_t i = 0; i < auxArticles.size(); ++i) {
        const article_t* pArt = GlobalDictsHolder->GetAuxArticle(auxArticles[i]);
        //если в поле СОСТАВ просто перечислены нужные слова, то не будем выпендриваться и искать их только в данном отрезке
        //все равно ф-ция FindKeyWords рано или поздно вызовется на всем предложении
        if (pArt->has_text_content()) {
            if (!hasTextKeys)
                FindKWWords(pArt->get_title(), KW_DICT);
        } else if (pArt->has_gram_file_name()) {
             CTomitaChainSearcher TomitaChainSearcher(m_Words, m_pReferenceSearcher, *this);
            newWordSequences.clear();
            const Stroka& gramfile = pArt->get_gram_file_name();
            TomitaChainSearcher.RunFactGrammar(gramfile, GlobalDictsHolder->GetGrammarOrRegister(gramfile), newWordSequences, wp);
            if (TomitaChainSearcher.m_bProcessedByGrammar)
                m_bProcessedByGrammar = true;

            for (size_t j = 0; j < newWordSequences.size(); ++j) {
                newWordSequences[j]->PutAuxArticle(auxArticles[i]);
                AddMultiWordInt(newWordSequences[j], false, TGramBitSet(), wp);
            }
        }
    }
}
bool FvMassStringFilter::CheckAndReplaceUnLawKeyWords(const FvString& kUTF8Src, FvString& kUTF8Des) const
{
	if(kUTF8Src.empty() || !m_uiUnLawKeyWordsPairArraySize)
		return false;

	kUTF8Des = kUTF8Src;
	bool bFind = false;

	size_t uiOffset = 0;
	size_t uiOffsetNext = 0;
	FvUInt64 kWord;
	UnLawKeyWordMap::const_iterator itr;
	while((uiOffsetNext = GetUTF8Word(kUTF8Des.c_str(), uiOffset, kWord)) != -1)
	{
		if(kWord == m_cMark)
		{
			uiOffset = uiOffsetNext;
			continue;
		}

		UnLawKeyWords* pkUnLawKeyWords = FindKeyWords(kWord);
		if(!pkUnLawKeyWords)
		{
			uiOffset = uiOffsetNext;
			continue;
		}

		size_t uiKeyWordLen = 0;
		const UnLawKeyWords& kUnLawKeyWords = *pkUnLawKeyWords;
		FV_ASSERT(kUnLawKeyWords.size());
		const FvString* pkIdx = &kUnLawKeyWords[0];
		const FvString* pkEnd = pkIdx + kUnLawKeyWords.size();
		for(; pkIdx!=pkEnd; ++pkIdx)
		{
			//! TODO: 换成KMP算法,会不会更快?
			FvString::size_type uiPos = uiKeyWordLen ? uiOffset + uiKeyWordLen : uiOffset;
			FvString::size_type uiLen = pkIdx->length();
			while((uiPos = kUTF8Des.find(*pkIdx, uiPos)) != FvString::npos)
			{
				bFind = true;
				kUTF8Des.replace(uiPos, uiLen, uiLen, m_cMark);
				uiPos += uiLen;
				if(!uiKeyWordLen && uiPos == uiOffset + uiLen)
					uiKeyWordLen = uiLen;
			}
		}

		if(uiKeyWordLen)
			uiOffset += uiKeyWordLen;
		else
			uiOffset = uiOffsetNext;
	}

	return bFind;
}