Beispiel #1
0
int main( int argc, const char * * argv )
{
    TinyDictIndexFile index;
    TinyDictDataFile data;
    TinyDictDataFile zdata;
    if ( !index.open("mueller7.index") ) {
        printf("cannot open index file mueller7.index\n");
        return -1;
    }
    if ( !data.open("mueller7.dict") ) {
        printf("cannot open data file mueller7.dict\n");
        return -1;
    }
    if ( !zdata.open("mueller7.dict.dz") ) {
        printf("cannot open data file mueller7.dict.dz\n");
        return -1;
    }
    TinyDictWordList words;
    const char * pattern = "full";
    index.find( pattern, true, words );
    printf( "%d words matched pattern %s\n", words.length(), pattern );
    for ( int i=0; i<words.length(); i++ ) {
        TinyDictWord * p = words.get(i);
        printf("%s %d %d\n", p->getWord(), p->getStart(), p->getSize() );
        const char * text = zdata.read( p );
        if ( text )
            printf( "article:\n%s\n", text );
        else
            printf( "cannot read article\n" );
    }

	{
		// create TinyDictionaryList object
		TinyDictionaryList dicts;
		// register dictionaries using 
		dicts.add( "mueller7.index", "mueller7.dict.dz" );

		// container for results
		TinyDictResultList results;
	    dicts.find(results, "empty", 0 ); // find exact match

		// for each source dictionary that matches pattern
		for ( int d = 0; d<results.length(); d++ ) {
			TinyDictWordList * words = results.get(d);
			printf("dict: %s\n", words->getDictionaryName() );
			// for each found word
			for ( int i=0; i<words->length(); i++ ) {
				TinyDictWord * word = words->get(i);
				printf("word: %s\n", word->getWord() );
				printf("article: %s\n", words->getArticle( i ) );
			}
		}
	}
#ifdef _WIN32
	printf("Press any key...");
	getchar();
#endif
    return 0;
}
Beispiel #2
0
/// search all dictionaries in list for specified pattern
bool TinyDictionaryList::find( TinyDictResultList & result, const char * prefix, int options )
{
	result.clear();
	for ( int i=0; i<count; i++ ) {
		TinyDictWordList * p = list[i]->find( prefix, options );
		if ( p )
			result.add( p );
	}
	return result.length() > 0;
}
lString8 CRTinyDict::translate(const lString8 & w)
{
    lString16 s16 = Utf8ToUnicode( w );
    s16.lowercase();
    lString8 word = UnicodeToUtf8( s16 );
    lString8 body;
    TinyDictResultList results;
    if ( dicts.length() == 0 ) {
        // should not happen
        body << "<title><p>No dictionaries found</p></title>";
    } else if ( dicts.find(results, word.c_str(), TINY_DICT_OPTION_STARTS_WITH ) ) {
        for ( int d = 0; d<results.length(); d++ ) {
            TinyDictWordList * words = results.get(d);
            if ( words->length()>0 )
                body << "<title><p>" << _("From dictionary ") << words->getDictionaryName() << ":</p></title>";
            // for each found word
            for ( int i=0; i<words->length(); i++ ) {
                //TinyDictWord * word = words->get(i);
                const char * article = words->getArticle( i );
                body << "<code style=\"text-align: left; text-indent: 0; font-size: 22\">";
                if ( article ) {
                    body << article;
                } else {
                    body << _("[cannot read article]");
                }
                body << "</code>";
                if ( i<words->length()-1 )
                    body << "<hr/>";
            }
        }
    } else {
        body << "<title><p>Article for word " << word << " not found</p></title>";
    }

    return body;
}