| 1 | #-*-perl-*- | ||
| 2 | # | ||
| 3 | # Copyright (C) 2004 Jörg Tiedemann <joerg@stp.ling.uu.se> | ||
| 4 | # | ||
| 5 | # This program is free software; you can redistribute it and/or modify | ||
| 6 | # it under the terms of the GNU General Public License as published by | ||
| 7 | # the Free Software Foundation; either version 2 of the License, or | ||
| 8 | # (at your option) any later version. | ||
| 9 | # | ||
| 10 | # This program is distributed in the hope that it will be useful, | ||
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | # GNU General Public License for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License | ||
| 16 | # along with this program; if not, write to the Free Software | ||
| 17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | |||
| 19 | |||
| 20 | sub getLinkScoresUwa1{ | ||
| 21 | my $self=shift; | ||
| 22 | $self->{linkProbs}={}; | ||
| 23 | &GetLinkScoresUwa1($self->{linkProbs}, | ||
| 24 | $self->{linkStreams}, | ||
| 25 | $self->{srcToken}, | ||
| 26 | $self->{trgToken}, | ||
| 27 | $self->{param}, | ||
| 28 | $self->{data}); | ||
| 29 | } | ||
| 30 | |||
| 31 | sub getLinkScoresUwa2{ | ||
| 32 | my $self=shift; | ||
| 33 | $self->{linkProbs}={}; | ||
| 34 | &GetLinkScoresUwa2($self->{linkProbs}, | ||
| 35 | $self->{linkStreams}, | ||
| 36 | $self->{srcToken}, | ||
| 37 | $self->{trgToken}, | ||
| 38 | $self->{param}, | ||
| 39 | $self->{data}); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub uwaSearch{ | ||
| 43 | my $self=shift; | ||
| 44 | my $links=shift; | ||
| 45 | my $minScore=$self->scoreThreshold(); | ||
| 46 | return &UwaAlign($links, | ||
| 47 | $self->{linkProbs}, | ||
| 48 | $self->{token}, | ||
| 49 | $self->{tokenAttr}, | ||
| 50 | $self->{srcToken}, | ||
| 51 | $self->{trgToken}, | ||
| 52 | $minScore); | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | #-------------------------------------------------------------------------- | ||
| 59 | # uwa style | ||
| 60 | #-------------------------------------------------------------------------- | ||
| 61 | |||
| 62 | |||
| 63 | |||
| 64 | sub GetLinkScoresUwa1{ | ||
| 65 | # my $self=shift; | ||
| 66 | my ($LinkProb,$links,$SrcTok,$TrgTok,$Param,$data)=@_; | ||
| 67 | |||
| 68 | foreach my $s (keys %{$SrcTok}){ | ||
| 69 | foreach my $t (keys %{$TrgTok}){ | ||
| 70 | |||
| 71 | my ($src,$trg)=($$SrcTok{$s}{general},$$TrgTok{$t}{general}); | ||
| 72 | if ($data->checkPairParameter($src,$trg,$$Param{general})){ | ||
| 73 | # if (&TokenPairParameter($src,$trg,$$Param{general})){ | ||
| 74 | |||
| 75 | foreach (keys %{$links}){ | ||
| 76 | my ($src,$trg)=($$SrcTok{$s}{$_},$$TrgTok{$t}{$_}); | ||
| 77 | if ($src and $trg){ | ||
| 78 | if ($data->checkPairParameter($src,$trg,$$Param{$_})){ | ||
| 79 | # if (&TokenPairParameter($src,$trg,$$Param{$_})){ | ||
| 80 | my %search=('source' => $src, | ||
| 81 | 'target' => $trg); | ||
| 82 | my $found=uplugTreeData->new; | ||
| 83 | if ($links->{$_}->select($found,\%search)){ | ||
| 84 | if (&CheckScore($found,$$Param{$_})){ | ||
| 85 | my $p=$$LinkProb{"$s\x00\x00$t"}; | ||
| 86 | |||
| 87 | my $score=$found->attribute('score'); | ||
| 88 | |||
| 89 | # shouldn't be >1, but in case ... | ||
| 90 | #-------------------------------- | ||
| 91 | if ($score>1){$score=1;} | ||
| 92 | #-------------------------------- | ||
| 93 | $$LinkProb{$_}{"$s\x00\x00$t"}=$score; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | foreach my $l (keys %{$LinkProb}){ | ||
| 103 | if ($$LinkProb{$l}<$$Param{general}{'minimal score'}){ | ||
| 104 | delete $$LinkProb{$l}; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | sub UwaAlign{ | ||
| 112 | # my $self=shift; | ||
| 113 | my ($Links,$LinkProb,$Token,$TokenAttr,$SrcTok,$TrgTok,$MinScore)=@_; | ||
| 114 | my @steps=( | ||
| 115 | 'identical num', | ||
| 116 | 'co-occurrence', | ||
| 117 | 'string similarities', | ||
| 118 | 'basic', | ||
| 119 | 'low', | ||
| 120 | 'previous', | ||
| 121 | 'co-occurrence', | ||
| 122 | 'string similarities', | ||
| 123 | '1:1' | ||
| 124 | ); | ||
| 125 | my @para; | ||
| 126 | $para[1]{score}=0.7; | ||
| 127 | $para[2]{score}=0.7; | ||
| 128 | $para[6]{score}=0.4; | ||
| 129 | $para[7]{score}=0.4; | ||
| 130 | |||
| 131 | foreach (0..$#steps){ | ||
| 132 | |||
| 133 | if ($steps[$_]=~/identical num/){ | ||
| 134 | foreach my $s (0..$#{$$SrcTok{general}}){ | ||
| 135 | my $src=$$SrcTok{general}[$s]; | ||
| 136 | &SetLanguage($lang{source}); | ||
| 137 | if (&IsNumeric($src)){ | ||
| 138 | foreach my $t (0..$#{$$TrgTok{general}}){ | ||
| 139 | my $trg=$$TrgTok{general}[$t]; | ||
| 140 | if ($trg eq $src){ | ||
| 141 | &AddLink($s,$t,$Links,$Token,$TokenAttr); | ||
| 142 | $Links->{$s}->{step}=$_; | ||
| 143 | &RemoveOverlap($s,$SrcTok); | ||
| 144 | &RemoveOverlap($t,$TrgTok); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | } | ||
| 150 | elsif ($steps[$_]=~/1\:(.)/){ | ||
| 151 | my $nr=$1; | ||
| 152 | if ($nr==1){ | ||
| 153 | if ((scalar keys %{$SrcTok} == 1) and | ||
| 154 | (scalar keys %{$TrgTok} == 1)){ | ||
| 155 | my ($s)=each %{$SrcTok}; | ||
| 156 | my ($t)=each %{$TrgTok}; | ||
| 157 | &AddLink($s,$t,$Links,$Token,$TokenAttr); | ||
| 158 | $Links->{$s}->{step}=$_; | ||
| 159 | &RemoveOverlap($s,$SrcTok); | ||
| 160 | &RemoveOverlap($t,$TrgTok); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | else{ | ||
| 164 | if ((scalar keys %{$SrcTok} == 1) or | ||
| 165 | (scalar keys %{$TrgTok} == 1)){ | ||
| 166 | my ($s)=each %{$SrcTok}; | ||
| 167 | my ($t)=each %{$TrgTok}; | ||
| 168 | &AddLink($s,$t,$Links,$Token,$TokenAttr); | ||
| 169 | $Links->{$s}->{step}=$_; | ||
| 170 | &RemoveOverlap($s,$SrcTok); | ||
| 171 | &RemoveOverlap($t,$TrgTok); | ||
| 172 | } | ||
| 173 | } | ||
| 174 | } | ||
| 175 | elsif ($steps[$_]=~/basic/){ | ||
| 176 | } | ||
| 177 | elsif ($steps[$_]=~/previous/){ | ||
| 178 | } | ||
| 179 | elsif ($steps[$_]=~/low/){ | ||
| 180 | } | ||
| 181 | elsif (defined $LinkProb->{$steps[$_]}){ | ||
| 182 | if (defined $para[$_]{score}){ | ||
| 183 | $MinScore=$para[$_]{score}; | ||
| 184 | } | ||
| 185 | &BestFirstSearch($Links,$LinkProb->{$steps[$_]}, | ||
| 186 | $Token,$TokenAttr,$SrcTok,$TrgTok,$MinScore,$_); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | sub AddLink{ | ||
| 193 | # my $self=shift; | ||
| 194 | my ($s,$t,$Links,$Token,$TokenAttr)=@_; | ||
| 195 | my $link=&GetLinkString($TokenAttr,$s,$t); | ||
| 196 | |||
| 197 | $$Links{$s}{link}=$link; | ||
| 198 | $$Links{$s}{source}=&NgramIDs($s,$TokenAttr,'source'); | ||
| 199 | $$Links{$s}{target}=&NgramIDs($t,$TokenAttr,'target'); | ||
| 200 | my $span=&NgramSpans($s,$TokenAttr,'source'); | ||
| 201 | if ($span){$$Links{$s}{src}=$span;} | ||
| 202 | $span=&NgramSpans($t,$TokenAttr,'target'); | ||
| 203 | if ($span){$$Links{$s}{trg}=$span;} | ||
| 204 | } | ||
| 205 | |||
| 206 | 1; |