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

Merge Master branch #1

Merged
merged 6 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
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
27 changes: 25 additions & 2 deletions Nepali_nlp/Nepali_tokenizer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import os
import sys
sys.path.append('..')
import string

import tensorflow as tf
import sentencepiece as spm

class Tokenizer:
def __init__(self):
pass
self.this_dir, self.this_file = os.path.split(__file__)

def sentence_tokenize(self, text):
"""This function tokenize the sentences
Expand All @@ -14,7 +19,7 @@ def sentence_tokenize(self, text):
Returns:
sentence {list} -- tokenized sentence in list
"""
sentences = text.split(u"।")
sentences = text.strip().split(u"।")
sentences = [sentence.translate(str.maketrans('', '', string.punctuation)) for sentence in sentences]
return sentences

Expand Down Expand Up @@ -63,5 +68,23 @@ def character_tokenize(self, word):

return char

def sentencepeice_tokenize(self, text):
"""unsupervised way of tokenizing the text using google sentencepiece library. More info at https://github.com/google/sentencepiece

Args:
text (string): Text in Nepali language

Returns:
list: tokenized words.
"""
try:
model = tf.gfile.Gfile(os.path.join(self.this_dir, "local_dataset", "m_bpe.model"), "rb").read() #tf version 1
except:
model = tf.io.gfile.GFile(os.path.join(self.this_dir, "local_dataset", "m_bpe.model"), "rb").read() #tf version 2
sp = spm.SentencePieceProcessor()
sp.load_from_serialized_proto(model)
return sp.encode_as_pieces(text)


def __str__(self):
return "Helps to tokenize content written in Nepali language."
Binary file added Nepali_nlp/local_dataset/m_bpe.model
Binary file not shown.
Loading