package com.vanward.dictionary; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class Word { private String spelling; private String partOfSpeech; private Collection definitions; private Collection synonyms; public Word(String spelling, String partOfSpeech) { this.spelling = spelling; this.partOfSpeech = partOfSpeech; this.definitions = new ArrayList(); this.synonyms = new ArrayList(); } public Word(String spelling, String partOfSpeech, Collection definitions) { this(spelling, partOfSpeech); if(definitions != null){ for(Iterator iter = definitions.iterator(); iter.hasNext();){ this.validateRelationship((Definition)iter.next()); } this.definitions = definitions; } } public Word(String spelling, String partOfSpeech, Collection definitions, Collection synonyms) { this(spelling, partOfSpeech, definitions); if(synonyms != null){ this.synonyms = synonyms; } } private void validateRelationship(Definition def){ if(def.getWord() == null || def.getWord() != this){ def.setWord(this); } } public Collection getDefinitions() { return definitions; } public void addDefinition(Definition definition) { this.validateRelationship(definition); this.definitions.add(definition); } public String getPartOfSpeech() { return partOfSpeech; } public void setPartOfSpeech(String partOfSpeech) { this.partOfSpeech = partOfSpeech; } public String getSpelling() { return spelling; } public void setSpelling(String spelling) { this.spelling = spelling; } public Collection getSynonyms() { return synonyms; } public void addSynonym(Word synonym) { this.synonyms.add(synonym); } } |
package com.vanward.dictionary; import java.util.Collection; public class Definition { private Word word; private String definition; private Collection exampleSentences; public Definition(String definition){ this.definition = definition; this.exampleSentences = new ArrayList(); } public Definition(String definition, Word word) { this(definition); this.word = word; } public Definition(String definition, Word word,Collection exampleSentences) { this(definition, word); if(exampleSentences != null){ this.exampleSentences = exampleSentences; } } public String getDefinition() { return definition; } public void setDefinition(String definition) { this.definition = definition; } public Collection getExampleSentences() { return exampleSentences; } public void addExampleSentence(String exampleSentence) { this.exampleSentences.add(exampleSentence); } public Word getWord() { return word; } public void setWord(Word word) { this.word = word; } } |
module Dictionary class Word attr_reader :spelling, :part_of_speech, :definitions, :synonyms attr_writer :spelling, :part_of_speech def initialize(spelling, part_of_speech, definitions = [], synonyms = []) @spelling = spelling @part_of_speech = part_of_speech definitions.each{ |idef| idef.word = self} @definitions = definitions @synonyms = synonyms end def add_definition(definition) definition.word = self if definition.word != self @definitions << definition end def add_synonym(synonym) @synonyms << synonym end end class Definition attr_reader :definition, :word, :example_sentences attr_writer :definition, :word def initialize(definition, word = nil, example_sentences = []) @definition = definition @word = word @example_sentences = example_sentences end end end |
require "dictionary" happy_wrd = Dictionary::Word.new("ebullient", "adjective") defin_one = Dictionary::Definition.new("Overflowing with enthusiasm") defin_two = Dictionary::Definition.new("Boiling up or over") happy_wrd.add_definition(defin_one) happy_wrd.add_definition(defin_two) |
require "dictionary" defin = Dictionary::Definition.new("Skill in or performance of tricks") defin_two = Dictionary::Definition.new("sleight of hand") defs = [defin, defin_two] tricky_wrd = Dictionary::Word.new("prestidigitation", "noun", defs) |
this.exampleSentences.add(exampleSentence) |
require "dictionary" idef_1 = Dictionary::Definition.new("Sad and lonely because deserted") idef_2 = Dictionary::Definition.new("Bereft; forsaken") defs = [idef_1, idef_2] idef_3 = Dictionary::Definition.new("Wretched in appearance or condition") idef_4 = Dictionary::Definition.new("Almost hopeless; desperate") defs_2 = [idef_3, idef_4] n_def = defs + defs_2 #n_def现在是[idef_1,idef_2,idef_3,idef_4] n_def[1] #生成idef_2 n_def[9] #生成nil n_def[1..2] #生成[idef_2,idef_3] |
require "dictionary" wrd = Dictionary::Word.new("turpitude", "Noun") wrd.part_of_speech # "Noun" wrd.spelling # "turpitude" wrd.spelling = "bibulous" wrd.spelling # "bibulous" syns = [Dictionary::Word.new("absorptive", "Adjective"), Dictionary::Word.new("imbibing", "Noun") ] #危险! wrd.synonyms = syns = syns #出现错误提示-"Exception: undefined method `synonyms='..." |
def initialize(spelling, part_of_speech, definitions = [], synonyms = []) @spelling = spelling @part_of_speech = part_of_speech definitions.each{ |idef| idef.word = self} @definitions = definitions @synonyms = synonyms end |
for(Iterator iter = definitions.iterator(); iter.hasNext();){ this.validateRelationship((Definition)iter.next()); } |
count = 0 File.open("./src/dictionary.rb").each { |loc| puts "#{count += 1}:" + loc } |
def add_definition(definition) definition.word = self if definition.word != self @definitions << definition end |
def add_definition(definition) if definition.word != self definition.word = self end @definitions << definition end |
package com.vanward.filter; public interface Filter { boolean applyFilter(String value); } |
package com.vanward.filter.impl; import org.apache.oro.text.regex.MalformedPatternException; import org.apache.oro.text.regex.Pattern; import org.apache.oro.text.regex.PatternCompiler; import org.apache.oro.text.regex.PatternMatcher; import org.apache.oro.text.regex.Perl5Compiler; import org.apache.oro.text.regex.Perl5Matcher; import com.vanward.filter.Filter; public class RegexPackageFilter implements Filter { private String filterExpression; private PatternCompiler compiler; private PatternMatcher matcher; public RegexPackageFilter() { this.compiler = new Perl5Compiler(); this.matcher = new Perl5Matcher(); } public RegexPackageFilter(final String filterExpression){ this(); this.filterExpression = filterExpression; } public boolean applyFilter(final String value) { try{ Pattern pattrn = this.getPattern(); return this.matcher.contains(value, pattrn); }catch(MalformedPatternException e){ throw new RuntimeException("Regular Expression was uncompilable " + e.getMessage()); } } private Pattern getPattern() throws MalformedPatternException{ return compiler.compile(this.filterExpression); } } |
private boolean applyFilters(final String value, final Filter[] filters){ boolean passed = false; for(int x = 0; (x < filters.length && !passed); x++){ passed = filters[x].applyFilter(value); } return passed; } |
class RegexFilter attr_reader :fltr_exprs def initialize(fltr_exprs) @fltr_exprs = fltr_exprs end def apply_filter(value) value =~ @fltr_exprs end end class SimpleFilter attr_reader :fltr_exprs def initialize(fltr_exprs) @fltr_exprs = fltr_exprs end def apply_filter(value) value.include?(@fltr_exprs) end end |
require "test/unit" require "filters" class FiltersTest < Test::Unit::TestCase def test_filters fltrs = [SimpleFilter.new("oo"), RegexFilter.new(/Go+gle/)] fltrs.each{ | fltr | assert(fltr.apply_filter("I love to Goooogle")) } end end |
本文出自 “青峰” 博客,转载请与作者联系!