使用C#数组的语言翻译器 [英] Language Translator using C# Arrays

查看:113
本文介绍了使用C#数组的语言翻译器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数组中实现数组并识别模式

例如:-输入->阿皮·格达拉·吉耶姆.妈妈gedara giyemi.

首先,我们假设使用句点作为分隔符将这两个句子存储在一个数组中.

Implementing array within array and identifying pattern

ex: - Input --> api gedara giyemu. mama gedara giyemi.

first we are suppose to store these two sentences inside one array using full stop as the separator.

array[0] =  api gedara giyemu
array[1]= mama gedara giyemi



那么我们假设使用空格作为分隔符将单词存储在数组中的一个句子中.例如, array[0] = api gedara giyemu must be存储为



then we are suppose to store words within one sentence inside the array using space as the separator. As an example array[0] = api gedara giyemu must be stored as

arr[0]=api
arr[1]=gedara
arr[2]= giyemu



那么我们必须将字母存储在数组中的单词中.例如,如果使用arr[2]= giyemi,则必须将其存储为



then we have to store letters within a word inside an array.As an example if we take arr[2]= giyemi, this must be stored as

ltt[0]=g
ltt[1]=i
ltt[2]=y
ltt[3]=e
ltt[4]=m
ltt[5]=i



然后如何从数组ltt []中识别出模式yy(这是数组ltt []的最后一个索引),又从数组ltt []中识别出模式gi呢?

谢谢



Then how can I identify the pattern ''yemi'' (which are the last indexes of the array ltt[]) from the array ltt[] and the pattern ''gi'' from the array ltt[]


Thank you

推荐答案

您可以声明句子数组,每个句子都表示为单词数组.字母数组毫无意义:类型字符串已经足以作为不可变字母数组.

您的声明必须为string[][],而不是string[,].在第一种情况下,您有一个锯齿状的数组:这是一个数组的数组,而内部"数组是一个字符串的数组(在这种情况下,每个单独的单词都表示为字符串,而不是句子,这是一个单词数组).您应该分别初始化每个具有不同大小的内部"数组.这就是数组呈锯齿状的原因:它是具有不同长度的数组的数组.

相比之下,string[,]不灵活,无法适应您的情况:您需要同时在垂直"和水平"方向上初始化大小.

在这两种情况下,长度的改变都是非常无效的.我认为使用数组根本不是个好主意(即使在初始化之前,您也需要预先计算单词和/或句子的数量,这通常是不可能的或无效的).如果要从数组中添加或删除元素,请使用System.Generic.List<System.Generic.List<string>> —字符串列表的列表.

整个结构中单词的标识"简化为双循环搜索(无论是数组数组还是列表列表,代码看起来都一样).您也可以将LINQ用于此目的.我认为您无法获得出色的表现.

如果我知道您想对数据执行全套操作,也许我可以提供更有效的数据结构.抱歉,您没有分享.

—SA
You can declare array of sentences, each sentence represented as array of words. The array of letters is pointless: the type string already is good enough as an immutable array of letters.

Your declaration must be string[][], and not string[,]. In first case, you have a jagged array: this is an array of arrays, and the "inner" array is an array of strings (in this case, with each separate word represented as a string, but not a sentence, which is an array of words). You should initialize each "inner" array separately one by one with different size. That''s why the array is jagged: it''s an array of arrays of different length.

In contrast, string[,] is inflexible and cannot suite your case: you need to initialize size in both "vertical" and "horizontal" direction at once.

In both case, modification of the length is very ineffective. I don''t think using arrays is a good idea at all (you will need to pre-calculate the number of words and/or sentences even before initializing, which is often impossible or ineffective). If you want to add or remove elements from arrays, use System.Generic.List<System.Generic.List<string>> — list of list of string.

The "identification" of a word in a whole structure is reduced to a search in a double loop (no matter, array of arrays or a list of lists, the code looks the same). You can also use LINQ for that purpose. I don''t think you can get good performance.

If I know full set of operations you want to perform on your data, perhaps I could offer more effective data structure. Sorry, you did not share it.

—SA


这篇关于使用C#数组的语言翻译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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