2016-05-30 103 views
3

我有一个很大的麻烦,除去采用Unicode编码语料中的错误模式。 在下面的表格打印在Perl中

രണവര്‍ഗ്ഗത്തിനകത്തു=ഭരണവര്‍ഗ്ഗത്തിന്:stemഅകത്തു|:suffix 
ഭസ്മമാക്കിക്കളയുകയും=ഭസ്മം:stemആക്കിക്കളയുകയും|:suffix 
ഭസ്മമാക്കി=ഭസ്മം:stemആക്കി|:suffix 
ഭാഗത്തുനിന്നുണ്ടാകണം=ഭാഗത്ത്:stemനിന്ന്:stemഉണ്ടാകണം|:suffix,: 
ഭാഗമായ=ഭാഗം:stemആയ|:suffix 
ഭാര്യമാരില്‍നിന്നും=ഭാര്യമാരില്‍:stemനിന്നും|:suffix:suffix 
ഭാര്യമാരുണ്ടായിരുന്നവരില്‍നിന്നു=ഭാര്യമാര്‍:stemഉണ്ടായിരുന്നവരില്‍:stemനിന്നു|:suffix,:suffix:suffix 
ഭാര്യയായി=ഭാര്യ:stemആയി|:suffix 
ഭാ‌ഷ്യകര്‍ത്താവായ=ഭാ‌ഷ്യകര്‍ത്താവ്:stemആയ|:suffix:suffix 
ഭിത്തികളൊക്കെ=ഭിത്തികള്‍:stemഒക്കെ|:suffix 
ഭിന്നതയില്ലെന്നും=ഭിന്നത:stemഇല്ല:stemഎന്നും|:suffix,:suffix0 
ഭൂപ്രഭുക്കളെന്ന്=ഭൂപ്രഭുക്കള്‍:stemഎന്ന്|:suffix0 
ഭൂമിയില്‍നിന്ന്=ഭൂമിയില്‍:stemനിന്ന്|:suffix 
ഭൂമിയിലുള്ള=ഭൂമിയില്‍:stemഉള്ള|:suffix 
ഭൂമിയെപ്പോലൊരു=ഭൂമിയെ:stemപോലെ:stemഒരു|:suffix,:suffix0 
ഭൂമുഖവീക്ഷണനായി=ഭൂമുഖവീക്ഷണന്‍:stemആയി|:suffix:suffix 
ഭൂസഞ്ചാരംപോലെ=ഭൂസഞ്ചാരം:stemപോലെ|:suffix 
ഭേദിക്കേണ്ടതായി=ഭേദിക്കേണ്ടതാ്:stemആയി|:suffix:suffix 
ഭൗതികവാദികളാണ്=ഭൗതികവാദികള്‍:stemആണ്|:suffix0 
മക്കളയച്ചു=മക്കള്‍:stemഅയച്ചു|:suffix 
മക്കള്‍ക്കാണ്=മക്കള്‍ക്ക്:stemആണ്|:suffix 
മഞ്ചേരിയിലേക്കാണ്=മഞ്ചേരിയിലേക്ക്:stemആണ്|:suffix:suffix 
മഞ്ചേശ്വരത്താണ്=മഞ്ചേശ്വരത്ത്:stemആണ്|:suffix:suffix 
മഞ്ഞുവെള്ളത്തിലാഴ്ത്തി=മഞ്ഞുവെള്ളത്തില്‍:stemആഴ്ത്തി|:suffix:suffix 
മടങ്ങാണിതിന്=മടങ്ങ്:stemആണ്:stemഇതിന്|:suffix,:suffix 
മടിയനായിരുന്നു=മടിയന്‍:stemആയിരുന്നു|:suffix 

,我需要两个茎一起,两个后缀去掉在一起。在两个茎的情况下,我需要保留第一个茎并将第二个茎转换为后缀。在两个后缀的这样:suffix:suffix的情况下,:suffix,:suffix0我只需要保留一个后缀

use strict; 
use warnings qw/ all FATAL /; 

use List::Util 'reduce'; 

while (<>) { 

    my ($word, $ss) =/\((/[^()]*) \) /gx; 

    my @ss = split ' ', $ss; 

    my $str = reduce { sprintf 'S (%s) (%s)', $a, $b } @ss; 

    printf "%s (%s)\n", $word, $str; 
} 

这是我试图改变Perl代码,但这些代码是不足以处理的复杂性。有什么办法来处理各种错误。

**Expected output** 

`ഭാര്യമാരുണ്ടായിരുന്നവരില്‍നിന്നു=ഭാര്യമാര്‍:stemഉണ്ടായിരുന്നവരില്‍:stemനിന്നു|:suffix,:suffix:suffix` to 
ഭാര്യമാരുണ്ടായിരുന്നവരില്‍നിന്നു=ഭാര്യമാര്‍:stemഉണ്ടായിരുന്നവരില്‍:suffixനിന്നു|:suffix 

