~jtv/corpusfiltergraph/cross-python

« back to all changes in this revision

Viewing changes to trunk/lib/corpusfg/graphs/sa-champollion/ar/ar-stemmer-utf8.pl

  • Committer: tahoar
  • Date: 2012-05-02 15:46:23 UTC
  • Revision ID: svn-v4:bc069b21-dff4-4e29-a776-06a4e04bad4e::266
new layout. need to update code to use the new layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env perl
 
2
 
 
3
use strict;
 
4
 
 
5
# This is Kareem Darwish's stem_cp1256.pl modified by
 
6
# Leah Larkey, Alexander Fraser and Xiaoyi Ma
 
7
 
 
8
## Modified 2010-07-31:
 
9
# Changed morph file to parameter not environment variable
 
10
# Added binmode for utf8 STDIN and STDOUT
 
11
 
 
12
binmode(STDIN, ":utf8");
 
13
binmode(STDOUT, ":utf8");
 
14
 
 
15
## Modified 2012-01-08:
 
16
# Added $| = 1 to disable Perl buffering
 
17
$| = 1;
 
18
 
 
19
my $atb_stems = "morph.ar";
 
20
 
 
21
while (@ARGV) {
 
22
        $_ = shift;
 
23
        /^-m$/ && ($atb_stems = shift, next);
 
24
}
 
25
 
 
26
my %stem;
 
27
 
 
28
open S, "<$atb_stems" || die "$0: Cannot open $atb_stems\n";
 
29
while (<S>) {
 
30
    chomp;
 
31
    if (/^(.+)\s+(.+)$/) {
 
32
        $stem{$1} = $2;
 
33
    }
 
34
}
 
35
close S;
 
36
 
 
37
while (<>) {
 
38
    chomp;
 
39
    # split on spaces and punctuation
 
40
    s/،/\,/g;
 
41
    s/؟/\?/g;
 
42
 
 
43
    # split on spaces since tokenization was done by atoken.pl
 
44
    my @tokens = split ' ', $_;
 
45
    for my $token (@tokens) {
 
46
      # remove all non-letters (diacritics, punctuation)
 
47
      my $newtoken = "";
 
48
      while ($token =~ /\G.*?((ء|آ|أ|ؤ|إ|ئ|ا|ب|ة|ت|ث|ج|ح|خ|د|ذ|ر|ز|س|ش|ص|ض|ط|ظ|ع|غ|ف|ق|ك|ل|م|ن|ه|و|ي|ى|[\x21-\x7E])+)/g) {
 
49
        $newtoken .= $1;
 
50
      }
 
51
      $token = $newtoken;
 
52
 
 
53
      # normalize ya and Alef Maqsoura
 
54
      $token =~ s/ى/ي/g;
 
55
 
 
56
      # normalizing different alef-maad, alef-hamza-top,
 
57
      # alef-hamza-bottom, bare-alef you can choose between light and
 
58
      # aggressive normalization.  The default is aggressive.
 
59
 
 
60
      # light normalization
 
61
      # $token =~ s/(آ|أ|إ)/ا/g;
 
62
      # aggressive normalization
 
63
      $token =~ s/(ء|آ|أ|ؤ|إ|ئ)/ا/g;
 
64
 
 
65
      if (exists $stem{$token}) {
 
66
          print "$stem{$token} ";
 
67
          next;
 
68
      }# else {
 
69
#         print STDERR "$token\n";
 
70
#      }
 
71
 
 
72
 
 
73
      # this regexp will match every string. It tries to take the longest
 
74
      # possible prefix and suffix. $2 will always be defined but can be empty.
 
75
      if ($token =~ /^(وال|فال|بال|بت|يت|لت|مت|تت|وت|ست|نت|بم|لم|وم|كم|فم|ال|لل|وي|لي|سي|في|وا|فا|لا|با)(.+)$/) {
 
76
          $token = $2;
 
77
      }
 
78
      while ($token =~ /^(.+)(ات|وا|تا|ون|وه|ان|تي|ته|تم|كم|هن|هم|ها|ية|تك|نا|ين|يه|ة|ه|ي|ا)$/) {
 
79
          $token = $1;
 
80
      }
 
81
#    if ($token =~ /^(وال|فال|بال|بت|يت|لت|مت|تت|وت|ست|نت|بم|لم|وم|كم|فم|ال|لل|وي|لي|سي|في|وا|فا|لا|با|)(.+?)(ات|وا|تا|ون|وه|ان|تي|ته|تم|كم|هن|هم|ها|ية|تك|نا|ين|يه|ة|ه|ي|ا)$/) {
 
82
#         print "$2 ";
 
83
#      } else {
 
84
#         print "$token ";
 
85
#      }
 
86
      print "$token ";
 
87
  }
 
88
    print "\n";
 
89
}
 
90
 
 
91
## Saved for possible future use
 
92
## remove diacritics and kashida
 
93
#s/(ً|ٌ|ٍ|َ|ُ|ِ|ّ|ْ|ـ)//g;