Skip to content

Commit

Permalink
Merge pull request #64 from abdullahwaqar/feature/61-strip-prefixes
Browse files Browse the repository at this point in the history
feat(utils): Add strip_prefixes function to remove common question prefix
  • Loading branch information
woodthom2 authored Nov 18, 2024
2 parents 3a2f02f + 9b27411 commit bf578c9
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions src/harmony/parsing/util/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'''
"""
MIT License
Copyright (c) 2023 Ulster University (https://www.ulster.ac.uk).
Expand All @@ -23,5 +23,42 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
"""

from typing import List, Optional


def strip_prefixes(question: str, prefixes: Optional[List[str]] = None) -> str:
"""
Strips specified prefixes from a question string if they are present.
Args:
question (str): The question string from which prefixes need to be removed.
prefixes (Optional[List[str]]): A list of prefixes to remove from the question.
If not provided, a default set of common prefixes is used.
Returns:
str: The question string with the prefix removed, if a match is found;
otherwise, the original question.
Example:
question = "Have you ever traveled abroad?"
result = strip_prefixes(question)
# result -> "traveled abroad?"
"""
default_prefixes = [
"Have you ever",
"Did you ever",
"Do you",
"Is it true that",
"Would you say",
"Can you",
"Are you aware that",
"Do you think",
]
prefixes = prefixes or default_prefixes

for prefix in prefixes:
if question.lower().startswith(prefix.lower()):
return question[len(prefix) :].strip()
return question

0 comments on commit bf578c9

Please sign in to comment.