【问题标题】:How to split sentences in an array如何在数组中拆分句子
【发布时间】:2014-03-04 10:19:52
【问题描述】:

我有一个字符串s 存储一个很长的句子,我想将s 的内容复制到一个数组C 中,每个单元格都存储一个句子。以下是我的代码,它没有给我任何输出,而是单元格的尺寸:

while(i<6)
  C(i)=s;
  end

这是我在打印C 时得到的输出:

C=
[1x76 char]

谁能帮帮我。

【问题讨论】:

  • 你能举个例子吗?
  • 我希望每个单元格存储一个句子。
  • 示例第一个单元存储语句:月球是地球唯一的天然卫星,也是太阳系中第五大卫星。第二个单元存储语句:在已知密度的卫星中,月球是密度第二高,仅次于木星的卫星木卫一。
  • 你的意思是很长的句子是句子的组合。例如“这是第一。这是第二。”你想要它作为数组中的两个单元格吗?
  • 是的,请帮我找到答案

标签: arrays string matlab text-processing text-parsing


【解决方案1】:

strsplit 的另一份工作:

>> sentences = 'This is the first one. Then here is a second. Yet another here.';
>> C = strsplit(sentences,'. ')
C = 
    'This is the first one'    'Then here is a second'    'Yet another here.'

我们指定一个句点后跟一个空格作为分隔符。根据需要进行更改。

【讨论】:

    【解决方案2】:

    假设长字符串为:

    longString = "This is first cell. This is second cell. this is third cell".
    

    现在由于. 在这里是分隔符,这意味着它充当句子的分隔符。所以你可以循环遍历longString 字符,每当遇到. 时,你只需增加数组索引计数并继续存储在这个数组索引中,直到找到另一个.

    这里是 sudo 代码:

    array[];
    index = 0;
    loop through(longString) character wise
    {
    if(currentChar equals  to '.')
    {
    index++;
    }
    else
    {
    array[index] = currentChanracter;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 2013-07-13
      • 2017-06-13
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多