-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't run chunker for perf and add dictionary cleaning transform
- Loading branch information
Showing
4 changed files
with
72 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/org/ohnlp/medtagger/backbone/CleanMedTaggerDictOutputTransform.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.ohnlp.medtagger.backbone; | ||
|
||
import org.apache.beam.sdk.coders.BigEndianLongCoder; | ||
import org.apache.beam.sdk.coders.KvCoder; | ||
import org.apache.beam.sdk.coders.RowCoder; | ||
import org.apache.beam.sdk.coders.StringUtf8Coder; | ||
import org.apache.beam.sdk.schemas.Schema; | ||
import org.apache.beam.sdk.schemas.transforms.Select; | ||
import org.apache.beam.sdk.transforms.*; | ||
import org.apache.beam.sdk.values.KV; | ||
import org.apache.beam.sdk.values.PCollection; | ||
import org.apache.beam.sdk.values.Row; | ||
import org.checkerframework.checker.initialization.qual.Initialized; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.checkerframework.checker.nullness.qual.UnknownKeyFor; | ||
import org.joda.time.Duration; | ||
import org.ohnlp.backbone.api.annotations.ComponentDescription; | ||
import org.ohnlp.backbone.api.components.OneToOneTransform; | ||
import org.ohnlp.backbone.api.exceptions.ComponentInitializationException; | ||
import org.ohnlp.medtagger.lvg.LvgLookup; | ||
|
||
@ComponentDescription( | ||
name = "Get Dict Freqs", | ||
desc = "Gets Frequency of Dictionary Terms (Useful for Cleaning Noise from Autogenerated Dictionary Entries)" | ||
) | ||
public class CleanMedTaggerDictOutputTransform extends OneToOneTransform { | ||
private final Schema schema = Schema.of( | ||
Schema.Field.of("matched_text", Schema.FieldType.STRING), | ||
Schema.Field.of("freq", Schema.FieldType.INT64) | ||
); | ||
|
||
@Override | ||
public Schema calculateOutputSchema(Schema schema) { | ||
return this.schema; | ||
} | ||
|
||
@Override | ||
public PCollection<Row> expand(PCollection<Row> input) { | ||
return input.apply(Select.fieldNames("matched_text", "note_source_value")).apply(ParDo.of( | ||
new DoFn<Row, KV<Row, String>>() { | ||
private LvgLookup lvg; | ||
@ProcessElement | ||
public void process(ProcessContext pc) { | ||
Row input = pc.element(); | ||
String text = input.getString("matched_text"); | ||
text = lvg.getNorm(text).replaceAll("\\s", "\t"); | ||
pc.output(KV.of(Row.withSchema(schema).addValues(text, 1L).build(), input.getString("note_source_value"))); | ||
} | ||
@Setup | ||
public void init() { | ||
this.lvg = new LvgLookup(); | ||
lvg.localInitialize(CleanMedTaggerDictOutputTransform.class.getResourceAsStream("/medtaggerresources/lvg/LRAGR_2021AB"), CleanMedTaggerDictOutputTransform.class.getResourceAsStream("/medtaggerresources/lvg/openclasswords.txt")); | ||
} | ||
} | ||
)).setCoder(KvCoder.of(RowCoder.of(this.schema), StringUtf8Coder.of()) | ||
).apply(Distinct.create() | ||
).apply(Count.perKey() | ||
).setCoder(KvCoder.of(RowCoder.of(this.schema), BigEndianLongCoder.of()) | ||
).apply(MapElements.via(new SimpleFunction<KV<Row, Long>, Row>() { | ||
@Override | ||
public Row apply(KV<Row, Long> input) { | ||
return Row.withSchema(schema).addValues(input.getKey().getValue("matched_text"), input.getValue()).build(); | ||
} | ||
})); | ||
} | ||
|
||
@Override | ||
public void init() throws ComponentInitializationException { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters