开发者社区> 问答> 正文

包含多个词典的翻译库

我需要创建一个包含多个词典的翻译库。每个词典执行从一种语言到另一种语言的翻译。有多个可用的字典,但并非所有字典都包含所有需要的条目。因此,例如,要查找从英语到德语的作品,我可能需要先查询英语-法语,然后查询法语-德语。我需要使用预定义数量的词典来设计翻译库,并创建一个API以使用所有可用的词典执行从一种语言到另一种语言的翻译。我创建了两个字典,法语-英语和英语-德语,并且我想将法语单词翻译成德语(通过我的两个字典),但是我不知道如何在两个字典之间建立一个“桥梁”。

#include <iostream>
#include <string.h>

#include <map>
using namespace std;

class Dictionary {
    // Structure for Node
public:

    //bool isWordFounded;

    struct Node {
        bool isEndOfWord;
        map <char, Node*> map;
        string meaning;
    };

    Node* node = nullptr;

    // Function to create a new Node node
    Node* getNewNodeNode()
    {
        Node* node = new Node;
        node->isEndOfWord = false;
        return node;
    }

    // Function to insert a word with its meaning
    // in the dictionary built using a Node
    void insert(Node*& root, const string& str, const string& meaning)
    {

        // If root is null
        if (root == nullptr){
            root = getNewNodeNode();
            cout << "Am create un nou NODE"<< endl;
        }

        Node* temp = root;

        for (int i = 0; i < str.length(); i++) {
            char x = str[i];
            //cout << x << endl;
            // Make a new node if there is no path

            if (temp->map.find(x) == temp->map.end()){

                temp->map[x] = getNewNodeNode();

                temp = temp->map[x];
            }

            // Mark end of word and store the meaning
            temp->isEndOfWord = true;
            temp->meaning = meaning;
        }
    }

    // Function to search a word in the Node
    // and return its meaning if the word exists
    string getMeaning(Node* root, const string& word)
    {

        // If root is null i.e. the dictionary is empty
        if (root == nullptr)
            return "";

        Node* temp = root;

        // Search a word in the Node
        for (int i = 0; i < word.length(); i++) {
            temp = temp->map[word[i]];
            if (temp == nullptr)
            {
                //isWordFounded = false;
                return "Nu am gasit cuvantul introdus";
            }
        }

        // If it is the end of a valid word stored
        // before then return its meaning
        if (temp->isEndOfWord)
            return temp->meaning;
        return "";
    }
};

int main()
{
    // Build the FRENCH-ENGLISH dictionary
    Dictionary fr_eng;

    fr_eng.insert(fr_eng.node, "maison", "house");
    fr_eng.insert(fr_eng.node, "masina", "voiture");

    // Build the ENGLISH-GERMANY dictionary
    Dictionary eng_ger;

    eng_ger.insert(eng_ger.node, "house", "hause");
    eng_ger.insert(eng_ger.node, "car", "auto");

    string str;
    cin >> str;
    cout << fr_eng.getMeaning(fr_eng.node, str) << endl;


    return 0;
}

展开
收起
几许相思几点泪 2019-12-22 18:17:52 1117 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
JCLI使用说明文档 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载