Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spacy parser #346

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions hazm/dependency_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
from tqdm import tqdm
from typing import List, Tuple
import os
import spacy
from spacy.tokens import Doc
from tqdm import tqdm
from typing import List, Tuple

class MaltParser(NLTKMaltParser):
"""این کلاس شامل توابعی برای شناسایی وابستگی‌های دستوری است.
Expand Down Expand Up @@ -133,7 +137,7 @@ def parse_tagged_sents(
raise Exception("MaltParser parsing failed: %s" % " ".join(cmd))

return (
DependencyGraph(item, top_relation_label='root')
DependencyGraph(item,top_relation_label='root')
for item in open(output_file.name, encoding="utf8").read().split("\n\n") # noqa: SIM115, PTH123
if item.strip()
)
Expand Down Expand Up @@ -318,7 +322,7 @@ def parse_sents(self: MaltParser, sentences: str, verbose: bool = False) -> str:
for sentence in sentences:
self._add_sentence2dict(sentence)

tagged_sentences = self.tagger.tag_sents(sentences,universal_tag=True)
tagged_sentences = self.tagger.tag_sents(sentences)
return self.parse_tagged_sents(tagged_sentences, verbose)


Expand All @@ -332,8 +336,8 @@ def _spacy_to_conll(self,doc):
token.i + 1,
token.text.replace(" ", "_"),
self.lemmatize(token.text, token.pos_).replace(" ", "_"),
token.pos_,
token.pos_,
token.tag_,
token.tag_,
"_",
head_id,
token.dep_,
Expand All @@ -357,12 +361,13 @@ def parse_tagged_sents(self: "SpacyDependencyParser", sentences: List[List[Tuple
for doc_id , doc in enumerate(docs):
pos_tags = [tag for w , tag in sentences[doc_id]]
for i in range(len(doc)):
docs[doc_id][i].pos_ = pos_tags[i]
docs[doc_id][i].pos_ = pos_tags[i].split(',')[0]
docs[doc_id][i].tag_ = pos_tags[i]
conll_sample = self._spacy_to_conll(docs[doc_id])
conll_list.append(conll_sample)

return (
DependencyGraph(item)
DependencyGraph(item, top_relation_label='root')
for item in conll_list
if item.strip()
)
Loading