ഭാ‌ഷ്യകര്‍ത്താവായ=ഭാ‌ഷ്യകര്‍ത്താവ്:stemആയ|:suffix:suffix to 
ഭാ‌ഷ്യകര്‍ത്താവായ=ഭാ‌ഷ്യകര്‍ത്താവ്:stemആയ|:suffix 
മഞ്ചേരിയിലേക്കാണ്=മഞ്ചേരിയിലേക്ക്:stemആണ്|:suffix:suffix to 
മഞ്ചേരിയിലേക്കാണ്=മഞ്ചേരിയിലേക്ക്:stemആണ്|:suffix 

任何一个有兴趣帮助我?

+0

@simbabque更新后 – karu

+0

你可以添加预期的输出? – simbabque

+0

我可以共享整个数据集 – karu

回答

3

说明

^([^:]+:stem[^:]+)(?::stem(?=.*?(:suffix))|)([^:]+?\|:suffix[^:]*)(?::suffix[^:]*)*$ 

替换:\1\2\3

Regular expression visualization

这个正则表达式将执行以下操作:

  • 假定每个线将有一个suffix字符串这个然后图案匹配,并拉入到捕获组2
  • 如果有一个第二stem它被替换suffix
  • 移除所有但第一suffix条目

现场演示

https://regex101.com/r/rJ9gW3/2

示例文本

ഭാര്യമാരുണ്ടായിരുന്നവരില്‍നിന്നു=ഭാര്യമാര്‍:stemഉണ്ടായിരുന്നവരില്‍:stemനിന്നു|:suffix,:suffix:suffix 
ഭാ‌ഷ്യകര്‍ത്താവായ=ഭാ‌ഷ്യകര്‍ത്താവ്:stemആയ|:suffix:suffix 
മഞ്ചേരിയിലേക്കാണ്=മഞ്ചേരിയിലേക്ക്:stemആണ്|:suffix:suffix 

样品匹配

ഭാര്യമാരുണ്ടായിരുന്നവരില്‍നിന്നു=ഭാര്യമാര്‍:stemഉണ്ടായിരുന്നവരില്‍:suffixനിന്നു|:suffix, 
ഭാ‌ഷ്യകര്‍ത്താവായ=ഭാ‌ഷ്യകര്‍ത്താവ്:stemആയ|:suffix 
മഞ്ചേരിയിലേക്കാണ്=മഞ്ചേരിയിലേക്ക്:stemആണ്|:suffix 

说明

NODE      EXPLANATION 
---------------------------------------------------------------------- 
^      the beginning of a "line" 
---------------------------------------------------------------------- 
    (      group and capture to \1: 
---------------------------------------------------------------------- 
    [^:]+     any character except: ':' (1 or more 
          times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    :stem     ':stem' 
---------------------------------------------------------------------- 
    [^:]+     any character except: ':' (1 or more 
          times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
)      end of \1 
---------------------------------------------------------------------- 
    (?:      group, but do not capture: 
---------------------------------------------------------------------- 
    :stem     ':stem' 
---------------------------------------------------------------------- 
    (?=      look ahead to see if there is: 
---------------------------------------------------------------------- 
     .*?      any character except \n (0 or more 
           times (matching the least amount 
           possible)) 
---------------------------------------------------------------------- 
     (      group and capture to \2: 
---------------------------------------------------------------------- 
     :suffix     ':suffix' 
---------------------------------------------------------------------- 
    )      end of \2 
---------------------------------------------------------------------- 
    )      end of look-ahead 
---------------------------------------------------------------------- 
    |      OR 
---------------------------------------------------------------------- 
)      end of grouping 
---------------------------------------------------------------------- 
    (      group and capture to \3: 
---------------------------------------------------------------------- 
    [^:]+?     any character except: ':' (1 or more 
          times (matching the least amount 
          possible)) 
---------------------------------------------------------------------- 
    \|      '|' 
---------------------------------------------------------------------- 
    :suffix     ':suffix' 
---------------------------------------------------------------------- 
    [^:]*     any character except: ':' (0 or more 
          times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
)      end of \3 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (0 or more times 
          (matching the most amount possible)): 
---------------------------------------------------------------------- 
    :suffix     ':suffix' 
---------------------------------------------------------------------- 
    [^:]*     any character except: ':' (0 or more 
          times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
)*      end of grouping 
---------------------------------------------------------------------- 
    $      before an optional \n, and the end of a 
          "line" 
----------------------------------------------------------------------