创建匹配的括号

编程入门 行业动态 更新时间:2024-10-09 15:17:59
本文介绍了创建匹配的括号-awk:sed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个具有三种模式的数据集:

I have a data set that has three patterns:

第一:

abrasion abrade:stem<>ion:suffix abstainer abstain:stem<>er:suffix abstention abstain:stem<>ion:suffix

第二:

inaccurate in:prefix<>accurate:stem inactive in:prefix<>active:stem

第三:

incommunicable in:prefix<>communicate:stem<>able:suffix incompatibility in:prefix<>compatible:stem<>ity:suffix

我需要将以上形式转换为以下形式:匹配宾夕法尼亚州树银行的方括号( languagelog.ldc.upenn.edu/myl/PennTreebank1995.pdf )

I need to convert the above to following form : Matching the brackets in the way for Penn Tree Bank (languagelog.ldc.upenn.edu/myl/PennTreebank1995.pdf)

第一:

abrasion ((abrade:stem) ion:suffix) abstainer ((abstain:stem)er:suffix) abstention ((abstain:stem)ion:suffix)

第二:

inaccurate (in:prefix(accurate:stem)) inactive (in:prefix(active:stem))

第三:

incommunicable (in:prefix ((communicate:stem)able:suffix)) incompatibility (in:prefix ((compatible:stem)ity:suffix))

我正在工作的代码正在使用awk

The code, I am working is using awk

{ n = gsub(/<>/,")",$2) s = sprintf("%*s",n,"") gsub(/ /,"(",s) print "(" $1, s "((" $2 "))" }

编辑

更复杂的表格

nationalistic national: stem <>ism:suffix<>ist:suffix<>ic:suffix

收件人:

nationalistic ((((national: stem) ism:suffix)ist:suffix)ic:suffix)

没有产生示例中提到的预期输出.

It is not producing the expected outputs that mentioned in the examples.

推荐答案

这应该足够通用,因为它考虑了:stem,:prefix和:suffix进行匹配:

This should be general enough as it takes into account :stem, :prefix, and :suffix for matching:

awk 'BEGIN{FS=OFS="\n"}{ a=gensub(/([a-zA-Z]*):stem/,"(\\1:stem)", "g"); b=gensub(/(\([a-zA-Z]*:stem\))<>([a-zA-Z]*):suffix/,"(\\1\\2:suffix)", "g", a); c=gensub(/([a-zA-Z]*:prefix)<>(.*)/,"(\\1\\2)", "g", b); print c;}' testfile

此处演示: ideone/U3ux91

编辑

这应该照顾多个后缀和前缀:

This should take care of multiple suffixes and prefixes:

awk 'BEGIN{FS=OFS="\n"}{ a=gensub(/([a-zA-Z]*):stem/,"(\\1:stem)", "g"); while ( a ~ /stem)<>.*:suffix/) { a=gensub(/(\([a-zA-Z]*:stem\).*?)<>([a-zA-Z]*):suffix/,"(\\1\\2:suffix)", "g", a); } while ( a ~ /<>/) { a=gensub(/([a-zA-Z]*?:prefix)<>(.*)/,"(\\1\\2)", "g", a); } print a;}' test

此处演示: ideone/U7LYXi (很抱歉,如果不是反民族主义,而是为了测试……)

Demo here: ideone/U7LYXi (sorry if antinationalistic is not a word, but for testing sake....)

更多推荐

创建匹配的括号

本文发布于:2023-11-29 02:29:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644939.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:括号

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!