[
¥È¥Ã¥×
] [
¿·µ¬
|
°ìÍ÷
|
ñ¸ì¸¡º÷
|
ºÇ½ª¹¹¿·
|
¥Ø¥ë¥×
]
³«»Ï¹Ô:
[[¥Î¡¼¥È>¥Î¡¼¥È/¥Î¡¼¥È]]~
ˬÌä¼Ô¿ô¡¡&counter();¡¡¡¡¡¡¡¡¡¡¡¡ºÇ½ª¹¹¿·¡¡&lastmod();
**Ìܼ¡ [#o465b310]
#contents
**09/12/17¡¡nltkÆâ¤«¤é¥³¥ó¥Ñ¥¤¥ëºÑ¤ß¤ÎJava(Íפ¹¤ë¤ËStanford Parser) ¤ò¸Æ½Ð¤¹¡£Ã¢¤·¹½Â¤¤ÎÃæ¿È¤ò¼«Í³¤Ë¼è¤ê½Ð¤»¤Ê¤¤¡£ [#y3709ec9]
-NLTK¤Ï¡¢Jython¤«¤é¤Ï»È¤¨¤Ê¤¤¡£Íýͳ¤Ï¤¤¤í¤¤¤í¤¢¤ë¤è¤¦¤À¤¬¡¢´ðËܤÏNLTK¤¬²¾Äꤹ¤ënumpy¥â¥¸¥å¡¼¥ë¤¬Python¤ÎC¼ÂÁõ¡ÊCPython¡Ë¤Ë°Í¸¤·¤Æ¤¤¤ë¤¿¤á¡£¤·¤¿¤¬¤Ã¤Æ¡¢¤½¤Î¤Þ¤Þ¤Ç¤ÏNLTK¤ÈStanford Parser¤Ï¶¦Â¸¤Ç¤¤Ê¤¤¡£²ò·èºö¤È¤·¤Æ¡¢¡NLTK¤ÎÃæ¤Ë¤¢¤ë¥Ñ¡¼¥¶¡¼¤òÍøÍѤ¹¤ë¡¢¢¥é¥Ã¥Ñ¡¼¤Î¤è¤¦¤ÊÊýË¡¤ÇPython¤«¤éJava¤Î¥×¥í¥°¥é¥à¤ò¸Æ¤Ó½Ð¤·¤Æ¼Â¹Ô¤µ¤»¤ë¡£
-NLTK¤ÎÃæ¤Ë¡¢Python¤«¤éJava¤Ø¤Î¥é¥Ã¥Ñ¡¼¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¤é¤·¤¤¡£»È¤¤Êý¤ÏNLTK¤Î"internals"¥â¥¸¥å¡¼¥ë¤Î¥Þ¥Ë¥å¥¢¥ë[[Package nltk :: Module internals :http://nltk.googlecode.com/svn/trunk/doc/api/index.html]]¤Îjava¤Èconfig_java¤ò»²¾È¡£
¼ÂºÝ¤ËStanford Parser¤ò¸Æ¤Ó½Ð¤·¤Æ¤ß¤ë¡£~
¤Þ¤º¡¢Java¤Î¥³¡¼¥É¤Î¥Õ¥¡¥¤¥ëParserDemo.java¤òºî¤ë¡£
import java.util.*;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
class ParserDemo {
public static void main(String[] args) {
LexicalizedParser lp = new LexicalizedParser("/usr/local/stanford-parser-2008-10-26/englishPCFG.ser.gz");
lp.setOptionFlags(new String[]{"-maxLength", "80", "-retainTmpSubcategories"});
String[] sent = { "This", "is", "an", "easy", "sentence", "." };
Tree parse = (Tree) lp.apply(Arrays.asList(sent));
parse.pennPrint();
System.out.println();
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
Collection tdl = gs.typedDependenciesCollapsed();
System.out.println(tdl);
System.out.println();
TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
tp.printTree(parse);
}
}
¤³¤ì¤ò¡¢¤¢¤é¤«¤¸¤ájavac¤Ç¥³¥ó¥Ñ¥¤¥ë¤·¤Æ¤ª¤¯¡£ParserDemo.class¤¬À¸À®¤µ¤ì¤ë¡£
¤³¤Î¥Ð¥¤¥È¥³¡¼¥É¥Õ¥¡¥¤¥ë¤ò¡¢Python/NLTK¤«¤é¸Æ¤Ó½Ð¤¹¤³¤È¤¬¤Ç¤¤ë¡£
python
>>> import nltk
>>> nltk.internals.java(['ParserDemo'],'/home/yamanouc/src/stanford:/usr/local/stanford-parser/stanford-parser.jar')
Loading parser from serialized file /usr/local/stanford-parser-2008-10-26/englishPCFG.ser.gz ... done [3.0 sec].
(ROOT [35.686]
(S [35.580]
(NP [6.140] (DT [2.301] This))
(VP [28.298] (VBZ [0.144] is)
(NP [22.961] (DT [3.221] an) (JJ [8.072] easy) (NN [8.897] sentence)))
(. [0.002] .)))
[nsubj(sentence-5, This-1), cop(sentence-5, is-2), det(sentence-5, an-3), amod(sentence-5, easy-4)]
(ROOT
(S
(NP (DT This))
(VP (VBZ is)
(NP (DT an) (JJ easy) (NN sentence)))
(. .)))
nsubj(sentence-5, This-1)
cop(sentence-5, is-2)
det(sentence-5, an-3)
amod(sentence-5, easy-4)
(None, None)
>>>
⤷¡¢¤³¤Î»È¤¤Êý¤Ï¡¢Java¤Î¥×¥í¥°¥é¥à¤ò¥Ð¥Ã¥ÁŪ¤Ë»È¤¦¤À¤±¤·¤«¤Ç¤¤º¡¢Stdin¤ÈStdout¤Ç¥Ç¡¼¥¿¤Î¼õ¤±ÅϤ·¤ò¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¤À¤±¤Ç¤¢¤ë¡£¤À¤«¤é¡¢¤Ç¤¤¿¥È¥ê¡¼¤Ï¡¢Ê¸»úÎó·Á¼°¤Ç½ÐÎϤ¹¤ë¤È¡¢¤½¤ì¤ò¤â¤¦°ìÅÙPythonÆâ¤Ç¥È¥ê¡¼¹½Â¤¤Ëºî¤êľ¤¹Êý¤¬»È¤¤°×¤¯¤Ê¤ë¤À¤í¤¦¡£¡Ê¤³¤ÎÉôʬ¤Ï¤³¤ì¤«¤é¸¡Æ¤¡Ë
**09/03/06 Jython¤«¤éJava (Íפ¹¤ë¤ËStanford Parser) ¤ò»È¤¦¡£Ã¢¤·Jython¤ÏNLTK¤È¶¦Â¸¤Ç¤¤Ê¤¤ [#z2534183]
-Python¤«¤éJava¤Î¥×¥í¥°¥é¥à¤Ë¥¢¥¯¥»¥¹¤¹¤ë¡Öɸ½àŪ¤Ê¡×ÊýË¡¤Ï¡¢Jython¤ò»È¤¦¤³¤È¤é¤·¤¤¡£¥é¥Ã¥Ñ¡¼¤â̵¤¤¤³¤È¤Ï¤Ê¤µ¤½¤¦¤À¤¬¡¢°µÅÝŪ¤ËJython¤ò»È¤¨¡¢¤È½ñ¤¤¤Æ¤¢¤ë¡£
-Jython¤Ï¡¢Python¤Î¼ÂÁõ¤Î¤Ò¤È¤Ä¤Ç¡¢ÉáÄ̤ÎPython¤ÏC¸À¸ì·Ï¤Ç¼ÂÁõ¤µ¤ì¤Æ¤¤¤ë¤Î¤ËÂФ·¡¢Jython¤ÏJava (¤ÈJava VM?) ¤Ç¼ÂÁõ¤µ¤ì¤Æ¤¤¤ë¡£
¤Ç¤Ï¡¢Jython¡Êhttp://www.jython.org¡Ë¤ò¥¤¥ó¥¹¥È¡¼¥ë¤¹¤ë¡£Oregano¤Ç¤ÏFecore Core¤Ê¤Î¤Çyum¤Ç¥¤¥ó¥¹¥È¡¼¥ë¤Ç¤¤ë¡£~
Jython¤ò¥¤¥ó¥¹¥È¡¼¥ë¤¹¤ë¤¿¤á¤Ë¤Ï¡¢Java¤¬É¬Íס£Java¤ÏJava¤Î¥µ¥¤¥È¤«¤éºÇ¿·¤Î¤â¤Î¡Ê1.6.0_12¡Ë¤ò¥¤¥ó¥¹¥È¡¼¥ë¡£
Jython¤ÇPython¤«¤éJava¥×¥í¥°¥é¥à¤ò¸Æ¤Ó½Ð¤¹¼ê½ç¤Ï¡Ê[[Jython User Guide:http://www.jython.org/Project/userguide.html]]¤Î[[Accessing Java from Jython:http://www.jython.org/Project/userguide.html#accessing-java-from-jython]]¤Ë¤è¤ë¡Ë
C:\jython>jython
Jython 2.0 on java1.2.1
Type "copyright", "credits" or "license" for more information.
>>> from java.util import Random
>>> r = Random()
>>> r.nextInt()
-790940041
>>> for i in range(5):
... print r.nextDouble()
...
0.23347681506123852
0.8526595592189546
0.3647833839988137
0.3384865260567278
0.5514469740469587
¤ä
Jython 2.0 on java1.2.1
Type "copyright", "credits" or "license" for more information.
>>> from java.util import *
>>> Random
<jclass java.util.Random at 31702169>
>>> Hashtable
<jclass java.util.Hashtable at 7538094>
¤Ê¤É¡£
¤Ç¤Ï¡¢Stanford Parser (/usr/local/stanford-parser/*) ¤ò¸Æ¤Ó½Ð¤¹¤³¤È¤ò¹Í¤¨¤ë¡£~
¤Þ¤º¥Ù¡¼¥¹¤È¤·¤ÆJavaÈǤΥ¢¥¯¥»¥¹¥×¥í¥°¥é¥à¡Ê¥Ñ¥Ã¥±¡¼¥¸Ãæ/ParserDemo.java¡Ë¤ò¸«¤Æ¤ß¤è¤¦¡£
import java.util.*;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
class ParserDemo {
public static void main(String[] args) {
LexicalizedParser lp = new LexicalizedParser("englishPCFG.ser.gz");
lp.setOptionFlags(new String[]{"-maxLength", "80", "-retainTmpSubcategories"});
String[] sent = { "This", "is", "an", "easy", "sentence", "." };
Tree parse = (Tree) lp.apply(Arrays.asList(sent));
parse.pennPrint();
System.out.println();
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
Collection tdl = gs.typedDependenciesCollapsed();
System.out.println(tdl);
System.out.println();
TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
tp.printTree(parse);
}
}
¤³¤ì¤Ï¡¢Java¤È¤·¤Æ¥³¥ó¥Ñ¥¤¥ë¡Êjavac ParserDemo.java¡Ë¢Í¼Â¹Ô¡Êjava ParserDemo¡Ë¤Ç¤¤ë¤³¤È¤ò³Îǧ¤¹¤ë¡£
$ java ParserDemo
Loading parser from serialized file englishPCFG.ser.gz ... done [3.1 sec].
(ROOT [35.686]
(S [35.580]
(NP [6.140] (DT [2.301] This))
(VP [28.298] (VBZ [0.144] is)
(NP [22.961] (DT [3.221] an) (JJ [8.072] easy) (NN [8.897] sentence)))
(. [0.002] .)))
[nsubj(sentence-5, This-1), cop(sentence-5, is-2), det(sentence-5, an-3), amod(sentence-5, easy-4)]
(ROOT
(S
(NP (DT This))
(VP (VBZ is)
(NP (DT an) (JJ easy) (NN sentence)))
(. .)))
nsubj(sentence-5, This-1)
cop(sentence-5, is-2)
det(sentence-5, an-3)
amod(sentence-5, easy-4)
¤Ç¤Ï¡¢¤³¤ì¤òPython¤«¤é¸Æ¤Ó½Ð¤¹¤è¤¦¤Ë½ñ¤´¹¤¨¤ë¡ÊParserDemo.py¡Ë¡£
#!/usr/bin/env python
# encoding: utf-8
# -*- coding: utf-8 -*-
# coding: utf-8
##
import sys
import codecs
import os
sys.path.append('/usr/local/stanford-parser/stanford-parser.jar')
#import java.util.*
from java.util import *
#import edu.stanford.nlp.trees.*
from edu.stanford.nlp.trees import *
#import edu.stanford.nlp.parser.lexparser.LexicalizedParser
from edu.stanford.nlp.parser.lexparser import LexicalizedParser
### A magic for printing UTF-8 characters
sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
#LexicalizedParser lp = new LexicalizedParser("englishPCFG.ser.gz")
lp = LexicalizedParser("/usr/local/stanford-parser/englishPCFG.ser.gz")
#optionstring = {"-maxLength ", "80", "-retainTmpSubcategories"}
optionstring = ["-maxLength", "80", "-retainTmpSubcategories"]
lp.setOptionFlags(optionstring)
##sent = [ "This", "is", "an", "easy", "sentence", "." ] #<< This works(1).
#sent = ( "This", "is", "an", "easy", "sentence", "." ) #This is also acceptable
#send_newversion = "This is an easy sentence."
#Tree parse = (Tree) lp.apply(Arrays.asList(sent))
#parse = lp.apply(Arrays.asList(sent)) #<< This works(1)
parse = lp.apply(sent)
parse.pennPrint()
print "-" * 80
#TreebankLanguagePack tlp = new PennTreebankLanguagePack();
tlp = PennTreebankLanguagePack();
#GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
gsf = tlp.grammaticalStructureFactory();
#GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
gs = gsf.newGrammaticalStructure(parse);
#Collection tdl = gs.typedDependenciesCollapsed();
tdl = gs.typedDependenciesCollapsed();
print(tdl);
print "-" * 80
#TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
tp = TreePrint("penn,typedDependenciesCollapsed");
tp.printTree(parse);
¼ç¤ÊÅÀ¤Ï¡¢
-PYTHONPATH¤òÀßÄꤷ¤Æstanford parser¤Îjar¥Ñ¥Ã¥±¡¼¥¸¤ò¸¡½Ð¤Ç¤¤ë¤è¤¦¤Ë¤¹¤ë¤¿¤á¤Ë¡¢sys.path.append¤Çstanford-parser.jar¤Î¤¢¤ê¤«¤òÄÌÃΤ¹¤ë¡£
-Java¤Ç¤Ïimport java.util.*¤È¤·¤Æ¤¤¤¿¤â¤Î¤ò¡¢from java.util import *¤Ë¤¹¤ë¡£Python¤Ç¤Ï¿¾¯½ñ¤Êý¤¬°ã¤¦¤è¤¦¤À¡£
-ÅöÁ³¡¢edu.stanford.nlp.parser.lexparser¤Ê¤É¤òimport¤¹¤ë
-Java¤Ç¤Ïnew¤ò»È¤Ã¤Æ¥¤¥ó¥¹¥¿¥ó¥¹¤òºî¤Ã¤Æ¤¤¤ë¤¬¡¢Python¤Ç¤Ïñ¤ËÊÑ¿ô¤ËÂåÆþ¤¹¤ë¤À¤±¤Ç¤¤¤¤¤é¤·¤¤¡£¤¿¤È¤¨¤Ðlp = LexicalizedParser(...)¡£
-lexicalized parser¤Îµ¯Æ°¥Ñ¥é¥á¥¿¡Ê°ú¿ô¡Ë¤òÍ¿¤¨¤ë¤È¤¡¢Java¤Ç¤Ï{"abc", "def", "ghi"}¤Î¤è¤¦¤Ë¡Êʸ»úÎó¤ÎÇÛÎó¤È¤·¤Æ¡©¡ËÍ¿¤¨¤Æ¤¤¤ë¤¬¡¢Python¤Ç¤Ï["abc", "def", "ghi"]¤Î¤è¤¦¤Ë¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£¶ñÂÎŪ¤Ë¤Ïoptionstring = ["-maxLength", "80", "-retainTmpSubcategories"]¤È¤·¤¿¡£
-Java¤ÎSystem.out.println¤Ï¡¢Python¤Ç¤Ïprint¡£
-¤¢¤È¤ÏJava¤ÈËØ¤ÉƱ¤¸¡£
¤³¤Î¥×¥í¥°¥é¥à¤òjython¤Çµ¯Æ°¼Â¹Ô¤¹¤ë¡£
$jython ParserDemo.py
Loading parser from serialized file /usr/local/stanford-parser/englishPCFG.ser.gz ... done [2.9 sec].
(ROOT [35.686]
(S [35.580]
(NP [6.140] (DT [2.301] This))
(VP [28.298] (VBZ [0.144] is)
(NP [22.961] (DT [3.221] an) (JJ [8.072] easy) (NN [8.897] sentence)))
(. [0.002] .)))
--------------------------------------------------------------------------------
[nsubj(sentence-5, This-1), cop(sentence-5, is-2), det(sentence-5, an-3), amod(sentence-5, easy-4)]
--------------------------------------------------------------------------------
(ROOT
(S
(NP (DT This))
(VP (VBZ is)
(NP (DT an) (JJ easy) (NN sentence)))
(. .)))
nsubj(sentence-5, This-1)
cop(sentence-5, is-2)
det(sentence-5, an-3)
amod(sentence-5, easy-4)
¤È¤Ê¤Ã¤¿¡£
**¤â¤¦£±¤Ä¤ÎÊýË¡ javabridge ¤ò»î¤¹ (2015-04-24, mint) [#a64559c2]
»²¾È¡§
-[[Python ¤«¤é Java ¤ò»È¤Ã¤Æ¤ß¤ë:http://d.hatena.ne.jp/shomah4a/20120707/1341671437]]
-[[javabridge¤Î¥Û¡¼¥à:http://pythonhosted.org//javabridge/]]
¥¤¥ó¥¹¥È¡¼¥ë
pip install javabridge
¿¾¯¥¨¥é¡¼·Ù¹ð¤¬½Ð¤¿¤¬½ªÎ»¡£
»È¤Ã¤Æ¤ß¤ë¡£[[¥Û¡¼¥à¤Î¥É¥¥å¥á¥ó¥È¥Ú¡¼¥¸:http://pythonhosted.org//javabridge/hello.html]] ¤Ë¤¢¤ëÎã¡£
import os
import javabridge
javabridge.start_vm(run_headless=True)
try:
print javabridge.run_script('java.lang.String.format("Hello, %s!", greetee);',
dict(greetee='world'))
finally:
javabridge.kill_vm()
¼Â¹Ô¤Ç¤¤¿¡£
Ʊ¥Ú¡¼¥¸¤Ë¤¢¤ë¾¤Î¥µ¥ó¥×¥ë¤Î¤¿¤á¤Ë¡¢wxPython¤ò¥¤¥ó¥¹¥È¡¼¥ë¡£
yum install wxPython-devel
¤¤¤¯¤Ä¤«°Í¸¥Õ¥¡¥¤¥ë¤â¥¤¥ó¥¹¥È¡¼¥ë¤µ¤ì¤¿¡£
¥¤¥ó¥¹¥È¡¼¥ë:
wxPython-devel.x86_64 0:2.8.12.0-1.el6
°Í¸À´ØÏ¢¤ò¥¤¥ó¥¹¥È¡¼¥ë¤·¤Þ¤·¤¿:
bakefile.x86_64 0:0.2.8-3.el6.centos
python-empy.noarch 0:3.3-5.el6.centos
wxBase.x86_64 0:2.8.12-1.el6.centos
wxGTK.x86_64 0:2.8.12-1.el6.centos
wxGTK-devel.x86_64 0:2.8.12-1.el6.centos
wxGTK-gl.x86_64 0:2.8.12-1.el6.centos
wxGTK-media.x86_64 0:2.8.12-1.el6.centos
wxPython.x86_64 0:2.8.12.0-1.el6
½ªÎ»¹Ô:
[[¥Î¡¼¥È>¥Î¡¼¥È/¥Î¡¼¥È]]~
ˬÌä¼Ô¿ô¡¡&counter();¡¡¡¡¡¡¡¡¡¡¡¡ºÇ½ª¹¹¿·¡¡&lastmod();
**Ìܼ¡ [#o465b310]
#contents
**09/12/17¡¡nltkÆâ¤«¤é¥³¥ó¥Ñ¥¤¥ëºÑ¤ß¤ÎJava(Íפ¹¤ë¤ËStanford Parser) ¤ò¸Æ½Ð¤¹¡£Ã¢¤·¹½Â¤¤ÎÃæ¿È¤ò¼«Í³¤Ë¼è¤ê½Ð¤»¤Ê¤¤¡£ [#y3709ec9]
-NLTK¤Ï¡¢Jython¤«¤é¤Ï»È¤¨¤Ê¤¤¡£Íýͳ¤Ï¤¤¤í¤¤¤í¤¢¤ë¤è¤¦¤À¤¬¡¢´ðËܤÏNLTK¤¬²¾Äꤹ¤ënumpy¥â¥¸¥å¡¼¥ë¤¬Python¤ÎC¼ÂÁõ¡ÊCPython¡Ë¤Ë°Í¸¤·¤Æ¤¤¤ë¤¿¤á¡£¤·¤¿¤¬¤Ã¤Æ¡¢¤½¤Î¤Þ¤Þ¤Ç¤ÏNLTK¤ÈStanford Parser¤Ï¶¦Â¸¤Ç¤¤Ê¤¤¡£²ò·èºö¤È¤·¤Æ¡¢¡NLTK¤ÎÃæ¤Ë¤¢¤ë¥Ñ¡¼¥¶¡¼¤òÍøÍѤ¹¤ë¡¢¢¥é¥Ã¥Ñ¡¼¤Î¤è¤¦¤ÊÊýË¡¤ÇPython¤«¤éJava¤Î¥×¥í¥°¥é¥à¤ò¸Æ¤Ó½Ð¤·¤Æ¼Â¹Ô¤µ¤»¤ë¡£
-NLTK¤ÎÃæ¤Ë¡¢Python¤«¤éJava¤Ø¤Î¥é¥Ã¥Ñ¡¼¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¤é¤·¤¤¡£»È¤¤Êý¤ÏNLTK¤Î"internals"¥â¥¸¥å¡¼¥ë¤Î¥Þ¥Ë¥å¥¢¥ë[[Package nltk :: Module internals :http://nltk.googlecode.com/svn/trunk/doc/api/index.html]]¤Îjava¤Èconfig_java¤ò»²¾È¡£
¼ÂºÝ¤ËStanford Parser¤ò¸Æ¤Ó½Ð¤·¤Æ¤ß¤ë¡£~
¤Þ¤º¡¢Java¤Î¥³¡¼¥É¤Î¥Õ¥¡¥¤¥ëParserDemo.java¤òºî¤ë¡£
import java.util.*;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
class ParserDemo {
public static void main(String[] args) {
LexicalizedParser lp = new LexicalizedParser("/usr/local/stanford-parser-2008-10-26/englishPCFG.ser.gz");
lp.setOptionFlags(new String[]{"-maxLength", "80", "-retainTmpSubcategories"});
String[] sent = { "This", "is", "an", "easy", "sentence", "." };
Tree parse = (Tree) lp.apply(Arrays.asList(sent));
parse.pennPrint();
System.out.println();
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
Collection tdl = gs.typedDependenciesCollapsed();
System.out.println(tdl);
System.out.println();
TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
tp.printTree(parse);
}
}
¤³¤ì¤ò¡¢¤¢¤é¤«¤¸¤ájavac¤Ç¥³¥ó¥Ñ¥¤¥ë¤·¤Æ¤ª¤¯¡£ParserDemo.class¤¬À¸À®¤µ¤ì¤ë¡£
¤³¤Î¥Ð¥¤¥È¥³¡¼¥É¥Õ¥¡¥¤¥ë¤ò¡¢Python/NLTK¤«¤é¸Æ¤Ó½Ð¤¹¤³¤È¤¬¤Ç¤¤ë¡£
python
>>> import nltk
>>> nltk.internals.java(['ParserDemo'],'/home/yamanouc/src/stanford:/usr/local/stanford-parser/stanford-parser.jar')
Loading parser from serialized file /usr/local/stanford-parser-2008-10-26/englishPCFG.ser.gz ... done [3.0 sec].
(ROOT [35.686]
(S [35.580]
(NP [6.140] (DT [2.301] This))
(VP [28.298] (VBZ [0.144] is)
(NP [22.961] (DT [3.221] an) (JJ [8.072] easy) (NN [8.897] sentence)))
(. [0.002] .)))
[nsubj(sentence-5, This-1), cop(sentence-5, is-2), det(sentence-5, an-3), amod(sentence-5, easy-4)]
(ROOT
(S
(NP (DT This))
(VP (VBZ is)
(NP (DT an) (JJ easy) (NN sentence)))
(. .)))
nsubj(sentence-5, This-1)
cop(sentence-5, is-2)
det(sentence-5, an-3)
amod(sentence-5, easy-4)
(None, None)
>>>
⤷¡¢¤³¤Î»È¤¤Êý¤Ï¡¢Java¤Î¥×¥í¥°¥é¥à¤ò¥Ð¥Ã¥ÁŪ¤Ë»È¤¦¤À¤±¤·¤«¤Ç¤¤º¡¢Stdin¤ÈStdout¤Ç¥Ç¡¼¥¿¤Î¼õ¤±ÅϤ·¤ò¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¤À¤±¤Ç¤¢¤ë¡£¤À¤«¤é¡¢¤Ç¤¤¿¥È¥ê¡¼¤Ï¡¢Ê¸»úÎó·Á¼°¤Ç½ÐÎϤ¹¤ë¤È¡¢¤½¤ì¤ò¤â¤¦°ìÅÙPythonÆâ¤Ç¥È¥ê¡¼¹½Â¤¤Ëºî¤êľ¤¹Êý¤¬»È¤¤°×¤¯¤Ê¤ë¤À¤í¤¦¡£¡Ê¤³¤ÎÉôʬ¤Ï¤³¤ì¤«¤é¸¡Æ¤¡Ë
**09/03/06 Jython¤«¤éJava (Íפ¹¤ë¤ËStanford Parser) ¤ò»È¤¦¡£Ã¢¤·Jython¤ÏNLTK¤È¶¦Â¸¤Ç¤¤Ê¤¤ [#z2534183]
-Python¤«¤éJava¤Î¥×¥í¥°¥é¥à¤Ë¥¢¥¯¥»¥¹¤¹¤ë¡Öɸ½àŪ¤Ê¡×ÊýË¡¤Ï¡¢Jython¤ò»È¤¦¤³¤È¤é¤·¤¤¡£¥é¥Ã¥Ñ¡¼¤â̵¤¤¤³¤È¤Ï¤Ê¤µ¤½¤¦¤À¤¬¡¢°µÅÝŪ¤ËJython¤ò»È¤¨¡¢¤È½ñ¤¤¤Æ¤¢¤ë¡£
-Jython¤Ï¡¢Python¤Î¼ÂÁõ¤Î¤Ò¤È¤Ä¤Ç¡¢ÉáÄ̤ÎPython¤ÏC¸À¸ì·Ï¤Ç¼ÂÁõ¤µ¤ì¤Æ¤¤¤ë¤Î¤ËÂФ·¡¢Jython¤ÏJava (¤ÈJava VM?) ¤Ç¼ÂÁõ¤µ¤ì¤Æ¤¤¤ë¡£
¤Ç¤Ï¡¢Jython¡Êhttp://www.jython.org¡Ë¤ò¥¤¥ó¥¹¥È¡¼¥ë¤¹¤ë¡£Oregano¤Ç¤ÏFecore Core¤Ê¤Î¤Çyum¤Ç¥¤¥ó¥¹¥È¡¼¥ë¤Ç¤¤ë¡£~
Jython¤ò¥¤¥ó¥¹¥È¡¼¥ë¤¹¤ë¤¿¤á¤Ë¤Ï¡¢Java¤¬É¬Íס£Java¤ÏJava¤Î¥µ¥¤¥È¤«¤éºÇ¿·¤Î¤â¤Î¡Ê1.6.0_12¡Ë¤ò¥¤¥ó¥¹¥È¡¼¥ë¡£
Jython¤ÇPython¤«¤éJava¥×¥í¥°¥é¥à¤ò¸Æ¤Ó½Ð¤¹¼ê½ç¤Ï¡Ê[[Jython User Guide:http://www.jython.org/Project/userguide.html]]¤Î[[Accessing Java from Jython:http://www.jython.org/Project/userguide.html#accessing-java-from-jython]]¤Ë¤è¤ë¡Ë
C:\jython>jython
Jython 2.0 on java1.2.1
Type "copyright", "credits" or "license" for more information.
>>> from java.util import Random
>>> r = Random()
>>> r.nextInt()
-790940041
>>> for i in range(5):
... print r.nextDouble()
...
0.23347681506123852
0.8526595592189546
0.3647833839988137
0.3384865260567278
0.5514469740469587
¤ä
Jython 2.0 on java1.2.1
Type "copyright", "credits" or "license" for more information.
>>> from java.util import *
>>> Random
<jclass java.util.Random at 31702169>
>>> Hashtable
<jclass java.util.Hashtable at 7538094>
¤Ê¤É¡£
¤Ç¤Ï¡¢Stanford Parser (/usr/local/stanford-parser/*) ¤ò¸Æ¤Ó½Ð¤¹¤³¤È¤ò¹Í¤¨¤ë¡£~
¤Þ¤º¥Ù¡¼¥¹¤È¤·¤ÆJavaÈǤΥ¢¥¯¥»¥¹¥×¥í¥°¥é¥à¡Ê¥Ñ¥Ã¥±¡¼¥¸Ãæ/ParserDemo.java¡Ë¤ò¸«¤Æ¤ß¤è¤¦¡£
import java.util.*;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
class ParserDemo {
public static void main(String[] args) {
LexicalizedParser lp = new LexicalizedParser("englishPCFG.ser.gz");
lp.setOptionFlags(new String[]{"-maxLength", "80", "-retainTmpSubcategories"});
String[] sent = { "This", "is", "an", "easy", "sentence", "." };
Tree parse = (Tree) lp.apply(Arrays.asList(sent));
parse.pennPrint();
System.out.println();
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
Collection tdl = gs.typedDependenciesCollapsed();
System.out.println(tdl);
System.out.println();
TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
tp.printTree(parse);
}
}
¤³¤ì¤Ï¡¢Java¤È¤·¤Æ¥³¥ó¥Ñ¥¤¥ë¡Êjavac ParserDemo.java¡Ë¢Í¼Â¹Ô¡Êjava ParserDemo¡Ë¤Ç¤¤ë¤³¤È¤ò³Îǧ¤¹¤ë¡£
$ java ParserDemo
Loading parser from serialized file englishPCFG.ser.gz ... done [3.1 sec].
(ROOT [35.686]
(S [35.580]
(NP [6.140] (DT [2.301] This))
(VP [28.298] (VBZ [0.144] is)
(NP [22.961] (DT [3.221] an) (JJ [8.072] easy) (NN [8.897] sentence)))
(. [0.002] .)))
[nsubj(sentence-5, This-1), cop(sentence-5, is-2), det(sentence-5, an-3), amod(sentence-5, easy-4)]
(ROOT
(S
(NP (DT This))
(VP (VBZ is)
(NP (DT an) (JJ easy) (NN sentence)))
(. .)))
nsubj(sentence-5, This-1)
cop(sentence-5, is-2)
det(sentence-5, an-3)
amod(sentence-5, easy-4)
¤Ç¤Ï¡¢¤³¤ì¤òPython¤«¤é¸Æ¤Ó½Ð¤¹¤è¤¦¤Ë½ñ¤´¹¤¨¤ë¡ÊParserDemo.py¡Ë¡£
#!/usr/bin/env python
# encoding: utf-8
# -*- coding: utf-8 -*-
# coding: utf-8
##
import sys
import codecs
import os
sys.path.append('/usr/local/stanford-parser/stanford-parser.jar')
#import java.util.*
from java.util import *
#import edu.stanford.nlp.trees.*
from edu.stanford.nlp.trees import *
#import edu.stanford.nlp.parser.lexparser.LexicalizedParser
from edu.stanford.nlp.parser.lexparser import LexicalizedParser
### A magic for printing UTF-8 characters
sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
#LexicalizedParser lp = new LexicalizedParser("englishPCFG.ser.gz")
lp = LexicalizedParser("/usr/local/stanford-parser/englishPCFG.ser.gz")
#optionstring = {"-maxLength ", "80", "-retainTmpSubcategories"}
optionstring = ["-maxLength", "80", "-retainTmpSubcategories"]
lp.setOptionFlags(optionstring)
##sent = [ "This", "is", "an", "easy", "sentence", "." ] #<< This works(1).
#sent = ( "This", "is", "an", "easy", "sentence", "." ) #This is also acceptable
#send_newversion = "This is an easy sentence."
#Tree parse = (Tree) lp.apply(Arrays.asList(sent))
#parse = lp.apply(Arrays.asList(sent)) #<< This works(1)
parse = lp.apply(sent)
parse.pennPrint()
print "-" * 80
#TreebankLanguagePack tlp = new PennTreebankLanguagePack();
tlp = PennTreebankLanguagePack();
#GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
gsf = tlp.grammaticalStructureFactory();
#GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
gs = gsf.newGrammaticalStructure(parse);
#Collection tdl = gs.typedDependenciesCollapsed();
tdl = gs.typedDependenciesCollapsed();
print(tdl);
print "-" * 80
#TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
tp = TreePrint("penn,typedDependenciesCollapsed");
tp.printTree(parse);
¼ç¤ÊÅÀ¤Ï¡¢
-PYTHONPATH¤òÀßÄꤷ¤Æstanford parser¤Îjar¥Ñ¥Ã¥±¡¼¥¸¤ò¸¡½Ð¤Ç¤¤ë¤è¤¦¤Ë¤¹¤ë¤¿¤á¤Ë¡¢sys.path.append¤Çstanford-parser.jar¤Î¤¢¤ê¤«¤òÄÌÃΤ¹¤ë¡£
-Java¤Ç¤Ïimport java.util.*¤È¤·¤Æ¤¤¤¿¤â¤Î¤ò¡¢from java.util import *¤Ë¤¹¤ë¡£Python¤Ç¤Ï¿¾¯½ñ¤Êý¤¬°ã¤¦¤è¤¦¤À¡£
-ÅöÁ³¡¢edu.stanford.nlp.parser.lexparser¤Ê¤É¤òimport¤¹¤ë
-Java¤Ç¤Ïnew¤ò»È¤Ã¤Æ¥¤¥ó¥¹¥¿¥ó¥¹¤òºî¤Ã¤Æ¤¤¤ë¤¬¡¢Python¤Ç¤Ïñ¤ËÊÑ¿ô¤ËÂåÆþ¤¹¤ë¤À¤±¤Ç¤¤¤¤¤é¤·¤¤¡£¤¿¤È¤¨¤Ðlp = LexicalizedParser(...)¡£
-lexicalized parser¤Îµ¯Æ°¥Ñ¥é¥á¥¿¡Ê°ú¿ô¡Ë¤òÍ¿¤¨¤ë¤È¤¡¢Java¤Ç¤Ï{"abc", "def", "ghi"}¤Î¤è¤¦¤Ë¡Êʸ»úÎó¤ÎÇÛÎó¤È¤·¤Æ¡©¡ËÍ¿¤¨¤Æ¤¤¤ë¤¬¡¢Python¤Ç¤Ï["abc", "def", "ghi"]¤Î¤è¤¦¤Ë¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£¶ñÂÎŪ¤Ë¤Ïoptionstring = ["-maxLength", "80", "-retainTmpSubcategories"]¤È¤·¤¿¡£
-Java¤ÎSystem.out.println¤Ï¡¢Python¤Ç¤Ïprint¡£
-¤¢¤È¤ÏJava¤ÈËØ¤ÉƱ¤¸¡£
¤³¤Î¥×¥í¥°¥é¥à¤òjython¤Çµ¯Æ°¼Â¹Ô¤¹¤ë¡£
$jython ParserDemo.py
Loading parser from serialized file /usr/local/stanford-parser/englishPCFG.ser.gz ... done [2.9 sec].
(ROOT [35.686]
(S [35.580]
(NP [6.140] (DT [2.301] This))
(VP [28.298] (VBZ [0.144] is)
(NP [22.961] (DT [3.221] an) (JJ [8.072] easy) (NN [8.897] sentence)))
(. [0.002] .)))
--------------------------------------------------------------------------------
[nsubj(sentence-5, This-1), cop(sentence-5, is-2), det(sentence-5, an-3), amod(sentence-5, easy-4)]
--------------------------------------------------------------------------------
(ROOT
(S
(NP (DT This))
(VP (VBZ is)
(NP (DT an) (JJ easy) (NN sentence)))
(. .)))
nsubj(sentence-5, This-1)
cop(sentence-5, is-2)
det(sentence-5, an-3)
amod(sentence-5, easy-4)
¤È¤Ê¤Ã¤¿¡£
**¤â¤¦£±¤Ä¤ÎÊýË¡ javabridge ¤ò»î¤¹ (2015-04-24, mint) [#a64559c2]
»²¾È¡§
-[[Python ¤«¤é Java ¤ò»È¤Ã¤Æ¤ß¤ë:http://d.hatena.ne.jp/shomah4a/20120707/1341671437]]
-[[javabridge¤Î¥Û¡¼¥à:http://pythonhosted.org//javabridge/]]
¥¤¥ó¥¹¥È¡¼¥ë
pip install javabridge
¿¾¯¥¨¥é¡¼·Ù¹ð¤¬½Ð¤¿¤¬½ªÎ»¡£
»È¤Ã¤Æ¤ß¤ë¡£[[¥Û¡¼¥à¤Î¥É¥¥å¥á¥ó¥È¥Ú¡¼¥¸:http://pythonhosted.org//javabridge/hello.html]] ¤Ë¤¢¤ëÎã¡£
import os
import javabridge
javabridge.start_vm(run_headless=True)
try:
print javabridge.run_script('java.lang.String.format("Hello, %s!", greetee);',
dict(greetee='world'))
finally:
javabridge.kill_vm()
¼Â¹Ô¤Ç¤¤¿¡£
Ʊ¥Ú¡¼¥¸¤Ë¤¢¤ë¾¤Î¥µ¥ó¥×¥ë¤Î¤¿¤á¤Ë¡¢wxPython¤ò¥¤¥ó¥¹¥È¡¼¥ë¡£
yum install wxPython-devel
¤¤¤¯¤Ä¤«°Í¸¥Õ¥¡¥¤¥ë¤â¥¤¥ó¥¹¥È¡¼¥ë¤µ¤ì¤¿¡£
¥¤¥ó¥¹¥È¡¼¥ë:
wxPython-devel.x86_64 0:2.8.12.0-1.el6
°Í¸À´ØÏ¢¤ò¥¤¥ó¥¹¥È¡¼¥ë¤·¤Þ¤·¤¿:
bakefile.x86_64 0:0.2.8-3.el6.centos
python-empy.noarch 0:3.3-5.el6.centos
wxBase.x86_64 0:2.8.12-1.el6.centos
wxGTK.x86_64 0:2.8.12-1.el6.centos
wxGTK-devel.x86_64 0:2.8.12-1.el6.centos
wxGTK-gl.x86_64 0:2.8.12-1.el6.centos
wxGTK-media.x86_64 0:2.8.12-1.el6.centos
wxPython.x86_64 0:2.8.12.0-1.el6
¥Ú¡¼¥¸Ì¾: