-1

我有一个我无法解决的问题。我有一组以行显示的数据(通常是按句子数量组织的文本)

我的句子中的文本示例:

1. Hello, world, It, is, beautiful, to, see, you, all
2. ,Wishing, you, happy, day, ahead 

我正在使用 strtok

    [token remain] = strtok(remain, ', ');

%             token = strtrim(token);
            CellArray {NumberOFCells} = token(1:end) ;  
            NumberOFCells= NumberOFCells+1;

我正在使用 CellArray 将令牌存储到单元格中,但是我的代码所做的是将第一句话放入单元格中,一旦它迭代到第二个句子并删除预先分配的单元格,因此它将其替换为令牌第二句。

预期产出

  [ nxn ] [ nxn    ] [  ] [ ]   [       ]   [     ] [   ] [  ]  [       ] ...... 
    'Hello' 'world' 'It' 'is' 'beautiful' 'to' see' 'you' 'all' 'Wishing' 'you' 'happy' 'day' 'ahead' 

问题是如何在不清除预填充单元格的情况下将第二个句子字符串附加到单元格中。

谢谢,期待与matlab程序员专家会面

我的代码..忽略注释行...检索到的基本上是这种形式。

[Index,Retrieved] = system(['wn ' keyword   type  ]);

    Retrieved;
    arrowSymbol = ' => ';
    CommaSymbol= ', '
    NumberOfSense= 'Sense '; 

    % let's look for the lines with '=> ' only?
     senses = regexp(Retrieved, [arrowSymbol '[\w, ]*\n '], 'match');
     SplitIntoCell = regexp(senses, [CommaSymbol '[\w, ]*\n'], 'match');

   % now, we take out the '=> ' symbol
    for i = 1:size(senses, 2)

        senses{i} = senses{i}(size(arrowSymbol,2):end);
        SplitIntoCell{i}= SplitIntoCell{i}(size(CommaSymbol,2): end);
%         SeperateCells= senses ([1:2 ; end-1:end]);
%         SplitCellContentIntoSingleRows{i}= strtok (SeperateCells, ['\n' ])
        numberCommas = size(regexp(senses{i}, CommaSymbol), 2);

        remain = senses{i};
        RestWord= SplitIntoCell{i};
        NumberOFCells=1;
        for j = 2:numberCommas + 1 + 1 % 1 for a word after last comma and 1 because starts at index 2

%             RemoveCellComma= regexp (Conversion,',');

%            CellArray = [CellArray strsplit(remain, ', ')];
%            [str,~] = regexp(remain,'[^, \t]+', 'match', 'split');
%            CellArray = [CellArray str];

%              [token remain] = strtok(remain, ', ');
%              token = strtrim(token);
%              CellArray {NumberOFCells} = token(1:end) ;  
%              
% %              CellArray =[CellArray strsplit(remain, ', ')]
%              [str, ~]= regexp(remain,'[^, \t]+', 'match', 'split');
%              CellArray = [CellArray str];
%              NumberOFCells= NumberOFCells+1;

            [token remain] = strtok(remain, ', ');
            token = strtrim(token);
            CellArray {NumberOFCells} = token;   
            NumberOFCells= NumberOFCells+1;

检索到=

cat, true cat
       => feline, felid
           => carnivore
               => placental, placental mammal, eutherian, eutherian mammal
                   => mammal, mammalian
                       => vertebrate, craniate
                           => chordate
                               => animal, animate being, beast, brute, creature, fauna
                                   => organism, being
                                       => living thing, animate thing
                                           => object, physical object
                                               => physical entity
                                                   => entity
4

1 回答 1

0

你的问题有点令人困惑,但阅读它(和其他评论)几次,我想我明白你在问什么。

Eitan T 对此使用正则表达式是正确的,当涉及到元胞数组时,请注意使用 [] 和 {} 进行索引/连接的差异:请参阅组合元胞数组。假设您使用循环遍历每个句子,您可以执行以下操作:

CellArray = [CellArray strsplit(next_sentence, ', ')];

使用regexp(或不区分大小写的替代regexpi),尝试添加'split'另一个函数选项,例如:

[str,~] = regexp(next_sentence,'[^, \t]+', 'match', 'split');
CellArray = [CellArray str];
于 2013-06-05T16:06:50.983 回答