Beispiel #1
0
 /// --------------------------------------------------------------------------------
 /// <summary>
 /// Initializes a new instance of the <see cref="XmlTranslation"/> class.
 /// </summary>
 /// --------------------------------------------------------------------------------
 public PhraseCustomization(TranslatablePhrase tp)
 {
     Reference      = tp.Reference;
     OriginalPhrase = tp.OriginalPhrase;
     ModifiedPhrase = tp.ModifiedPhrase;
     Type           = tp.IsExcluded ? CustomizationType.Deletion : CustomizationType.Modification;
 }
Beispiel #2
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EditQuestion"/> class.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public EditQuestionDlg(TranslatablePhrase question)
        {
            m_question = question;
            InitializeComponent();
            m_txtOriginal.Text = question.OriginalPhrase;
            m_txtModified.Text = question.PhraseInUse;
            Question q = question.QuestionInfo;

            if (q != null && q.AlternateForms != null)
            {
                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(EditQuestionDlg));

                foreach (string alternateForm in q.AlternateForms.Skip(1))
                {
                    RadioButton newBtn = new RadioButton();
                    m_pnlAlternatives.Controls.Add(newBtn);
                    resources.ApplyResources(newBtn, "m_rdoAlternative");
                    m_pnlAlternatives.SetFlowBreak(newBtn, true);
                    newBtn.Text            = alternateForm;
                    newBtn.CheckedChanged += m_rdoAlternative_CheckedChanged;
                }
                m_rdoAlternative.Text = q.AlternateForms.First();
                return;
            }
            m_pnlAlternatives.Hide();
        }
 /// ------------------------------------------------------------------------------------
 /// <summary>
 /// Gets the index of the (first) phrase in the (filtered) collection that matches the
 /// given key.
 /// </summary>
 /// ------------------------------------------------------------------------------------
 public int FindPhrase(QuestionKey phraseKey)
 {
     for (int i = 0; i < FilteredSortedPhrases.Count; i++)
     {
         TranslatablePhrase phrase = FilteredSortedPhrases[i];
         if (phrase.PhraseKey.Matches(phraseKey))
         {
             return(i);
         }
     }
     return(-1);
 }
Beispiel #4
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Initializes a new instance of the <see cref="PhraseParser"/> class.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        internal PhraseParser(Dictionary <Word, List <KeyTermMatch> > keyTermsTable,
                              Dictionary <Regex, string> substituteStrings, TranslatablePhrase phrase,
                              Func <IEnumerable <Word>, TranslatablePhrase, int, Part> yieldPart)
        {
            m_keyTermsTable = keyTermsTable;
            YieldPart       = yieldPart;
            m_phrase        = phrase;

            string phraseToParse = m_phrase.PhraseInUse;

            foreach (KeyValuePair <Regex, string> substituteString in substituteStrings)
            {
                phraseToParse = substituteString.Key.Replace(phraseToParse, substituteString.Value);
            }

            m_words = GetWordsInString(phraseToParse.Trim().ToLowerInvariant(), true);
        }
