-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
109 additions
and
6 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
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
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
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,69 @@ | ||
require "engtagger" | ||
|
||
class PartOfSpeechClassifier | ||
POS_TAG_HUMANIZED = { | ||
"nn" => "noun", | ||
"nns" => "noun", | ||
"nnp" => "noun", | ||
"nnps" => "noun", | ||
"vb" => "verb", | ||
"vbd" => "verb", | ||
"vbg" => "verb", | ||
"vbn" => "verb", | ||
"vbp" => "verb", | ||
"vbz" => "verb", | ||
"jj" => "adjective", | ||
"jjr" => "adjective", | ||
"jjs" => "adjective", | ||
"rb" => "adverb", | ||
"rbr" => "adverb", | ||
"rbs" => "adverb", | ||
"in" => "preposition", | ||
"cc" => "conjugation", | ||
"dt" => "determiner", | ||
"cd" => "number", | ||
"prp" => "pronoun", | ||
"prp$" => "pronoun", | ||
"rp" => "particle", | ||
"fw" => "misc", | ||
"sym" => "misc", | ||
"." => "punctuation" | ||
} | ||
|
||
def self.classify(word, definition) | ||
new.classify(word, definition) | ||
end | ||
|
||
def initialize | ||
@tagger = EngTagger.new | ||
end | ||
|
||
def classify(word, definition) | ||
return nil unless single_word?(word) | ||
|
||
# Tag the word alone first | ||
word_tag = @tagger.add_tags(word) | ||
word_pos = word_tag.scan(/<(\w+)>/).flatten.first | ||
|
||
# If the word alone doesn't give a clear POS, use the definition for context | ||
if word_pos == "nn" || word_pos == "nnp" | ||
text = "#{word} #{definition}" | ||
tagged = @tagger.add_tags(text) | ||
pos = tagged.scan(/<(\w+)>/).flatten.first | ||
else | ||
pos = word_pos | ||
end | ||
|
||
humanize_tag pos | ||
end | ||
|
||
private | ||
|
||
def single_word?(word) | ||
word.split.size == 1 | ||
end | ||
|
||
def humanize_tag(tag) | ||
POS_TAG_HUMANIZED[tag] || "misc." | ||
end | ||
end |
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