C#单词句子替换问题. [英] c# word sentences replacein problem.

查看:126
本文介绍了C#单词句子替换问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事单词申请.

我的任务是读取doc文件的内容并将其翻译为另一种语言,然后以准确的位置保存在doc文件中.
简而言之,翻译内容且不要更改doc文件的格式.

首先,我阅读doc文件,提取句子并将其保存到List对象.

I am working on word application.

My task is to read doc file''s content and translate it to another language then save at exact postion in doc file.
In short, translate content and don''t change the format of doc file.

Firstly, I read doc file, extracted sentences and saved it to List object.

public static void GetSentencesAtContent()
        {
            int count = WordDoc.Content.Sentences.Count;

            if (m_list != null)
                m_list.Clear();
            m_list = new List<SentencesInfo>();

            for (int i = 1; i <= count; i++)
            {
                SentencesInfo m_info;
                m_info.start = WordDoc.Content.Sentences[i].Start;
                m_info.end = WordDoc.Content.Sentences[i].End;
                string m_str = WordDoc.Content.Sentences[i].Text.Trim();

                m_info.text = m_str.Replace("\r\a", "");
                m_list.Add(m_info);

            }
        }



我尝试用随机句子替换doc中的所有句子以测试替换.



I tried to replace all sentences in doc with random sentences in order to test the replacing.

        public static void replace(int index, string text )
        {
            int p = WordDoc.Content.Sentences.Count;
            object start, end;
            start = WordDoc.Content.Sentences[index].Start;
            end = WordDoc.Content.Sentences[index].End;
            Range rng;
            rng = WordDoc.Range(ref start, ref end);
            rng.Text = text;
            rng.Select();
            WordDoc.Save();
        
        }
             
        public static void replaceAll()
        {
            m_testlist = new List<string>();
            m_testlist.Add("hi!");
            m_testlist.Add("how r u.");
            m_testlist.Add("fine day.");
            m_testlist.Add("speak.");
            m_testlist.Add("8 million.");
            m_testlist.Add("stand up.");
            m_testlist.Add("victory.");
            System.Random a = new Random(System.DateTime.Now.Millisecond);
            int count = m_list.Count;
            for (int i = 0 ; i < count; i++)
            {
              
                string m_str = m_list[i].text;
                int RandKey = a.Next(count - 1);
                replace(i + 1, m_testlist[RandKey] + "\r\n");    
       
            }
        }
</string>



我编写了一个用新值替换一个句子的函数. (我没有使用句子的开头和结尾信息,因为替换时可以通过句子的索引来获取它.)

我将该函数放在一个循环中,在其中尝试用随机值替换doc文件中的I语句.


但是问题是:当一行中有很多句子时,我的代码将替换后的一行(一行中有多个句子)分成几行,每行有一个句子.

为了避免这种情况,我从代码中删除了"\ r \ n":



I write a function which replaces one sentence by new value. (I did not use the start and end inofrmation of sentences, since I can get it by sentence''s index when replacing).

I placed the function in a loop, where I tried to replace I sentences in doc file by random value.


But the problem is : when there are many sentences in one line, my code break one line (there are more than one sentences in one line) into several lines after replacement, each line has one sentence.

To avoid this I removed the "\r\n" from the code :

replace(i + 1, m_testlist[RandKey]);



但是这时又出现了另一个问题,就是句子的数量越来越少,因此当替换最后几个句子时循环中会出现错误.

当我第一次阅读句子时,我从句子中删除了"\ r \ a",如果我没有这样做,那么当我在XAML中将句子保存在XElement中时,它会显示错误,提示无法识别simbol 0x07之类的东西(尤其是当句子来自表格,每个单元格的文本是一个句子,每个单元格的内容后总是有"\ r \ n").

查看GetSentencesAtContent函数.

我应该如何处理该任务?
读取doc文件,将其翻译然后保存到同一文件中?
在翻译期间,我应该将文档文件内容发送到XAML.

正如我所说:
XAML显示错误,无法识别0x07.
在循环替换句子期间,句子编号s消失,这会导致运行时错误.
替换句子导致更改doc文件中的行.

如何解决这些问题并完成任务?



but at this time another problem occured, which is that the sentences number is deacresing, so there is error in the loops when the last few sentences is replacing.

When I read sentences at first time I removed "\r\a" from the sentences, if I did not do that when I save sentences in XElement in XAML it shows error which says unable to recognize simbol 0x07 something like that (expecially when sentences came from table, each cell''s text is one sentences , and there are always "\r\n" after each cell''s content).

Look at GetSentencesAtContent function.

How should I deal with that task?
Read doc file , translate it then save it to same file?
During translation I should send doc file content to XAML .

As I said:
XAML shows error don''t recognizing 0x07.
during replacing in loops sentences the sentences number s deacresing and which cause runtime error.
Replacing sentences caused changing lines in doc file.

How can I fix those problem and finish the task?

is there anyone who give advice ?

推荐答案

在替换句子时,我们需要格外小心.

when replace the sentences ,we need to be carefull with ending position.

start = WordDoc.Content.Sentences[index].Start;
end = WordDoc.Content.Sentences[index].End;
Range rng;
rng = WordDoc.Range(ref start, ref end);
rng.Text = text;
rng.Select();



如果我们像上面那样使用,它将替换句子结尾标记.
它必须像下面一样使用.



it will replace sentences ending mark if we use like above.
it have to be used like below.

start = WordDoc.Content.Sentences[index].Start;
end = WordDoc.Content.Sentences[index].End - 1;
Range rng;
rng = WordDoc.Range(ref start, ref end);
rng.Text = text;
rng.Select();


doc文件中的每个句子看起来都有其结束标记,我们必须保留该标记,仅替换其内容.
所以效果很好.
我的解决方法是:
在文档级别,首先获取doc文件中的所有句子.
替换所有.
通过这样做,您可以获得纯文本,表格的内容.因此您不必担心矛状处理纯文本和表格.


each sentences in doc file looks like has its ending mark, we have to remain the mark , only replace its content.
so it worked well.
my solution is :
at document level get all sentences in doc file first.
the replace all of them.
by doing this you can get content fo pure text, tables. so you need not to warry about spearate handling pure text and tables.


这篇关于C#单词句子替换问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