Beispiel #5
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Gets the best rendering for this term in when used in the context of the given
        /// phrase.
        /// </summary>
        /// <remarks>If this term occurs more than once in the phrase, it is not possible to
        /// know which occurrence is which.</remarks>
        /// ------------------------------------------------------------------------------------
        public string GetBestRenderingInContext(TranslatablePhrase phrase)
        {
            IEnumerable <string> renderings = Renderings;

            if (!renderings.Any())
            {
                return(string.Empty);
            }
            if (renderings.Count() == 1 || TranslatablePhrase.s_helper.TermRenderingSelectionRules == null)
            {
                return(Translation);
            }

            List <string> renderingsList = null;

            foreach (RenderingSelectionRule rule in TranslatablePhrase.s_helper.TermRenderingSelectionRules.Where(r => !r.Disabled))
            {
                if (renderingsList == null)
                {
                    renderingsList = new List <string>();
                    foreach (string rendering in renderings)
                    {
                        if (rendering == Translation)
                        {
                            renderingsList.Insert(0, rendering);
                        }
                        else
                        {
                            renderingsList.Add(rendering);
                        }
                    }
                }
                string s = rule.ChooseRendering(phrase.PhraseInUse, Words, renderingsList);
                if (!string.IsNullOrEmpty(s))
                {
                    return(s);
                }
            }
            return(Translation);
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Gets or creates a part matching the given sub-phrase.
        /// </summary>
        /// <param name="words">The words of the sub-phrase.</param>
        /// <param name="owningPhraseOfPart">The owning phrase of the part to find or create.</param>
        /// <param name="tempWordCountOfPhraseBeingBroken">The temp word count of phrase being broken.</param>
        /// <returns>the newly created or found part</returns>
        /// ------------------------------------------------------------------------------------
        private Part GetOrCreatePart(IEnumerable <Word> words, TranslatablePhrase owningPhraseOfPart,
                                     int tempWordCountOfPhraseBeingBroken)
        {
            Debug.Assert(words.Any());
            Part part = null;

            Dictionary <Word, List <Part> > partsTable;
            List <Part> parts = null;

            if (m_partsTable.TryGetValue(words.Count(), out partsTable))
            {
                if (partsTable.TryGetValue(words.First(), out parts))
                {
                    part = parts.FirstOrDefault(x => x.Words.SequenceEqual(words));
                }
            }
            else
            {
                m_partsTable[words.Count()] = partsTable = new Dictionary <Word, List <Part> >();
            }

            if (parts == null)
            {
                partsTable[words.First()] = parts = new List <Part>();
            }

            if (part == null)
            {
                Debug.Assert(tempWordCountOfPhraseBeingBroken != words.Count());
                part = new Part(words);
                parts.Add(part);
            }

            part.AddOwningPhrase(owningPhraseOfPart);

            return(part);
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Initializes a new instance of the <see cref="PhraseTranslationHelper"/> class.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        public PhraseTranslationHelper(IEnumerable <TranslatablePhrase> phrases,
                                       IEnumerable <IKeyTerm> keyTerms, KeyTermRules keyTermRules,
                                       IEnumerable <Substitution> phrasesToIgnore)
        {
            TranslatablePhrase.s_helper = this;

            m_keyTermsTable = new Dictionary <Word, List <KeyTermMatch> >(keyTerms.Count());
            PopulateKeyTermsTable(keyTerms, keyTermRules);

            m_phraseSubstitutions = new Dictionary <Regex, string>(phrasesToIgnore.Count());
            foreach (Substitution substitutePhrase in phrasesToIgnore)
            {
                m_phraseSubstitutions[substitutePhrase.RegEx] = substitutePhrase.RegExReplacementString;
            }

            m_partsTable = new SortedDictionary <int, Dictionary <Word, List <Part> > >();
            foreach (TranslatablePhrase phrase in phrases.Where(p => !string.IsNullOrEmpty(p.PhraseToDisplayInUI)))
            {
                if (!phrase.IsExcluded)
                {
                    PhraseParser parser = new PhraseParser(m_keyTermsTable, m_phraseSubstitutions, phrase, GetOrCreatePart);
                    foreach (IPhrasePart part in parser.Parse())
                    {
                        phrase.m_parts.Add(part);
                    }
                }
                m_phrases.Add(phrase);
                if (phrase.Category == -1)
                {
                    m_categories[phrase.SequenceNumber] = phrase;
                }
            }

            for (int wordCount = m_partsTable.Keys.Max(); wordCount > 1; wordCount--)
            {
                Dictionary <Word, List <Part> > partsTable;
                if (!m_partsTable.TryGetValue(wordCount, out partsTable))
                {
                    continue;
                }

                List <Part> partsToDelete = new List <Part>();

                foreach (KeyValuePair <Word, List <Part> > phrasePartPair in partsTable)              // REVIEW: problem: won't be able to add a new part that starts with this word
                {
                    foreach (Part part in phrasePartPair.Value)
                    {
                        if (part.OwningPhrases.Count() != 1)
                        {
                            continue;
                        }

                        // Look to see if some other part is a sub-phrase of this part.
                        SubPhraseMatch match = FindSubPhraseMatch(part);
                        if (match != null)
                        {
                            TranslatablePhrase owningPhraseOfPart = part.OwningPhrases.First();
                            int iPart = owningPhraseOfPart.m_parts.IndexOf(part);
                            // Deal with any preceding remainder
                            if (match.StartIndex > 0)
                            {
                                Part preceedingPart = GetOrCreatePart(part.GetSubWords(0, match.StartIndex), owningPhraseOfPart, wordCount);
                                owningPhraseOfPart.m_parts.Insert(iPart++, preceedingPart);
                            }
                            match.Part.AddOwningPhrase(owningPhraseOfPart);
                            owningPhraseOfPart.m_parts[iPart++] = match.Part;
                            // Deal with any following remainder
                            // Breaks this part at the given position because an existing part was found to be a
                            // substring of this part. Any text before the part being excluded will be broken off
                            // as a new part and returned. Any text following the part being excluded will be kept
                            // as this part's contents.
                            if (match.StartIndex + match.Part.m_words.Count < part.m_words.Count)
                            {
                                Part followingPart = GetOrCreatePart(part.GetSubWords(match.StartIndex + match.Part.m_words.Count), owningPhraseOfPart, wordCount);
                                owningPhraseOfPart.m_parts.Insert(iPart, followingPart);
                            }
                            partsToDelete.Add(part);
                        }
                    }
                }
                foreach (Part partToDelete in partsToDelete)
                {
                    partsTable[partToDelete.m_words[0]].Remove(partToDelete);
                }
            }
            m_filteredPhrases = m_phrases;
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Processes a new translation on a phrase.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        internal void ProcessTranslation(TranslatablePhrase tp)
        {
            string initialPunct, finalPunct;

            StringBuilder bldr = new StringBuilder();

            if (tp.Translation.StartsWith("\u00BF"))
            {
                bldr.Append(tp.Translation[0]);
            }
            if (bldr.Length > 0 && bldr.Length < tp.Translation.Length)
            {
                m_initialPunct[tp.TypeOfPhrase] = initialPunct = bldr.ToString();
            }
            else
            {
                initialPunct = InitialPunctuationForType(tp.TypeOfPhrase);
            }

            bldr.Length = 0;
            foreach (char t in tp.Translation.Reverse().TakeWhile(Char.IsPunctuation))
            {
                bldr.Insert(0, t);
            }
            if (bldr.Length > 0 && bldr.Length < tp.Translation.Length)
            {
                m_finalPunct[tp.TypeOfPhrase] = finalPunct = bldr.ToString();
            }
            else
            {
                finalPunct = InitialPunctuationForType(tp.TypeOfPhrase);
            }

            List <Part> tpParts = tp.TranslatableParts.ToList();

            if (tpParts.Count == 0)
            {
                return;
            }

            string translation = tp.GetTranslationTemplate();

            foreach (TranslatablePhrase similarPhrase in tpParts[0].OwningPhrases.Where(phrase => !phrase.HasUserTranslation && phrase.PartPatternMatches(tp)))
            {
                if (similarPhrase.PhraseInUse == tp.PhraseInUse)
                {
                    similarPhrase.Translation = tp.Translation;
                }
                else if (tp.AllTermsMatch)
                {
                    similarPhrase.SetProvisionalTranslation(translation);
                }
            }

            if (tp.AllTermsMatch)
            {
                if (tpParts.Count == 1)
                {
                    if (translation.StartsWith(initialPunct))
                    {
                        translation = translation.Remove(0, initialPunct.Length);
                    }
                    if (translation.EndsWith(finalPunct))
                    {
                        translation = translation.Substring(0, translation.Length - finalPunct.Length);
                    }

                    tpParts[0].Translation = Regex.Replace(translation, @"\{.+\}", string.Empty).Trim();
                    if (TranslationsChanged != null)
                    {
                        TranslationsChanged();
                    }
                    return;
                }
            }

            if (m_justGettingStarted)
            {
                return;
            }

            if (translation.StartsWith(initialPunct))
            {
                translation = translation.Remove(0, initialPunct.Length);
            }
            if (translation.EndsWith(finalPunct))
            {
                translation = translation.Substring(0, translation.Length - finalPunct.Length);
            }

            List <Part>    unTranslatedParts    = new List <Part>(tpParts);
            HashSet <Part> partsNeedingUpdating = new HashSet <Part>();

            foreach (Part part in tpParts)
            {
                partsNeedingUpdating.UnionWith(RecalculatePartTranslation(part).Where(p => !tpParts.Contains(p)));
                if (part.Translation.Length > 0)
                {
                    int ichMatch = translation.IndexOf(part.Translation, StringComparison.Ordinal);
                    if (ichMatch >= 0)
                    {
                        translation = translation.Remove(ichMatch, part.Translation.Length);
                    }
                    unTranslatedParts.Remove(part);
                }
            }
            if (unTranslatedParts.Count == 1)
            {
                unTranslatedParts[0].Translation = Regex.Replace(translation, @"\{.+\}", string.Empty).Trim();
            }

            foreach (Part partNeedingUpdating in partsNeedingUpdating)
            {
                RecalculatePartTranslation(partNeedingUpdating);
            }

            if (TranslationsChanged != null)
            {
                TranslationsChanged();
            }
        }
Beispiel #9
0
 /// ------------------------------------------------------------------------------------
 /// <summary>
 /// Gets the best rendering for this part in when used in the context of the given
 /// phrase.
 /// </summary>
 /// <remarks>If this part occurs more than once in the phrase, it is not possible to
 /// know which occurrence is which.</remarks>
 /// ------------------------------------------------------------------------------------
 public string GetBestRenderingInContext(TranslatablePhrase phrase)
 {
     return(Translation);
 }
Beispiel #10
0
 /// ------------------------------------------------------------------------------------
 /// <summary>
 /// Adds the spcified phrase as an owner of this Part.
 /// </summary>
 /// ------------------------------------------------------------------------------------
 internal void AddOwningPhrase(TranslatablePhrase phrase)
 {
     m_owningPhrases.Add(phrase);
 }
 /// --------------------------------------------------------------------------------
 /// <summary>
 /// Initializes a new instance of the <see cref="XmlTranslation"/> class.
 /// </summary>
 /// --------------------------------------------------------------------------------
 public XmlTranslation(TranslatablePhrase tp)
 {
     Reference   = tp.PhraseKey.ScriptureReference;
     PhraseKey   = tp.PhraseKey.Text;
     Translation = tp.Translation;
 }