/[suikacvs]/webroot/swe/lib/SWE/Object/Document.pm
Suika

Contents of /webroot/swe/lib/SWE/Object/Document.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (hide annotations) (download)
Mon Sep 14 02:41:01 2009 UTC (15 years, 7 months ago) by wakaba
Branch: MAIN
Changes since 1.6: +29 -91 lines
++ swe/lib/SWE/ChangeLog	14 Sep 2009 02:38:21 -0000
	* Lang.pm: Changed module names.

2009-09-14  Wakaba  <wakaba@suika.fam.cx>

++ swe/lib/SWE/Object/Document/ChangeLog	14 Sep 2009 02:40:50 -0000
	* SWML.pm: New module.

2009-09-14  Wakaba  <wakaba@suika.fam.cx>

++ swe/lib/SWE/Object/ChangeLog	14 Sep 2009 02:40:09 -0000
	* Document.pm: Added |rebless| method family.  Moved
	SWML-to-HTML/XML/text convertion methods to another module.

	* Document/: New directory.

2009-09-14  Wakaba  <wakaba@suika.fam.cx>

++ swe/lib/suikawiki/ChangeLog	14 Sep 2009 02:39:42 -0000
	* main.pl: Call the |rebless| method on the SWE::Object::Document
	module such that format-dependent methods can be invoked.

2009-09-14  Wakaba  <wakaba@suika.fam.cx>

1 wakaba 1.1 package SWE::Object::Document;
2     use strict;
3     use warnings;
4 wakaba 1.7 use SWE::Lang qw/%ContentMediaType/;
5     use UNIVERSAL::require;
6 wakaba 1.1
7     sub new ($%) {
8     my $class = shift;
9     my $self = bless {@_}, $class;
10    
11     return $self;
12     }
13    
14 wakaba 1.3 sub new_id ($%) {
15     my $self = shift->new (@_);
16    
17 wakaba 1.4 ## NOTE: MUST be executed in names_lock.
18    
19 wakaba 1.3 my $idgen = $self->db->id;
20     my $id = $idgen->get_next_id;
21     $self->{id} = $id;
22    
23     return $self;
24     } # new_id
25    
26 wakaba 1.1 sub db { $_[0]->{db} }
27    
28     sub id { $_[0]->{id} }
29    
30 wakaba 1.7 sub reblessed : lvalue { $_[0]->{reblessed} }
31    
32     sub rebless ($) {
33     my $self = shift;
34     return if $self->reblessed;
35    
36     my $ct = $self->content_media_type;
37     my $module = $ContentMediaType{$ct}->{module} || $ContentMediaType{'text/x-suikawiki'}->{module};
38     $module->require or die $@;
39     bless $self, $module;
40    
41     $self->reblessed = 1;
42     }
43    
44     sub content_media_type ($) {
45     return 'text/x-suikawiki';
46     }
47    
48 wakaba 1.1 sub associate_names ($$%) {
49     my ($self, $names, %args) = @_;
50    
51     ## NOTE: names_lock MUST be executed before the invocation.
52    
53     my $id = $self->id;
54     my $time = $args{time} || time;
55     my $sw3_pages = $self->{sw3_pages}; ## TODO: ...
56    
57     my $vc = $self->db->vc;
58    
59     my $name_prop_db = $self->{name_prop_db}; ## TODO: ...
60     local $name_prop_db->{version_control} = $vc;
61    
62     my $name_history_db = $self->db->name_history;
63     local $name_history_db->{version_control} = $vc;
64    
65     for my $name (keys %$names) {
66     my $name_props = $name_prop_db->get_data ($name);
67     unless (defined $name_props) {
68     my $sw3id = $sw3_pages->get_data ($name);
69     main::convert_sw3_page ($sw3id => $name); ## TODO: ...
70    
71     $name_props = $name_prop_db->get_data ($name);
72     unless (defined $name_props) {
73     $name_history_db->append_data ($name => [$time, 'c']);
74     }
75     }
76    
77     push @{$name_props->{id} ||= []}, $id;
78     $name_props->{name} = $name;
79     $name_prop_db->set_data ($name => $name_props);
80    
81     $name_history_db->append_data ($name => [$time, 'a', $id]);
82     }
83    
84     my $user = $args{user} || '(anon)';
85     $vc->commit_changes ("id=$id created by $user");
86     } # associate_names
87    
88 wakaba 1.2 sub update_tfidf ($$) {
89     my ($self, $doc) = @_; ## TODO: $doc should not be an argument
90    
91     ## It is REQUIRED to lock the $id before the invocation of this
92     ## method to keep the consistency of tfidf data for the $id.
93    
94     my $id = $self->id;
95    
96     my $tfidf_db = $self->db->id_tfidf;
97    
98     require SWE::Data::FeatureVector;
99    
100     my $deleted_terms = SWE::Data::FeatureVector->parse_stringref
101     ($tfidf_db->get_data ($id))->as_key_hashref;
102    
103     my $tc = $doc->document_element->text_content;
104    
105     ## TODO: use element semantics...
106    
107     my $orig_tfs = {};
108     my $all_terms = 0;
109     main::for_unique_words ($tc => sub {
110     $orig_tfs->{$_[0]} = $_[1];
111     $all_terms += $_[1];
112     }); ## TODO: XXX
113    
114     my $names_index_db = $self->db->name_inverted_index;
115     $names_index_db->lock;
116    
117     my $idgen = $self->db->id;
118     my $doc_number = $idgen->get_last_id;
119    
120     my $terms = SWE::Data::FeatureVector->new;
121     for my $term (keys %$orig_tfs) {
122     my $n_tf = $orig_tfs->{$term} / $all_terms;
123    
124     my $df = $names_index_db->get_count ($term);
125     my $idf = log ($doc_number / ($df + 1));
126    
127     my $tfidf = $n_tf * $idf;
128    
129     $terms->set_tfidf ($term, $tfidf);
130     $names_index_db->add_data ($term => $id => $tfidf);
131    
132     delete $deleted_terms->{$term};
133     }
134    
135     for my $term (keys %$deleted_terms) {
136     $names_index_db->delete_data ($term, $id);
137     }
138    
139     $tfidf_db->set_data ($id => \( $terms->stringify ));
140     } # update_tfidf
141    
142 wakaba 1.5 sub lock ($) {
143     my $self = shift;
144     my $lock = $self->{lock} ||= $self->{id_locks}->get_lock ($self->id); ## XXX
145     $self->{lock_n}++ or $self->lock;
146     } # lock
147    
148     sub unlock ($) {
149     my $self = shift;
150     my $lock = $self->{lock};
151     $self->{lock_n}--;
152     if ($lock and $self->{lock_n} <= 0) {
153     $lock->unlock;
154     delete $self->{lock};
155     delete $self->{lock_n};
156     }
157     } # unlock
158    
159 wakaba 1.7 for (qw/
160     to_text to_text_media_type
161     to_xml to_xml_media_type
162     to_html_fragment
163     /) {
164     eval qq{
165     sub $_ {
166     die "\$_[0]\->$_: Method not implmeneted\n";
167 wakaba 1.6 }
168 wakaba 1.7 };
169 wakaba 1.6 }
170    
171 wakaba 1.1 1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24