I am trying to effectively clean text that was derived from automatic speech recognition software Using spaCy phrase matcher (https://spacy.io/usage/rule-based-matching#phrasematcher). The data is very dirty and does not separate speakers, so I am trying to remove repetitive phrases across all data samples. Using the rule-based phrase matcher, I am able to find the target text in my sample strings, but in trying to replace them with spaces, I receive a type error below:
TypeError: replace() argument 1 must be str, not spacy.tokens.token.Token
My code is below:
# Import the required libraries:
import spacy
from spacy.matcher import PhraseMatcher
# Declare string from text extracted from a dataframe. Please note that there are many errors in the ASR, including words recognized incorrectly such as "mercado" which a mis-translated utterance from the IVR.
conv_str = "Welcome to companyx, to continue in English, please press one but I contin into mercado. Hello, I am V companyx, virtual assistant to best serve you during our conversation. Please provide your responses after I finished speaking in a few words please tell me what you're calling about. You can say something like I want to change my account information"
# call the matcher
matcher = PhraseMatcher(nlp.vocab, attr="LOWER")
# Declare a list of strings to search for in another string
terms = ["Welcome to CompanyX", "to continue in English, please press one", "virtual assistant", "In a few words please tell me what you're calling about", "CompanyX"]
# the stack overflow interface is incorrectly coloring some of the term strings, but it works in python
# create patterns variable
patterns = [nlp.make_doc(text) for text in terms]
matcher.add("TerminologyList", patterns)
doc = nlp(conv_str)
matches = matcher(doc)
for match_id, start, end in matches:
span = doc[start:end] # span is a list
terms_not_needed = list(span)
for item in terms_not_needed:
conv_str.replace(item, ' ')
As I mentioned, I get the TypeError as printed out above. I understand that the str.replace argument requires a string, but I was thinking that by declaring the span a list that I could iterate through that terms_not_needed list for individual string matches. Any guidance would be very helpful.