Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
feat: add max token and sentence argument
Browse files Browse the repository at this point in the history
  • Loading branch information
dsdanielpark committed Jul 25, 2023
1 parent c3bc9b8 commit 1b46b7f
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions bardapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,53 @@ def extract_bard_cookie():
return cookie.value
except:
continue


def max_token(text: str, n: int):
"""
Print the first 'n' tokens (words) of the given text.
Args:
text (str): The input text to be processed.
n (int): The number of tokens (words) to be printed from the beginning.
Returns:
None
"""
word_count = 0
word_start = 0
for i, char in enumerate(text):
if char.isspace():
word_count += 1
if word_count == n:
print(text[:i])
break
else:
print(text)


def max_sentence(text: str, n: int):
"""
Print the first 'n' sentences of the given text.
Args:
text (str): The input text to be processed.
n (int): The number of sentences to be printed from the beginning.
Returns:
None
"""
punctuations = set('?!.')

sentences = []
sentence_count = 0
for char in text:
sentences.append(char)
if char in punctuations:
sentence_count += 1
if sentence_count == n:
print(''.join(sentences).strip())
return
print(''.join(sentences).strip())


0 comments on commit 1b46b7f

Please sign in to comment.