Exemplo n.º 1
0
    private string parseBook(string bookFileName)
    {
        MarkovDict markovDict = new MarkovDict();

        markovDict.initialize();

        using (StreamReader sr = new StreamReader(Application.dataPath + "/Books/" + bookFileName)) {
            string line = null;
            while ((line = sr.ReadLine()) != null)
            {
                Markov markov = new Markov();
                markovDict.mergeWeightedDistribution(markov.getWeightedDistribution(line));
            }
        }

        BinaryFormatter bf = new BinaryFormatter();

        using (FileStream fs = new FileStream(Application.dataPath + "/dict.dat", FileMode.Create)) {
            bf.Serialize(fs, markovDict);
        }

        string result = "";

        foreach (KeyValuePair <string, Dictionary <string, int> > dict in markovDict.weightedDistribution)
        {
            result += "\"" + dict.Key + "\" \t ";
            foreach (KeyValuePair <string, int> pair in dict.Value)
            {
                result += "[\"" + pair.Key + "\" : " + pair.Value.ToString() + "] ";
            }
            result += "\n";
        }

        return(result);
    }
Exemplo n.º 2
0
    private string getWeightedDistribution(string sentence)
    {
        Markov markov = new Markov();
        Dictionary <string, Dictionary <string, int> > weightedDistribution = markov.getWeightedDistribution(sentence);

        BinaryFormatter bf = new BinaryFormatter();

        MarkovDict markovDict = null;

        if (File.Exists(Application.dataPath + MarkovDict.DataPath))
        {
            using (FileStream fs = new FileStream(Application.dataPath + "/dict.dat", FileMode.Open)) {
                markovDict = bf.Deserialize(fs) as MarkovDict;
            }
        }
        else
        {
            markovDict = new MarkovDict();
            markovDict.initialize();
        }

        markovDict.mergeWeightedDistribution(weightedDistribution);

        using (FileStream fs = new FileStream(Application.dataPath + "/dict.dat", FileMode.Create)) {
            bf.Serialize(fs, markovDict);
        }

        string result = "";

        foreach (KeyValuePair <string, Dictionary <string, int> > dict in markovDict.weightedDistribution)
        {
            result += "\"" + dict.Key + "\" \t ";
            foreach (KeyValuePair <string, int> pair in dict.Value)
            {
                result += "[\"" + pair.Key + "\" : " + pair.Value.ToString() + "] ";
            }
            result += "\n";
        }

        return(result);
    }