【问题标题】:How to group non-empty lines with PEG.js如何使用 PEG.js 对非空行进行分组
【发布时间】:2017-01-31 07:22:25
【问题描述】:

我正在尝试使用 PEG.js 解析类别文件

如何对类别进行分组(一组非空行后跟一个空行)

stopwords:fr:aux,au,de,le,du,la,a,et,avec

synonyms:en:flavoured, flavored

synonyms:en:sorbets, sherbets

en:Artisan products
fr:Produits artisanaux

< en:Artisan products
fr:Gressins artisanaux

en:Baby foods
fr:Aliments pour bébé, aliment pour bébé, alimentation pour bébé, aliment bébé, alimentation bébé, aliments bébé

< en:Baby foods
fr:Céréales pour bébé, céréales bébé

< en:Whisky
fr:Whisky écossais
es:Whiskies escoceses
wikipediacategory:Q8718387

现在我可以用这段代码逐行解析:

start = stopwords* synonyms* category+

language_and_words = l:[^:]+ ":" w:[^\n]+ {return {language: l.join(''), words: w.join('')};}

stopwords = "stopwords:" w:language_and_words "\n"+ {return {stopwords: w};}

synonyms = "synonyms:" w:language_and_words "\n"+ {return {synonyms: w};}

category_line = "< "? w:language_and_words "\n"+ {return w;}

category = c:category_line+ {return c;}

我明白了:

{
    "language": "en",
    "words": "Artisan products"
},
{
    "language": "fr",
    "words": "Produits artisanaux"
}

但我想要(针对每个组):

{
    {
        "language": "en",
        "words": "Artisan products"
    },
    {
        "language": "fr",
        "words": "Produits artisanaux"
    }
}

我也试过这个,但它没有分组,我在一些行的开头得到了 \n。

category_line = "< "? w:language_and_words "\n" {return w;}

category = c:category_line+ "\n" {return c;}

【问题讨论】:

    标签: parsing pegjs


    【解决方案1】:

    我找到了部分解决方案:

    start = category+
    
    word = c:[^,\n]+ {return c.join('');}
    
    words = w:word [,]? {return w.trim();}
    
    parent = p:"< "? {return (p !== null);}
    
    line = p:parent w:words+ "\n" {return {parent: p, words: w};}
    
    category = l:line+ "\n"? {return l;}
    

    我可以解析这个...

    < fr:a,b
    fr:aa,bb
    
    en:d,e,f
    fr:dd,ee, ffff
    

    并分组:

    [
        [ {...}, {...} ],
        [ {...}, {...} ]
    ]
    

    但是每个类别开头的“lang:”都有问题,如果我尝试解析“lang:”我的类别没有分组...

    【讨论】:

      【解决方案2】:

      我发现迭代分解解析很有用(问题分解,老派 à la Wirth)。这是我认为可以让您朝着正确方向前进的部分解决方案(我没有解析类别的Line 元素。

      start = 
        stopwords 
        synonyms 
        category+
      
      category "category"
        = category:(Line)+ categorySeparator { return category }
      
      stopwords "stopwords"
        = stopwordLine*
      
      stopwordLine "stopword line"
        = stopwordLine:StopWordMatch EndOfLine* { return stopwordLine }
      
      StopWordMatch 
        = "stopwords:" match:Text { return match }
      
      synonyms "stopwords"
        = synonymLine*
      
      synonymLine "stopword line"
        = synonymLine:SynonymMatch EndOfLine* { return synonymLine }
      
      SynonymMatch 
        = "synonyms:" match:Text { return match }
      
      Line "line"
        = line:Text [\n] { return line }
      
      Text "text"
        = [^\n]+ { return text() }
      
      EndOfLine "(end of line)"
        = '\n'
      
      EndOfFile 
        = !. { return "EOF"; }
      
      categorySeparator "separator"
        = EndOfLine EndOfLine* / EndOfLine? EndOfFile
      

      我对混合大小写的使用是随意的,不是很时髦。 还有一种方法可以在线保存解决方案:http://peg.arcanis.fr/2WQ7CZ/

      【讨论】:

        猜你喜欢
        • 2014-11-22
        • 1970-01-01
        • 2015-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-07
        • 2011-03-27
        • 2022-01-12
        相关资源
        最近更新 更多