示例#1
0
    private string GenerateSequence()
    {
        DataString seedContent = string.IsNullOrEmpty(start) ? new DataString(string.Empty) : new DataString(start);
        DataPhrase seed        = new DataPhrase(new[] { seedContent });
        //IEnumerable<string> result = model.Walk ( sequenceLength, seed );
        IEnumerable <DataPhrase> result = model.Walk(sequenceLength, seed, true);

        //IEnumerable<string> result = model.WalkRepeat ( sequenceLength, seed );
        sequence = "";
        string previousLyricIndex = string.Empty;

        foreach (DataPhrase phrase in result)
        {
            sequence += phrase.ToString((token) => {
                string index;
                if (token.data.TryGetValue("index", out index))
                {
                    bool sameSource    = previousLyricIndex == index;
                    string word        = string.Format("{0}{1} ", sameSource ? "" : "|", token.ToString());
                    previousLyricIndex = index;

                    return(word);
                }

                return(string.Empty);
            }) + "\n";
        }

        return(sequence);
    }
示例#2
0
    protected void Start()
    {
        //csvInput.Test(2, new[] { "songs", "lyrics" });
        csvInput.Test();

        model = new DataStringMarkov(n);
        model.EnsureUniqueWalk = true;

        //string[] lineDelim = new[] { "\r\n", "\r", "\n" };
        string[] wordDelim = new[] { " " };
        char[]   trimChars = new[] { '\r', '\n', ' ', '"', '\'' };

        if (csvInput != null)
        {
            List <string> lyrics    = csvInput.Data["lyrics"];
            List <string> songsInfo = csvInput.Data["songs"];

            textDisplay.text = "";

            int numLyrics = lyrics.Count;
            Debug.LogFormat("Lyrics #: {0}", numLyrics);
            for (int iLyric = 0; iLyric < numLyrics; iLyric++)
            {
                string   info  = songsInfo[iLyric];
                string[] words = lyrics[iLyric].Trim().Split(wordDelim, System.StringSplitOptions.RemoveEmptyEntries);

                int          numWords  = words.Length;
                DataString[] wordsData = new DataString[numWords];

                for (int iWord = 0; iWord < numWords; iWord++)
                {
                    DataString word = new DataString(words[iWord].Trim(trimChars));
                    word.data.Add("info", info);
                    word.data.Add("index", iLyric.ToString());

                    wordsData[iWord] = word;

                    if (iLyric < 10)
                    {
                        textDisplay.text += word.value;
                    }
                }

                DataPhrase phrase = new DataPhrase(wordsData);
                input.Add(phrase);

                // Train the model
                model.Learn(phrase);
            }
        }

        if (rand == null)
        {
            rand = gameObject.AddComponent <Rand> ();
        }
    }
        public IFacadeUpdateResult <DataPhraseData> SaveDataPhrases(object languageId, List <DataPhraseDto> dtoList)
        {
            FacadeUpdateResult <DataPhraseData> result = new FacadeUpdateResult <DataPhraseData>();
            // Retrieve phrases by languageId
            Dictionary <string, DataPhrase> phrases = new Dictionary <string, DataPhrase>();
            IDataPhraseService service = UnitOfWork.GetService <IDataPhraseService>();
            var query = service.SearchByLanguage(languageId);

            if (query.HasResult)
            {
                foreach (DataPhrase phrase in query.ToBoList <DataPhrase>())
                {
                    phrases.Add(phrase.PhraseKey, phrase);
                }
            }
            // Retrieve or Create instance
            List <DataPhrase> instances = new List <DataPhrase>();

            foreach (DataPhraseDto dto in dtoList)
            {
                DataPhrase instance = null;
                if (phrases.ContainsKey(dto.PhraseKey))
                {
                    instance             = phrases[dto.PhraseKey];
                    instance.PhraseValue = dto.PhraseValue;
                }
                else
                {
                    instance             = service.CreateNew <DataPhrase>();
                    instance.LanguageId  = languageId;
                    instance.PhraseKey   = dto.PhraseKey;
                    instance.PhraseValue = dto.PhraseValue;
                }
                instances.Add(instance);
            }
            // Save batch
            UnitOfWork.BeginTransaction();
            var saveQuery = service.SaveAll(instances);

            result.Merge(saveQuery);

            if (result.IsSuccessful)
            {
                UnitOfWork.CommitTransaction();
            }
            else
            {
                UnitOfWork.RollbackTransaction();
            }

            return(result);
        }
示例#4
0
        internal TDto RetrieveOrNewDataPhrase <TDto>(object instanceId, IDataConverter <DataPhraseData, TDto> converter)
            where TDto : class
        {
            ArgumentValidator.IsNotNull("converter", converter);
            IDataPhraseService service = UnitOfWork.GetService <IDataPhraseService>();
            FacadeUpdateResult <DataPhraseData> result = new FacadeUpdateResult <DataPhraseData>();
            DataPhrase instance = RetrieveOrNew <DataPhraseData, DataPhrase, IDataPhraseService>(result.ValidationResult, instanceId);

            if (result.IsSuccessful)
            {
                return(converter.Convert(instance.RetrieveData <DataPhraseData>()));
            }
            else
            {
                return(null);
            }
        }
示例#5
0
        internal IFacadeUpdateResult <DataPhraseData> DeleteDataPhrase(object instanceId)
        {
            ArgumentValidator.IsNotNull("instanceId", instanceId);

            FacadeUpdateResult <DataPhraseData> result = new FacadeUpdateResult <DataPhraseData>();
            IDataPhraseService service = UnitOfWork.GetService <IDataPhraseService>();
            var query = service.Retrieve(instanceId);

            if (query.HasResult)
            {
                DataPhrase instance  = query.ToBo <DataPhrase>();
                var        saveQuery = instance.Delete();
                result.Merge(saveQuery);
            }
            else
            {
                AddError(result.ValidationResult, "DataPhraseCannotBeFound");
            }

            return(result);
        }
示例#6
0
        internal IFacadeUpdateResult <DataPhraseData> SaveDataPhrase(DataPhraseDto dto)
        {
            ArgumentValidator.IsNotNull("dto", dto);

            FacadeUpdateResult <DataPhraseData> result = new FacadeUpdateResult <DataPhraseData>();
            IDataPhraseService service  = UnitOfWork.GetService <IDataPhraseService>();
            DataPhrase         instance = RetrieveOrNew <DataPhraseData, DataPhrase, IDataPhraseService>(result.ValidationResult, dto.Id);

            if (result.IsSuccessful)
            {
                instance.LanguageId  = dto.LanguageId;
                instance.PhraseKey   = dto.PhraseKey;
                instance.PhraseValue = dto.PhraseValue;

                var saveQuery = service.Save(instance);

                result.AttachResult(instance.RetrieveData <DataPhraseData>());
                result.Merge(saveQuery);
            }

            return(result);
        }