(parse=Parser().parse)
| 1044 | # python -m pattern.en.parser xml -s "Hello, my name is Dr. Sbaitso. Nice to meet you." -OTCLI |
| 1045 | |
| 1046 | def commandline(parse=Parser().parse): |
| 1047 | import optparse |
| 1048 | import codecs |
| 1049 | p = optparse.OptionParser() |
| 1050 | p.add_option("-f", "--file", dest="file", action="store", help="text file to parse", metavar="FILE") |
| 1051 | p.add_option("-s", "--string", dest="string", action="store", help="text string to parse", metavar="STRING") |
| 1052 | p.add_option("-O", "--tokenize", dest="tokenize", action="store_true", help="tokenize the input") |
| 1053 | p.add_option("-T", "--tags", dest="tags", action="store_true", help="parse part-of-speech tags") |
| 1054 | p.add_option("-C", "--chunks", dest="chunks", action="store_true", help="parse chunk tags") |
| 1055 | p.add_option("-R", "--relations", dest="relations", action="store_true", help="find verb/predicate relations") |
| 1056 | p.add_option("-L", "--lemmata", dest="lemmata", action="store_true", help="find word lemmata") |
| 1057 | p.add_option("-e", "--encoding", dest="encoding", action="store_true", help="character encoding", default="utf-8") |
| 1058 | p.add_option("-v", "--version", dest="version", action="store_true", help="version info") |
| 1059 | o, arguments = p.parse_args() |
| 1060 | # Version info. |
| 1061 | if o.version: |
| 1062 | sys.path.insert(0, os.path.join(MODULE, "..", "..")) |
| 1063 | from pattern import __version__ |
| 1064 | print __version__ |
| 1065 | sys.path.pop(0) |
| 1066 | # Either a text file (-f) or a text string (-s) must be supplied. |
| 1067 | s = o.file and codecs.open(o.file, "r", o.encoding).read() or o.string |
| 1068 | # The given text can be parsed in two modes: |
| 1069 | # - implicit: parse everything (tokenize, tag/chunk, find relations, lemmatize). |
| 1070 | # - explicit: define what to parse manually. |
| 1071 | if s: |
| 1072 | explicit = False |
| 1073 | for option in [o.tokenize, o.tags, o.chunks, o.relations, o.lemmata]: |
| 1074 | if option is not None: explicit=True; break |
| 1075 | if not explicit: |
| 1076 | a = {"encoding": o.encoding } |
| 1077 | else: |
| 1078 | a = {"tokenize": o.tokenize or False, |
| 1079 | "tags": o.tags or False, |
| 1080 | "chunks": o.chunks or False, |
| 1081 | "relations": o.relations or False, |
| 1082 | "lemmata": o.lemmata or False, |
| 1083 | "encoding": o.encoding } |
| 1084 | s = parse(s, **a) |
| 1085 | # The output can be either slash-formatted string or XML. |
| 1086 | if "xml" in arguments: |
| 1087 | s = Tree(s, s.tags).xml |
| 1088 | print s |
| 1089 | |
| 1090 | #### VERBS ######################################################################################### |
| 1091 |
no test coverage detected
searching dependent graphs…