ltp-dep-to-con
ltp-dep-to-con
A Crystal library to construct grammar trees from LTP4 (Language Technology Platform) output (CWS, POS, NER, DEP) for Chinese-to-Vietnamese machine translation tasks.
Installation
Add this to your application's shard.yml:
dependencies:
ltp-dep-to-con:
github: chi-vi/ltp-dep-to-con
Usage
require "ltp-dep-to-con"
# Example LTP4 output tokens (1-based index usually)
# Structure: {id, word, pos, ner, head, deprel}
tokens = [
{id: 1, word: "我", pos: "r", ner: "O", head: 2, deprel: "SBV"},
{id: 2, word: "爱", pos: "v", ner: "O", head: 0, deprel: "HED"},
{id: 3, word: "北京", pos: "ns", ner: "S-Ns", head: 4, deprel: "ATT"},
{id: 4, word: "天安门", pos: "ns", ner: "S-Ns", head: 2, deprel: "VOB"}
]
# Build the tree
roots = LtpDepToCon.build_tree(tokens)
root = roots.first
# Access properties
puts root.word # => "爱"
puts root.children.first.word # => "我"
# Flatten to get linear sentence
puts root.flatten.map(&.word).join(" ") # => "我 爱 北京 天安门"
# Reordering (e.g., for Vietnamese grammar)
# Example: Swap ATT (Attribute) to after Head
# "Red Flower" (Red -> Flower)
# Assume Red is child, Flower is head.
flower = root # assuming simplified example
red = flower.children.find { |c| c.deprel == "ATT" }
if red
# Default visual_index is id. Flower id=2, Red id=1.
# Move Red to after Flower
red.visual_index = flower.visual_index + 0.5
end
puts flower.flatten.map(&.word).join(" ")
Features
- Tree Construction: Builds a traversable object tree from flat dependency parse lists.
- Reordering: Supports
visual_indexbased reordering for translation adjustments (e.g., SVO -> VSO or modifier reordering). - Data Preservation: Keeps all LTP tags (POS, NER, DEP) on the nodes.
- Translation Hooks:
vietnamese_meaningsproperty available on nodes.
Contributing
- Fork it (https://github.com/chi-vi/ltp-dep-to-con/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Repository
ltp-dep-to-con
Owner
Statistic
- 0
- 0
- 0
- 0
- 0
- 4 days ago
- February 7, 2026
License
Links
Synced at
Sat, 07 Feb 2026 05:23:50 GMT
Languages