Oracle文本分类:新增辅助列实现多规则集隔离方案咨询
Great question! Oracle Text absolutely supports this approach—you can use an auxiliary doc_type column to isolate rule sets without duplicating tables. Here's how it impacts your existing script and how to implement it:
Oracle Text fully supports isolating multiple rule sets using an auxiliary column, eliminating the need for separate table sets. We can achieve this by modifying table structures, training logic, and prediction logic.
Below are the core adjustments you need to make:
1. Extend Table Structures
First, add a doc_type column to relevant tables to mark the group that documents, categories, and rules belong to:
-- Add doc_type to the document table with a default value of 1 (adjust as needed) alter table ml_docs add doc_type number default 1 not null; -- Update existing documents: set network-related docs to doc_type=2 update ml_docs set doc_type = 2 where doc_id in (4,5); -- Add doc_type to the category association table, sync with the document table alter table ml_doc_categories add doc_type number not null; update ml_doc_categories dc set dc.doc_type = (select d.doc_type from ml_docs d where d.doc_id = dc.dc_doc_id); -- Add doc_type to the rules table to distinguish rule groups alter table ml_rules add doc_type number not null;
2. Adjust Training Logic
Oracle Text's ctx_cls.train procedure supports limiting the training document subset with where_clause, and writing doc_type to the rules table using rescols. You'll need to train a separate rule set for each doc_type:
-- Optional: Clear existing rules before retraining truncate table ml_rules; -- Train rule set for doc_type=1 (fast food) begin ctx_cls.train( index_name => 'ml_docsindex', docid => 'doc_id', cattab => 'ml_doc_categories', catdocid => 'dc_doc_id', catid => 'dc_category', restab => 'ml_rules', rescatid => 'rule_cat_id', resquery => 'rule_text', resconfid => 'rule_confidence', where_clause => 'doc_type = 1', -- Only train on doc_type=1 documents rescols => 'doc_type' -- Write doc_type to the rules table ); end; / -- Train rule set for doc_type=2 (computer networking) begin ctx_cls.train( index_name => 'ml_docsindex', docid => 'doc_id', cattab => 'ml_doc_categories', catdocid => 'dc_doc_id', catid => 'dc_category', restab => 'ml_rules', resquery => 'rule_text', resconfid => 'rule_confidence', where_clause => 'doc_type = 2', rescols => 'doc_type' ); end; /
3. Adjust Prediction Logic
When predicting, only match rules from the corresponding group based on the incoming document's doc_type to ensure full rule set isolation:
set serveroutput on; declare incoming_doc clob; v_doc_type number := 1; -- Specify the doc_type of the incoming document (adjust as needed) begin incoming_doc := 'I have spent my entire life managing restaurants selling burgers'; for c in ( select distinct cd_description from ml_rules r join ml_category_descriptions cd on cd.cd_category = r.rule_cat_id where r.doc_type = v_doc_type -- Only use rules for the target doc_type and matches(r.rule_text, incoming_doc) > 0 ) loop dbms_output.put_line('CATEGORY: '||c.cd_description); end loop; end; /
- Create a regular index on the
doc_typecolumn ofml_rulesto speed up prediction queries:create index ml_rules_doc_type_idx on ml_rules(doc_type); - When inserting new documents, always set the correct
doc_typevalue and ensureml_doc_categoriessyncs with the document table to maintain training accuracy.
内容的提问来源于stack exchange,提问作者Ctznkane525

