0

我在这里按照指南

目前这是模型:

SOS_token = 0
EOS_token = 1


class Lang:
    def __init__(self, name):
        self.name = name
        self.word2index = {}
        self.word2count = {}
        self.index2word = {0: "SOS", 1: "EOS"}
        self.n_words = 2  # Count SOS and EOS

    def addSentence(self, sentence):
        for word in sentence.split(' '):
            self.addWord(word)

    def addWord(self, word):
        if word not in self.word2index:
            self.word2index[word] = self.n_words
            self.word2count[word] = 1
            self.index2word[self.n_words] = word
            self.n_words += 1
        else:
            self.word2count[word] += 1
def unicodeToAscii(s):
    return ''.join(
        c for c in unicodedata.normalize('NFD', s)
        if unicodedata.category(c) != 'Mn'
    )

# Lowercase, trim, and remove non-letter characters


def normalizeString(s):
    s = unicodeToAscii(s.lower().strip())
    s = re.sub(r"([.!?])", r" \1", s)
    s = re.sub(r"[^a-zA-Z.!?]+", r" ", s)
    return s
def readLangs(lang1, lang2, reverse=False):
    print("Reading lines...")

    # Read the file and split into lines
    lines = open('Scribe/%s-%s.txt' % (lang1, lang2), encoding='utf-8').\
        read().strip().split('\n')

    # Split every line into pairs and normalize
    pairs = [[normalizeString(s) for s in l.split('\t')] for l in lines]

    # Reverse pairs, make Lang instances
    if reverse:
        pairs = [list(reversed(p)) for p in pairs]
        input_lang = Lang(lang2)
        output_lang = Lang(lang1)
    else:
        input_lang = Lang(lang1)
        output_lang = Lang(lang2)

    return input_lang, output_lang, pair
MAX_LENGTH = 5000

eng_prefixes = (
    "i am ", "i m ",
    "he is", "he s ",
    "she is", "she s ",
    "you are", "you re ",
    "we are", "we re ",
    "they are", "they re "
)


def filterPair(p):
    return len(p[0].split(' ')) < MAX_LENGTH and \
        len(p[1].split(' ')) < MAX_LENGTH and \
        p[1].startswith(eng_prefixes)


def filterPairs(pairs):
    return [pair for pair in pairs if filterPair(pair)]
def prepareData(lang1, lang2, reverse=False):
    input_lang, output_lang, pairs = readLangs(lang1, lang2, reverse)
    print("Read %s sentence pairs" % len(pairs))
    pairs = filterPairs(pairs)
    print("Trimmed to %s sentence pairs" % len(pairs))
    print("Counting words...")
    for pair in pairs:
        input_lang.addSentence(pair[0])
        output_lang.addSentence(pair[1])
    print("Counted words:")
    print(input_lang.name, input_lang.n_words)
    print(output_lang.name, output_lang.n_words)
    return input_lang, output_lang, pairs

我正在尝试做的事情与指南之间的区别在于我正在尝试将我的输入语言作为字符串列表插入,而不是从文件中读取它们:

pairs=['string one goes like this', 'string two goes like this'] 
input_lang = Lang(pairs[0][0])
output_lang = Lang(pairs[1][1]) 

但是当我尝试计算字符串中的单词数时,我似乎input_lang.n_words总是得到 2。

我在上课时有什么遗漏Lang吗?

更新:
我跑了

language = Lang('english')

for sentence in pairs: language.addSentence(sentence)

print (language.n_words)

这给了我虽然字数,pairs
但并没有给我input_langoutput_lang就像指南一样:

for pair in pairs:
    input_lang.addSentence(pair[0])
    output_lang.addSentence(pair[1])
4

1 回答 1

0

所以首先你要初始化Lang对象调用pairs[0][0]pairs[1][1]它是相同Lang('s')Lang('t')

Lang对象应该是一个存储有关语言的信息的对象,因此我希望您只需将其初始化一次,Lang('english')然后Lang使用该函数将数据集中的句子添加到该对象中Lang.addSentence

现在您根本没有将数据集加载到Lang对象中,因此当您想知道language.n_words它只是创建对象时获得的初始值self.n_words = 2 # Count SOS and EOS

您在问题中所做的一切都没有任何意义,但我认为您想要的是以下内容:

language = Lang('english')

for sentence in pairs: language.addSentence(sentence)

print (language.n_words)
于 2019-04-08T13:09:23.863 回答