NEED HELP FIXING ERRORS IN THE FOLLOWING CODE: from typing import…
Question NEED HELP FIXING ERRORS IN THE FOLLOWING CODE: from typing import… NEED HELP FIXING ERRORS IN THE FOLLOWING CODE: from typing import Iterator, Sequence, Text, Tuple, Unionimport numpy as npfrom scipy.sparse import spmatrixNDArray = Union[np.ndarray, spmatrix]TokenSeq = Sequence[Text]PosSeq = Sequence[Text] def read_ptbtagged(ptbtagged_path: str) -> Iterator[Tuple[TokenSeq, PosSeq]]:”””Reads sentences from a Penn TreeBank .tagged file.Each sentence is a sequence of tokens and part-of-speech tags.Penn TreeBank .tagged files contain one token per line, with an empty linemarking the end of each sentence. Each line is composed of a token, a tabcharacter, and a part-of-speech tag. Here is an example: What WP ‘s VBZ next JJ ? . Slides NNS to TO illustrate VB Shostakovich NNP quartets NNS ? . :param ptbtagged_path: The path of a Penn TreeBank .tagged file, formattedas above.:return: An iterator over sentences, where each sentence is a tuple ofa sequence of tokens and a corresponding sequence of part-of-speech tags.””” with open(ptbtagged_path) as ptbtagged_file: for sentence in ptbtagged_file.read().split(‘nn’): if sentence: yield tuple(zip(*(line.split(‘t’) for line in sentence.splitlines()))) class Classifier(object): def __init__(self): “””Initializes the classifier.””” self.features = [] self.labels = [] def train(self, tagged_sentences: Iterator[Tuple[TokenSeq, PosSeq]]) -> Tuple[NDArray, NDArray]: “””Trains the classifier on the part-of-speech tagged sentences, and returns the feature matrix and label vector on which it was trained. The feature matrix should have one row per training token. The number of columns is up to the implementation, but there must at least be 1 feature for each token, named “token=T”, where “T” is the token string, and one feature for the part-of-speech tag of the preceding token, named “pos-1=P”, where “P” is the part-of-speech tag string, or “” if the token was the first in the sentence. For example, if the input is: What WP ‘s VBZ next JJ ? . Then the first row in the feature matrix should have features for “token=What” and “pos-1=“, the second row in the feature matrix should have features for “token=’s” and “pos-1=WP”, etc. The alignment between these feature names and the integer columns of the feature matrix is given by the `feature_index` method below. The label vector should have one entry per training token, and each entry should be an integer. The alignment between part-of-speech tag strings and the integers in the label vector is given by the `label_index` method below. :param tagged_sentences: An iterator over sentences, where each sentence is a tuple of a sequence of tokens and a corresponding sequence of part-of-speech tags. :return: A tuple of (feature-matrix, label-vector). “”” labels = [] features = [] for tagged_sentence in tagged_sentences: labels.append(tagged_sentence[1]) features.append(tagged_sentence[0]) return (features, labels) def feature_index(self, feature: Text) -> int: “””Returns the column index corresponding to the given named feature. The `train` method should always be called before this method is called. :param feature: The string name of a feature. :return: The column index of the feature in the feature matrix returned by the `train` method. “”” return self.features.index(feature) def label_index(self, label: Text) -> int: “””Returns the integer corresponding to the given part-of-speech tag The `train` method should always be called before this method is called. :param label: The part-of-speech tag string. :return: The integer for the part-of-speech tag, to be used in the label vector returned by the `train` method. “”” return self.labels.index(label) ERRORS:Image transcription texttest_read_ptbtagged deftest_read_ptbtagged ( ) : # keep acounter here (instead of … Show more… Show more Image transcription texttest_train_tensors deftest_train_tensors ( ) : classifier =memm. Classifier( ) ptb_tra… Show more… Show more Image transcription text= = short test summary info ===FAILED test_memm. py : :test_read_ptbtagged – ass… Show more… Show more LINK HAS THE FILE WITH THE TEST CASES AND TRAIN, TEST, DEV FILES: https://drive.google.com/drive/folders/1N5Fgj-5WUcNGkGC3F7W78k-XjKvesCKN?usp=sharing python – please help asap Computer Science Engineering & Technology Python Programming CSC 439 Share QuestionEmailCopy link Comments (0)


