def object_parse(text): ''' This function uses regular expressions to determine whether the input is an object detection command and uses regular expressions to parse the necessary data from the inputed text. @param text: The sentence to check if it is an object detection command. ***MUST BE IN LOWERCASE*** @return: A string depecting what the object is to be picked up. If the o object contains a descriptor or is two words, the program will return the whole thing. If the input text is not a locomation command, the function returns an empty string. ''' item = "" itemExp = r""" RB: {(<NN>)+} """ if isObjCommand(text): locItem = nlp_util.match_regex_and_keywords(text, itemExp, custom_tags=custom) firstItem = locItem[0] nounsList = firstItem[0] for noun in nounsList: item = item + noun[0] + " " item = item.strip() with open("util/coco.txt") as f: for line in f: if item in line: return item return None
def preprocess(text): """ Returns the same text, with all numbers converted from English words to decimal form. Ex. "move five feet forward" returns "move 5 feet forward" @param text: the original text (must be in lowercase) """ text = text.translate(str.maketrans('', '', string.punctuation)) quant = parser.parse(text) for q in quant: words = str(q).split(' ') number_word = words[0] number = int(q.value) text = text.replace(number_word, str(number)) lst = text.split(' ', 1) text = text if len(lst) <= 1 else lst[1] print(text) r_expr2 = r""" DirectionFirst: {(((<TO|IN>)<DT>)?<RB|VBD|JJ|VBP|NN|VBN><CD><NNS|NN>?)} NumberFirst: {(<CD><NNS|NN>?((<TO|IN>)<DT>)?<RB|VBD|JJ|VBP|NN|VBN>)} """ target_verbs = [ "move", "spin", "rotate", "turn", "go", "drive", "stop", "travel" ] target_words = [ "degrees", "left", "right", "forward", "backward", "clockwise", "counterclockwise" ] locPhrase, keywords = nlp_util.match_regex_and_keywords( text, r_expr2, target_words) return locPhrase, keywords
def faceRecog(text): """ This function takes a text input and determines which facial recognition command it is. It also appends the name of the command to a new file. If it is a make friends command, it will append the name of the new friend to a file. @param text: The sentence to check @return: A new file with the command name listed. """ greetings_keywords = { "wave ", "hello ", "hi ", "greetings ", "what's up ", "Wave ", "Hello ", "Hi ", "Greetings ", "What's up " } if isFaceRecognition(text): deleteFiles() if "attendance " in text: live_streaming.append_to_file("attendance.txt", "attendance") print("created new attendance file") for greeting in greetings_keywords: if greeting in text: live_streaming.append_to_file("greeting.txt", "greeting") print("created new greetings file") if ("call me ") in text or "Call me " in text: name = "" nameE = r""" nameE: {(<NNP>)+} """ namePhrase = nlp_util.match_regex_and_keywords(text, nameE) nameList = namePhrase[0][0] for noun in nameList: name = name + noun[0] + " " live_streaming.append_to_file("friends.txt", name) print("created new attendance file with " + name)
def pathPlanning(text): ''' This function uses regular expressions to determine whether the input is a locomotion command and uses regular expressions to parse the necessary data from the inputed text. @param text: The sentence to check if it is a locomotion command and parse the direction and distance from it. ***MUST BE IN LOWERCASE*** @return: A triple. The first element is the body part to move. Second element is the direction (i.e. 90 if right, 0 if forward, -90 if left, or 180 of backwards). The Third element is the distance. If the input text is not a locomation command, the function returns ("", -500, -500) by default. ''' # return variables direction = -500 moveAmmount = -500 itemMove = "" target_directions = [ "forward", "backward", "left", "right", "up", "down", "forwards", "backwards" ] target_movements = [ "strong arm", "precision arm", "body", "C1C0", "head", "cico", "c1c0", "kiko", "strongarm", "precisionarm", "strong-arm", "precision-arm" ] # if we find a path related phrase if isLocCommand(text): ammountE = r""" RB: {<CD>} """ # grabs the number of steps from the phrase movePhrase, keyword = nlp_util.match_regex_and_keywords(text, ammountE) firstItem = movePhrase[0] temp = firstItem[0] moveAmmount = temp[0] for item in target_movements: if item in text: itemMove = item # based on the direction, returns the corresponding degrees if "left" in text: direction = -90 if "right" in text: direction = 90 if "forward" in text or "up" in text: direction = 0 if "backward" in text or "down" in text: direction = 180 return (itemMove, direction, moveAmmount)
def isObjCommand(text): ''' Determines whether a string is an object detection command or not based on the sentence structure @param text: The sentence to check (must be in lowercase) Some examples of phrases include "grab the watter bottle" or "pick up the pen" @return: A boolean. True indicates that the input is an object detection command ''' r_expr = r""" VP: {<VB.*>(<RP>)?(<DT>|<PRP.*>)?(<NN>)+} """ target_verbs = ["grab", "get", "take", "pick"] verbPhrase, keywords = nlp_util.match_regex_and_keywords( text, r_expr, keywords=target_verbs, custom_tags=custom) if len(verbPhrase) > 0 and verbPhrase[0].label() != 'S': return True else: return False