File: test.rb

package info (click to toggle)
ruby-mecab 0.99.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 336 kB
  • ctags: 494
  • sloc: cpp: 5,600; ruby: 59; makefile: 2
file content (63 lines) | stat: -rw-r--r-- 1,374 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/ruby
# -*- coding: utf-8 -*-

require 'MeCab'
sentence = "太郎はこの本を二郎を見た女性に渡した。"

begin
  
  print MeCab::VERSION, "\n"	
  model = MeCab::Model.new(ARGV.join(" "))
  tagger = model.createTagger()

  puts tagger.parse(sentence)

  n = tagger.parseToNode(sentence)

  while n do
    print n.surface,  "\t", n.feature, "\t", n.cost, "\n"
    n = n.next
  end
  print "EOS\n";

  lattice = MeCab::Lattice.new()
  lattice.set_sentence(sentence)
  tagger.parse(lattice)
  len = lattice.size()
  for i in 0..len
    b = lattice.begin_nodes(i)
    while b do
      printf "B[%d] %s\t%s\n", i, b.surface, b.feature;
      b = b.bnext 
    end
    e = lattice.end_nodes(i)
    while e do
      printf "E[%d] %s\t%s\n", i, e.surface, e.feature;
      e = e.bnext 
    end  
  end
  print "EOS\n";

  lattice.set_sentence(sentence)
  lattice.set_request_type(MeCab::MECAB_NBEST)
  tagger.parse(lattice)
  for i in 0..10
    lattice.next()
    print lattice.toString()
  end
  
  d = model.dictionary_info()
  while d do
    printf "filename: %s\n", d.filename
    printf "charset: %s\n", d.charset
    printf "size: %d\n", d.size
    printf "type: %d\n", d.type
    printf "lsize: %d\n", d.lsize
    printf "rsize: %d\n", d.rsize
    printf "version: %d\n", d.version
    d = d.next
  end
  
rescue
  print "RuntimeError: ", $!, "\n";
end