0

第 2 轮:在电子邮件中挑选领导者 好的,所以我的下一个问题是试图找出项目中的领导者。为了确定这一点,我们会收到一封电子邮件,并且必须找到说“你想要...”的人(大小写可能会有所不同)。我觉得我的代码大部分应该都可以工作,但是我在弄清楚如何正确填充单元格数组时确实遇到了问题。我可以用它来创建单元阵列,但它只是将电子邮件重新放入其中。所以每个单元格基本上就是名字。

function[Leader_Name] = teamPowerHolder(email)

email = fopen(email, 'r'); %// Opens my file
lines = fgets(email); %// Reads the first line

conversations = {lines}; %// Creates my cell array


while ischar(lines) %// Populates my cell array, just not correct
    Convo = fgets(email);
    if Convo == -1 %// Prevents it from just logging -1 into my cell array like a jerk
        break; %// Returns to function
    end
    conversations = [conversations {lines}]; %// Populates my list
end
Sentences = strfind(conversations,'Do you want'); %// Locates the leader position


Leader_Name = Sentences{1}; %// Indexes that position

fclose(email);
end

我理想情况下需要它做的是找到'/n'字符(因此我使用fgets),但我不知道如何让它做到这一点。我试图让我的while循环像:

while lines == '/n'

但这是不正确的。我觉得我知道如何做'/n'位,我只是想不出。所以我会很感激一些提示或技巧来做到这一点。我总是可以尝试 strsplit 或 strtok 函数,但我需要然后填充我的单元格数组,这样可能会变得混乱。

请并感谢您的帮助:)

Test Case:
Anna: Hey guys, so I know that he just assigned this project, but I want to go ahead   and get started on it.
Can you guys please respond and let me know a weekly meeting time that will work for you?

Wiley: Ummmmm no because ain't nobody got time for that.

John: Wiley? What kind of a name is that? .-.

Wiley: It's better than john. >.>

Anna: Hey boys, let's grow up and talk about a meeting time.
Do you want to have a weekly meeting, or not?

Wiley: I'll just skip all of them and not end up doing anything for the project anyway.
So I really don't care so much.

John: Yes, Anna, I'd like to have a weekly meeting.
Thank you for actually being a good teammate and doing this. :)

out2 = teamPowerHolder('teamPowerHolder_convo2.txt')
    => 'Anna'
4

1 回答 1

1

它不起作用的主要原因是因为您应该更新lines循环中的变量,但是您正在创建一个名为Convo正在更新的新变量。这就是为什么每次放入lines元胞数组时,它只是重复放入第一行并且从不退出循环。


但是,我建议您在每一行中读取,然后查找:字符,然后提取字符串直到您第一次遇到该字符减 1,因为您不想包含实际:字符本身。这很可能对应于说话人的姓名。如果我们错过了这个事件,那么那个人还在说话。因此,您必须保留一个变量来跟踪当前仍在说话的人,直到找到“do you want”字符串。不管谁这么说,我们都会返回当前正在说话的人,当然会跳出循环!为确保该行不区分大小写,您需要将字符串转换为小写。

可能存在找不到领导者的情况。在这种情况下,您可能希望返回空字符串。因此,初始化Leader_Name为空字符串。在这种情况下,那将是[]. 这样,如果我们通过电子邮件发现没有领导者,MATLAB 将返回[].

您拥有的逻辑非常正确,但我什至不会费心将东西存储到单元格数组中。只需检查文本文件中的每一行,并跟踪当前正在说话的人,直到我们遇到具有另一个:字符的句子。我们可以使用它strfind来促进这一点。但是,我要提到的一个小警告是,如果说话的人:在他们的对话中包含 a,那么这种方法就会失效。

从我看到你的测试用例的对话来看,这可能不会是这样,所以我们没问题。因此,从您当前的代码中借用,只需执行以下操作:

function[Leader_Name] = teamPowerHolder(email)

Leader_Name = []; %// Initialize leader name to empty
name = [];    

email = fopen(email, 'r'); %// Opens my file
lines = fgets(email); %// Reads the first line

while ischar(lines)

    % // Get a line in your e-mail
    lines = fgets(email);

    % // Quit like a boss if you see a -1
    if lines == -1
        break;
    end

    % // Check if this line has a ':' character.
    % // If we do, then another person is talking.
    % // Extract the characters just before the first ':' character
    % // as we don't want the ':' character in the name
    % // If we don't encounter a ':' character, then the same person is
    % // talking so don't change the current name
    idxs = strfind(lines, ':');
    if ~isempty(idxs)
        name = lines(1:idxs(1)-1);
    end    

    % // If we find "do you want" in this sentence, then the leader
    % // is found, so quit.
    if ~isempty(strfind(lower(lines), 'do you want'))
        Leader_Name = name;
        break;
    end
end

通过用你的测试用例运行上面的代码,我得到了:

out2 = teamPowerHolder('teamPowerHolder_convo2.txt')

out2 = 

Anna
于 2014-10-11T15:44:33.770 回答