2024年10月16日 星期三

英文造句

 def senten(column_array):

    """進行句子處理"""

    主詞 = ["I", "He", "She", "They", "Mary", "John"]

    主詞第三人稱單數 = ["He", "She", "Mary", "John"]

    及物動詞 = ["love", "like", "see", "find"]

    受詞 = ["me", "him", "her", "them", "Mary", "John"]

    反身代名詞 = ["myself", "himself", "herself", "themselves", "herself", "himself"]


    # 對前三個字進行處理

    for i in range(3):

        if column_array[i] in 受詞:

            # 交換受詞與第三個元素

            column_array[i], column_array[2] = column_array[2], column_array[i]

        if column_array[i] in 及物動詞:

            # 交換動詞與第二個元素

            column_array[i], column_array[1] = column_array[1], column_array[i]


    # 處理反身代名詞

    if column_array[2] in 受詞 and column_array[0] in 主詞:

        column_array[2] = 反身代名詞[受詞.index(column_array[2])]


    # 處理動詞單數形式,避免重複加"s"

    if column_array[0] in 主詞第三人稱單數 and not column_array[1].endswith("s"):

        column_array[1] = column_array[1] + "s"


# 模擬的test7.txt內容

str1 = "3\nI love him\nHe sees her\nThey find John"

str2 = ""

line_array = str1.splitlines()


# 對每一行進行處理

for i in range(1, int(line_array[0]) + 1):

    column_array = line_array[i].split(" ")

    senten(column_array)

    str2 += " ".join(column_array[:3]) + ".\n"

print(str2)


# Output:

# I love himself.

# He sees herself.

# They find himself.

沒有留言:

張貼留言