2017-05-13 10 views
-1

私は2つのファイルを持っています。最初は、各行の言葉XMLファイル内の単語のテキストファイルを検索して追加します

bus do car 
car tree 

の配列を有する第二のファイルはXMLファイルです

<title>i have a car. i take bus..</title> 

私は、XMLファイル内の各単語のテキストファイルを検索したいです。見つかった場合は、表示されているテキストファイルからすべての行を挿入し、スペースはすべてxに置き換えます。

結果ファイルが

<title>i have a car busxdoxcar carxtree. i take bus busxdoxcar..</title> 

だろう、私はこの

use strict; 
use warnings; 
use autodie; 

my $QueryFile = "query.txt"; 
my $SequenceFile = "Seq_2_terms_150.txt"; 
my %hashlist; 

open NewQueryFile ,">./NewQuery.txt" 
    or die "Cannot create NewQuery.txt"; 

open(my $fh,$SequenceFile) 
    or die "$SequenceFile : $!"; 

while (<$fh>) { 
    chop; 
    s/^\s+|\s+$//g; 
    my $h = \%hashlist; 
    foreach (split('\s+', $_)) { 
     $h->{$_} //= {};   
     $h = $h->{$_}; 
    } 
    $h->{'#'} = 1; 
} 
close $fh; 

open(my $fd, $QueryFile) 
    or die "$QueryFile : $!"; 

for my $xml (<$fd>) { 
    foreach my $line (split(/\n/, $xml)) { 
     my @words = split(/\s/, $line); 
     if $words = @hashlist[$_] { 
      print NewQueryFile join ('x',$words) ; 
     } 
    } 
} 

close NewQueryFile ; 

close($fd); 
+0

if $ words = @hashlist [$ _] 'はバグです。何をしようとしていますか? – xxfelixxx

+0

なぜあなたは '$ h = $ h - > {$ _}'をしますか? – xxfelixxx

+0

xmlファイル内の単語がハッシュリストに存在するかどうか確認したい – nicha

答えて

0

を試してみてください私は一緒に1はこれについて行くかもしれない方法を指示するための簡単なスクリプトを入れています。

私はxmlを気にしませんでした。これは悪い気分で私を残している可能性があります。

私のアドバイスは:あなたのコードが混乱してバグを起こしてしまったために保存していないものを失っても、変数を使用することです。

#!/usr/bin/env perl 

use strict; 
use warnings; 

# Notes: 

# - more than one space or tab in a row are mangled: They become one space only 
# - the query file is not checked for containing actual words to match on, 
# it is assumed to be suitable 
# - I have made no attempt to parse xml. You should use a parser for that. 
# Search Stack Overflow or Google or CPAN or all of those for examples. 
# - The replace_xml_text function can be used on the text supplied by the 
# parser to get the desired output 
# - a feeble attempt is made to deal with punctuation in replace_xml_text 
# - This code is not really tested 

my %query_words; 

my $query_fn = 'query.txt'; 
open (my $fh, "<",$query_fn) or die "could not open file '$query_fn'"; 

# build list of words from query file 

while (<$fh>){ 
    chomp; 

    # Words mentioned in line. 
    my @words = split(/\s+/,$_); 

    # Words joined by 'x'. Seems a strange choice *shrug*. 
    # This is used to replace words with later. 
    my $line = join("x",@words); 

    # Storing in arrayref, this seems easier to me 
    # than concatening repeatedly and getting the spaces right. 
    for my $word (@words){ 
     push @{$query_words{$word}}, $line; 
    } 
} 



# Expects the text to replace. 
# Returns the text to replace it with. 

sub replace_xml_text { 
    my $original_text = shift; 

    my @words; 

    for my $word (split(/\s+/,$original_text)){ 

     my $punctuation = ''; 

     # Remove punctuation before matching, 
     # but do preserve it. 
     if ($word =~s /(\s*[,.]\s*)$//){ 
      $punctuation = $1; 
     } 

     if (my $additions = $query_words{$word}){ 
      $word = join(" ",$word,@$additions); 
     } 

     # Put punctuation back. 
     $word .= $punctuation; 

     # Done replacing in this word, next 
     push @words,$word; 
    } 

    return join(" ",@words); 
} 
関連する問題