"[SOLVED] Extract number of words in a document"

RWingerterRWingerter Member Posts: 38 Contributor II
edited June 2019 in Help
Dear all,

I created the process below in order to study the term weights created by the "Process Documents from Data" operator. For example, I found it interesting to see that TF-IDF will give weight=0 to words that occur in every document even without a stopword filter.

Now I would like to add more features to my process. In the next step I would like to have a result table containing document-IDs and the number of words in this document (and maybe the document weight, resp. the sum of its word weights).

My question is: How can I extract the number of words from a document?

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<process version="5.3.008">
 <context>
   <input/>
   <output/>
   <macros/>
 </context>
 <operator activated="true" class="process" compatibility="5.3.008" expanded="true" name="Process">
   <process expanded="true">
     <operator activated="true" class="generate_data_user_specification" compatibility="5.3.008" expanded="true" height="60" name="Generate Data by User Specification" width="90" x="45" y="30">
       <list key="attribute_values">
         <parameter key="def_id" value="1"/>
         <parameter key="def_text" value="&quot;A horse is an animal&quot;"/>
       </list>
       <list key="set_additional_roles"/>
     </operator>
     <operator activated="true" class="generate_data_user_specification" compatibility="5.3.008" expanded="true" height="60" name="Generate Data by User Specification (2)" width="90" x="45" y="120">
       <list key="attribute_values">
         <parameter key="def_id" value="2"/>
         <parameter key="def_text" value="&quot;A dog is an animal&quot;"/>
       </list>
       <list key="set_additional_roles"/>
     </operator>
     <operator activated="true" class="generate_data_user_specification" compatibility="5.3.008" expanded="true" height="60" name="Generate Data by User Specification (3)" width="90" x="45" y="210">
       <list key="attribute_values">
         <parameter key="def_id" value="3"/>
         <parameter key="def_text" value="&quot;A car is definitely not an animal but an artifact&quot;"/>
       </list>
       <list key="set_additional_roles"/>
     </operator>
     <operator activated="true" class="append" compatibility="5.3.008" expanded="true" height="112" name="Append" width="90" x="246" y="75"/>
     <operator activated="true" class="nominal_to_text" compatibility="5.3.008" expanded="true" height="76" name="Nominal to Text" width="90" x="380" y="75"/>
     <operator activated="true" class="multiply" compatibility="5.3.008" expanded="true" height="94" name="Multiply" width="90" x="514" y="75"/>
     <operator activated="true" class="set_role" compatibility="5.3.008" expanded="true" height="76" name="Set Role" width="90" x="246" y="210">
       <parameter key="attribute_name" value="def_id"/>
       <parameter key="target_role" value="id"/>
       <list key="set_additional_roles">
         <parameter key="def_text" value="regular"/>
       </list>
     </operator>
     <operator activated="true" class="text:process_document_from_data" compatibility="5.3.000" expanded="true" height="76" name="Process Documents from Data" width="90" x="380" y="210">
       <parameter key="keep_text" value="true"/>
       <list key="specify_weights"/>
       <process expanded="true">
         <operator activated="true" class="text:tokenize" compatibility="5.3.000" expanded="true" height="60" name="Tokenize" width="90" x="112" y="75"/>
         <operator activated="true" class="text:transform_cases" compatibility="5.3.000" expanded="true" height="60" name="Transform Cases" width="90" x="246" y="75"/>
         <connect from_port="document" to_op="Tokenize" to_port="document"/>
         <connect from_op="Tokenize" from_port="document" to_op="Transform Cases" to_port="document"/>
         <connect from_op="Transform Cases" from_port="document" to_port="document 1"/>
         <portSpacing port="source_document" spacing="0"/>
         <portSpacing port="sink_document 1" spacing="0"/>
         <portSpacing port="sink_document 2" spacing="0"/>
       </process>
     </operator>
     <operator activated="true" class="select_attributes" compatibility="5.3.008" expanded="true" height="76" name="Select Attributes" width="90" x="246" y="345">
       <parameter key="attribute_filter_type" value="subset"/>
       <parameter key="attributes" value="|text"/>
       <parameter key="invert_selection" value="true"/>
       <parameter key="include_special_attributes" value="true"/>
     </operator>
     <operator activated="true" class="de_pivot" compatibility="5.3.008" expanded="true" height="76" name="De-Pivot" width="90" x="380" y="345">
       <list key="attribute_name">
         <parameter key="weight" value="^(?!def_id).*"/>
       </list>
       <parameter key="index_attribute" value="def_text"/>
       <parameter key="create_nominal_index" value="true"/>
     </operator>
     <operator activated="true" class="rename" compatibility="5.3.008" expanded="true" height="76" name="Rename" width="90" x="514" y="345">
       <parameter key="old_name" value="def_text"/>
       <parameter key="new_name" value="word"/>
       <list key="rename_additional_attributes"/>
     </operator>
     <operator activated="true" class="filter_examples" compatibility="5.3.008" expanded="true" height="76" name="Filter Examples" width="90" x="648" y="300">
       <parameter key="condition_class" value="attribute_value_filter"/>
       <parameter key="parameter_string" value="weight &gt; 0"/>
     </operator>
     <connect from_op="Generate Data by User Specification" from_port="output" to_op="Append" to_port="example set 1"/>
     <connect from_op="Generate Data by User Specification (2)" from_port="output" to_op="Append" to_port="example set 2"/>
     <connect from_op="Generate Data by User Specification (3)" from_port="output" to_op="Append" to_port="example set 3"/>
     <connect from_op="Append" from_port="merged set" to_op="Nominal to Text" to_port="example set input"/>
     <connect from_op="Nominal to Text" from_port="example set output" to_op="Multiply" to_port="input"/>
     <connect from_op="Multiply" from_port="output 1" to_port="result 1"/>
     <connect from_op="Multiply" from_port="output 2" to_op="Set Role" to_port="example set input"/>
     <connect from_op="Set Role" from_port="example set output" to_op="Process Documents from Data" to_port="example set"/>
     <connect from_op="Process Documents from Data" from_port="example set" to_op="Select Attributes" to_port="example set input"/>
     <connect from_op="Process Documents from Data" from_port="word list" to_port="result 2"/>
     <connect from_op="Select Attributes" from_port="example set output" to_op="De-Pivot" to_port="example set input"/>
     <connect from_op="De-Pivot" from_port="example set output" to_op="Rename" to_port="example set input"/>
     <connect from_op="Rename" from_port="example set output" to_op="Filter Examples" to_port="example set input"/>
     <connect from_op="Filter Examples" from_port="example set output" to_port="result 3"/>
     <connect from_op="Filter Examples" from_port="original" to_port="result 4"/>
     <portSpacing port="source_input 1" spacing="0"/>
     <portSpacing port="sink_result 1" spacing="0"/>
     <portSpacing port="sink_result 2" spacing="0"/>
     <portSpacing port="sink_result 3" spacing="0"/>
     <portSpacing port="sink_result 4" spacing="0"/>
     <portSpacing port="sink_result 5" spacing="0"/>
   </process>
 </operator>
</process>
Regards

Roland

Answers

  • awchisholmawchisholm RapidMiner Certified Expert, Member Posts: 458 Unicorn
  • RWingerterRWingerter Member Posts: 38 Contributor II
    Hi Andrew,

    that's great, thank you very much. I downloaded your example process and ran it. The "Aggregate Token Length" operator is exactly what I was looking for. A first quick test with my test data delivered the word count I was looking for.

    Thanks again

    Roland
  • RWingerterRWingerter Member Posts: 38 Contributor II
    I had not been aware of the "Extract Token Number" operator, which does what I was looking for. The text mining extension is fun.

    Happy mining!

    Roland
Sign In or Register to comment.